Add to the examine method the ability to examine the room, ie to repeat the description and items

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

The Examine method

There is already a method to examine the inventory or an item so find this code:

private static void Examine(List<Item> items, List<Character> characters, string itemToExamine, int currentLocation)
        {
            int Count = 0;
            if (itemToExamine == "inventory")
            {
                DisplayInventory(items);
            }
            else

An if statement controls if it is the inventory or an item which is examined. We can change this if statement to also add the room, by adding an "else if" into this if statement. So change the code to the code below:

private static void Examine(List<Item> items, List<Character> characters, string itemToExamine, int currentLocation)
        {
            int Count = 0;
            if (itemToExamine == "inventory")
            {
                DisplayInventory(items);
            }
            else if (itemToExamine == "room")
            {
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine(places[characters[0].CurrentLocation - 1].Description);
                    DisplayGettableItemsInLocation(items, characters[0].CurrentLocation);
            }
            else

The code to write the room description in the else if is lifted from the PlayGame method. The issue you will now have is that places doesn't currently get passed into Examine so we need to change that:

private static void Examine(List<Place> places, List<Item> items, List<Character> characters, string itemToExamine, int currentLocation)

Using the new Examine Method

We now need to change the main switch case statement in PlayGame. Look for the code below:

                    case "examine":
                        Examine(items, characters, instruction, characters[0].CurrentLocation);
                        break;

The word Examine should be underlined because we aren't currently passing the right parameters, so change it to this:

                    case "examine":
                        Examine(places, items, characters, instruction, characters[0].CurrentLocation);
                        break;