Difference between revisions of "Add a Drop command to drop an item in the inventory"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(The Method)
(The Method)
 
Line 13: Line 13:
 
         private static void DropItem(List<Item> items, string itemToDrop, int currentLocation)
 
         private static void DropItem(List<Item> items, string itemToDrop, int currentLocation)
 
         {
 
         {
             int indexOfItem;
+
             int indexOfItem= GetIndexOfItem(itemToDrop, -1, items);
            indexOfItem = GetIndexOfItem(itemToDrop, -1, items);
 
 
             if (indexOfItem==-1)
 
             if (indexOfItem==-1)
 
             {
 
             {

Latest revision as of 15:31, 11 December 2018

You can carry as many items as you like in your inventory, however it might be a good idea to be able to drop an item. This will remove it from the inventory but set the location of the item to the current location.

The Method

This looks up the index number of the item required (-1 will be returned if it doesn't exist).

The first if will check to see if no item was found, it will display a message to this affect.

The second will then check if the item is in the inventory, if it is the item is removed from the Inventory by changing its location.

The final else will be run if the item exists but it isn't in the inventory.

        private static void DropItem(List<Item> items, string itemToDrop, int currentLocation)
        {
            int indexOfItem= GetIndexOfItem(itemToDrop, -1, items);
            if (indexOfItem==-1)
            {
                Console.WriteLine("You can't find " + itemToDrop + " to drop.");
            }
            else if (items[indexOfItem].Location == Inventory)
            {
                ChangeLocationOfItem(items, indexOfItem, currentLocation);
                Console.WriteLine("The item has been dropped");
            }
            else
            {
                Console.WriteLine("The item is not in your inventory");
            }
        }

Using the Method

You can add a command into the switch case statement within the playgame method:

                instruction = GetInstruction();
                Command = ExtractCommand(ref instruction);
                switch (Command)
                {
                    case "get":
                        GetItem(items, instruction, characters[0].CurrentLocation, ref stopGame);
                        break;

so find the above code, and add a new case similar to the code below:

                instruction = GetInstruction();
                Command = ExtractCommand(ref instruction);
                switch (Command)
                {
                    case "get":
                        GetItem(items, instruction, characters[0].CurrentLocation, ref stopGame);
                        break;
                    case "drop":
                        DropItem(items, instruction, characters[0].CurrentLocation);
                        break;