Add a Drop command to drop an item in the inventory

From TRCCompSci - AQA Computer Science
Revision as of 15:15, 11 December 2018 by Admin (talk | contribs) (Created page with "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 locat...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

        private static void DropItem(List<Item> items, string itemToDrop, int currentLocation)
        {
            int indexOfItem;
            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;