Add a command to Drink (you start with a flask in the items)

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

There are multiple stages to this task, you have items which are containers and there is a method already to display the contents of a container. So what we can do is when we fill a container we can create a water object and set the location to the ID of the container.

Fill method

We need to create a method called FillItem:

private static void FillItem(List<Item> items, string container,int CurrentLocation)
{

}

This method will need to accept the list of items, the name of the item to use as the container and the current location.

Like other of these command methods we can start by getting the item itself:

            int indexOfItem;
            indexOfItem = GetIndexOfItem(container, -1, items);
            if (indexOfItem == -1)
            {
                Console.WriteLine("You can't find " + container + " to fill.");
            }

From here we want to add an else condition. The if above checks the item exists, so the else would be run if the item exists. So add the following directly after the {} of the if:

else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("container"))
            {


            }
            else
            {
                Console.WriteLine("The item is not in your inventory or it is not a container");
            }

This adds an else if to look if the item is in the inventory and it is a container within the game. The else will be that it is either not a container or not in the inventory.

The main code will need to go in the else if:

else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("container"))
            {
                    bool test = false;
                    int tempid = 0;
                    foreach(Item i in items)
                    {
                        if (i.ID > 0)
                            tempid = i.ID;
                        if (i.Location==CurrentLocation && i.Status.Contains("water"))
                        {
                            test = true;
                        }
                    }

            }

We need to check if the current location contains a source of water, and we also need to find the current highest ID so that we can create a new item with a unique ID. The bool is set to false and tempid is set to zero ready for our tests. The foreach loop will cycle through each item, if the ID is greater than tempid we change the value of tempid and if the status of this item contains the word "water" we set test to true.

Once this code is run we should have 2 things, a source of water and the highest current ID in use. Now add to the else if, the extra code below to check the value of test, and if it is true create a new item or if it is false to create a message:

else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("container"))
            {
                    bool test = false;
                    int tempid = 0;
                    foreach(Item i in items)
                    {
                        if (i.ID > 0)
                            tempid = i.ID;
                        if (i.Location==CurrentLocation && i.Status.Contains("water"))
                        {
                            test = true;
                        }
                    }

                    if (test)
                    {
                        Item temp = new Item();
                        temp.ID = tempid+1;
                        temp.Location = items[indexOfItem].ID;
                        temp.Description = "water";
                        temp.Name = "water";
                        temp.Status = "drinkable";
                        items.Add(temp);
                        Console.WriteLine(container + " now contains "+temp.Name);
                    }
                    else
                        Console.WriteLine("You can't fill " + container + " here.");
            }

The new item uses tempid+1 for its ID, tempid should be the current highest so adding 1 will be unique. The status is set to drinkable and the name and description are set to just "water". The location of the new item should be the ID of the containing item.

The drink Method

This is the basic structure for the method:

 private static void Drink(List<Item> items, string container)
        {
            int indexOfItem = GetIndexOfItem(container, -1, items);

            if (indexOfItem == -1) //check if item exists
            {
                Console.WriteLine("You can't find " + container + " to drink.");
            }
            else if (items[indexOfItem].Status.Contains("container")&&items[indexOfItem].Location!=Inventory)
            {

            }
            else
            {
                Console.WriteLine(container + " is not a container, or not in your inventory");
            }
        }

The if statement checks the container exists, the else if then checks if it can be used as a container and it is in your inventory. The else will catch when it is isn't a container or in your inventory.

Now the difficult bit, we need to add code to inside the else if statement:

            else if (items[indexOfItem].Status.Contains("container") && items[indexOfItem].Location != Inventory)
            {
                int drinkitem = -1;
                string drinkname = "";
                foreach(Item i in items)
                {
                    if (i.Location == items[indexOfItem].ID && i.Status.Contains("drinkable"))
                    {
                        drinkitem = i.ID;
                        drinkname = i.Name;
                        i.Status = "drunk";
                        i.Location = -1;
                    }
                }
       }

drinkitem and drinkname are to store the ID and name of the drinkable contents of the container. The for each loop will cycle through each item, we need to check that the location of the item is equal to the ID of the container and also that the items status includes the word "drinkalble".

When we find the drinkable contents, we set drinkitem and drinkname, change the status from drinkable to drunk and change the location so it is now longer contained within the container.

Finally we also need to add the appropriate messages to the console, so directly below the code above add:

                if (drinkitem == -1)
                {
                    Console.WriteLine(container + " contains nothing drinkable");
                }
                else
                {
                    Console.WriteLine("you drink the " + drinkname + " in the " + container);
                }

Adding commands

We need to add the following into the switch case statement within the PlayGame method:

                    case "drink":
                        Drink(items, instruction);
                        break;
                    case "fill":
                        FillItem(items, instruction, characters[0].CurrentLocation);
                        break;