Difference between revisions of "Add a command to Eat (you start with an apple in the items)"
(→A New Method) |
(→A New Method) |
||
Line 13: | Line 13: | ||
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
− | private static void EatItem(List<Item> items, string itemToEat | + | private static void EatItem(List<Item> items, string itemToEat) |
{ | { | ||
int indexOfItem = GetIndexOfItem(itemToEat, -1, items); | int indexOfItem = GetIndexOfItem(itemToEat, -1, items); | ||
Line 24: | Line 24: | ||
else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("edible")) | else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("edible")) | ||
{ | { | ||
− | |||
items.RemoveAt(indexOfItem); | items.RemoveAt(indexOfItem); | ||
Console.WriteLine("The item has been eaten"); | Console.WriteLine("The item has been eaten"); | ||
Line 33: | Line 32: | ||
} | } | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | =Making the Command= | ||
+ | In the playgame method, you need to find the switch case statement for the commands: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | instruction = GetInstruction(); | ||
+ | Command = ExtractCommand(ref instruction); | ||
+ | switch (Command) | ||
+ | { | ||
+ | case "get": | ||
+ | GetItem(items, instruction, characters[0].CurrentLocation, ref stopGame); | ||
+ | break; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Now add a case for the "Eat" command: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | instruction = GetInstruction(); | ||
+ | Command = ExtractCommand(ref instruction); | ||
+ | switch (Command) | ||
+ | { | ||
+ | case "get": | ||
+ | GetItem(items, instruction, characters[0].CurrentLocation, ref stopGame); | ||
+ | break; | ||
+ | case "eat": | ||
+ | EatItem(items, instruction); | ||
+ | break; | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 13:55, 11 December 2018
Overview
You start the game with an apple in your inventory, when you dump the data for an apple it has a status of "edible". So clearly you are expected to program a method to eat the apple.
A New Method
We need to pass this method the list of items, and the name of the item you want to eat.
The first if, checks to see if the item exists
The second if, checks to see if the item is in the inventory and it is edible
The else is for when the item is not edible or not in your inventory.
private static void EatItem(List<Item> items, string itemToEat)
{
int indexOfItem = GetIndexOfItem(itemToEat, -1, items);
if (indexOfItem == -1) //check if item exists
{
Console.WriteLine("You can't find " + itemToEat + " to eat.");
}
//check if item is edible & in inventory
else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("edible"))
{
items.RemoveAt(indexOfItem);
Console.WriteLine("The item has been eaten");
}
else //not in inventory or not edible
{
Console.WriteLine("You cannot eat " + itemToEat);
}
}
Making the Command
In the playgame method, you need to find the switch case statement for the commands:
instruction = GetInstruction();
Command = ExtractCommand(ref instruction);
switch (Command)
{
case "get":
GetItem(items, instruction, characters[0].CurrentLocation, ref stopGame);
break;
Now add a case for the "Eat" command:
instruction = GetInstruction();
Command = ExtractCommand(ref instruction);
switch (Command)
{
case "get":
GetItem(items, instruction, characters[0].CurrentLocation, ref stopGame);
break;
case "eat":
EatItem(items, instruction);
break;