Add the code to use an item as a weapon(truncheon has status of weapon)

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

The chance of this question coming up is very high, using flag2.gme you start in the guard room with the guard.

An Attack method

We need to define a new method for the Attack command. So create the following:

private static void Attack(List<Item> items, List<Character> characters, string attackVictim)
{

}

The parameters are required because we need to check if we have an item with a status of weapon, we will also need the list of characters to make sure we can only attack another character, the "attackVictim" string will be the name of the character to attack from the command. Firstly to check if we have a weapon. Add this code inside the Attack method:

 private static void Attack(List<Item> items, List<Character> characters, string attackVictim)
        {
            string weapon = "";
            bool victim = false;

            //do they have a weapon
            foreach(Item i in items)
            {
                if(i.Location==Inventory && i.Status.Contains("weapon"))
                {
                    weapon = i.Name;
                }
            }
}

The foreach loop will cycle through each item within the game. We are checking to see that the item is in the inventory and the status of the item contains the word "weapon". If these conditions are true we record the name of the weapon used.

Now we have check weapon, we also need to check the characters. Look at his code (put it after the foreach loop above):

            //get character to attack
            foreach(Character c in characters)
            {
                if (c.Name==attackVictim)
                {
                    victim = true;
                    // are the characters in the same location
                    if (characters[0].CurrentLocation == c.CurrentLocation)
                    {
                        //do we have a weapon
                        if(weapon!="")
                        {
                            Console.WriteLine("You attack the guard with the " + weapon+ " and he is stunned");
                            TakeItemFromOtherCharacter(items, c.ID);
                        }
                        else
                        {
                            Console.WriteLine("You have no weapon to use");
                        }
                    }
                }
            }

            if (!victim)
                Console.WriteLine("Can't find the character to attach");

The foreach code cycles through each character, we check the name of the character with the "attackVictim" name. We still need to check that both the playing character and the character to attack are in the same location. If we had a weapon it tells you the attack was successful and will allow you to take an item from the other character. If you had no weapon it will tell you it found no weapon to use.

Using the Attack Method

Within the switch case statement in the PlayGame method, add the following case:

                    case "attack":
                        Attack(items, characters, instruction);
                        break;