Difference between revisions of "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
(Created page with "=The Examine method= There is already a method to examine the inventory or an item so find this code: <syntaxhighlight lang=c#> private static void Examine(List<Item> items,...")
 
(The Examine method)
 
(One intermediate revision by the same user not shown)
Line 33: Line 33:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The code to write the room description in the else if is lifted from the PlayGame method.
+
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:
 +
 
 +
<syntaxhighlight lang=c#>
 +
private static void Examine(List<Place> places, List<Item> items, List<Character> characters, string itemToExamine, int currentLocation)
 +
</syntaxhighlight>
 +
 
 +
=Using the new Examine Method=
 +
We now need to change the main switch case statement in PlayGame. Look for the code below:
 +
 
 +
<syntaxhighlight lang=c#>
 +
                    case "examine":
 +
                        Examine(items, characters, instruction, characters[0].CurrentLocation);
 +
                        break;
 +
</syntaxhighlight>
 +
 
 +
The word Examine should be underlined because we aren't currently passing the right parameters, so change it to this:
 +
 
 +
<syntaxhighlight lang=c#>
 +
                    case "examine":
 +
                        Examine(places, items, characters, instruction, characters[0].CurrentLocation);
 +
                        break;
 +
</syntaxhighlight>

Latest revision as of 12:59, 13 December 2018

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;