Difference between revisions of "Add a command to Drink (you start with a flask in the items)"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "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 method)
Line 63: Line 63:
 
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:
 
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:
  
 +
<syntaxhighlight lang=c#>
 
else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("container"))
 
else if (items[indexOfItem].Location == Inventory && items[indexOfItem].Status.Contains("container"))
 
             {
 
             {

Revision as of 14:27, 18 December 2018

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.