Adapt the Use method to switch the torch on and off

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

The torch has a status of "lightable" and also "off". So can we switch on the torch instead of just writing out that it is now easier to see.

Current Use method

 private static void UseItem(List<Item> items, string itemToUse, int currentLocation, ref bool stopGame, List<Place> places)
        {
            int position, indexOfItem;
            string resultForCommand, subCommand = "", subCommandParameter = "";
            indexOfItem = GetIndexOfItem(itemToUse, -1, items);
            if (indexOfItem != -1)
            {
                if (items[indexOfItem].Location == Inventory || (items[indexOfItem].Location == currentLocation && items[indexOfItem].Status.Contains("usable")))
                {
                    position = GetPositionOfCommand(items[indexOfItem].Commands, "use");
                    resultForCommand = GetResultForCommand(items[indexOfItem].Results, position);
                    ExtractResultForCommand(ref subCommand, ref subCommandParameter, resultForCommand);
                    if (subCommand == "say")
                    {
                        Say(subCommandParameter);
                    }
                    else if (subCommand == "lockunlock")
                    {
                        int IndexOfItemToLockUnlock, IndexOfOtherSideItemToLockUnlock;
                        IndexOfItemToLockUnlock = GetIndexOfItem("", Convert.ToInt32(subCommandParameter), items);
                        IndexOfOtherSideItemToLockUnlock = GetIndexOfItem("", Convert.ToInt32(subCommandParameter) + IDDifferenceForObjectInTwoLocations, items);
                        ChangeStatusOfDoor(items, currentLocation, IndexOfItemToLockUnlock, IndexOfOtherSideItemToLockUnlock);
                    }
                    else if (subCommand == "roll")
                    {
                        Say("You have rolled a " + RollDie(resultForCommand[5].ToString(), resultForCommand[7].ToString()));
                    }
                    return;
                }
            }
            Console.WriteLine("You can't use that!");
        }

In the middle of this method find the line if (subCommand == "say"). We need to add a bit of logic before this:

                    if(items[indexOfItem].Status.Contains("lightable"))
                    {
                        if(items[indexOfItem].Status.Contains("off"))
                        {
                            ChangeStatusOfItem(items, indexOfItem, "off", "on");
                        }
                        else if (items[indexOfItem].Status.Contains("on"))
                        {
                            ChangeStatusOfItem(items, indexOfItem, "on", "off");
                        }
                    }

This code will check if the item used is "lightable", if it is it check the status for "off" or "on". It will run the ChangeStatusOfItem method to change the value to off or on. This method was added by a previous improvement Improve change item status method, you can replace but not remove just a single word from the status.