Difference between revisions of "Add a command to Eat (you start with an apple in the items)"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=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...")
 
(A New Method)
Line 9: Line 9:
 
The second if, checks to see if the item is in the inventory and it is edible
 
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.
+
The else is for when the item is not edible or not in your inventory.
  
  

Revision as of 13:18, 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 currentLocation)
        {
            int indexOfItem = GetIndexOfItem(itemToEat, -1, items);

            if (indexOfItem == -1)
            {
                Console.WriteLine("You can't find " + itemToEat + " to eat.");
            }
            else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("edible"))
            {
                currentLocation = GetPositionOfCommand(items[indexOfItem].Commands, "eat");
                items.RemoveAt(indexOfItem);
                Console.WriteLine("The item has been eaten");
            }
            else
            {
                Console.WriteLine("You cannot eat " + itemToEat);
            }
        }