Drop hier links of afbeeldingen om ze aan de editor toe te voegen.
RPG inventory

Grab your sword and shield.

Try

Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

Lists are useful when you want to have multiple variables with the same identifier. E.g. instead of using three variables: inventory1, inventory2, inventory3, we would use one list: inventory[0], inventory[1], inventory[2] etc.

This is because the index inside the square brackets can be replaced with a variable. E.g. inventory[choice]. You cannot do this with variables.

Lists are dynamic which means the number of indexes can grow and shrink as needed when a program is running. They are part of the System.Collections.Generic library, so to use lists in a program you have to include the following command at the top of the program:

using System.Collections.Generic;
 

By including the library you can now use the command:

List<data type>x = new List<data type>;

This defines x as a new empty list, where the elements are of the data type integer, string, double etc.

List<string> x = new List<string> {"Craig", "Dave"};

Defines x as a list containing two values: the string “Craig” in index 0 and the string “Dave” in index 1.

The new commands used in this program and others that may be useful. Select them below to learn more:

x = y[z];

x is assigned the value stored in list y at index z. Remember that indexes start at 0 so y[0] is the first item in the list y. Be careful that z is a valid index and not out of bounds of the list.

int x = y.Count;

x is assigned the number of items in list y.

int x = y.IndexOf(z);

x is assigned the index of where string z is in list y.

int x = y.Length;

x is assigned the number of items in list y. Can also be used to return the number of characters in string y.

x.Add(y);

Adds y to the end of list x.

x.Contains(y);

Returns true if y is in list x or false if not.

x.Insert(y,z);

Inserts z at index y into list x. All other elements are moved down by one index.

x.Remove(y);

Removes the first occurrence of y from list x. If the element is found and removed, the method returns true, otherwise it returns false. Remember that this will reduce the index of all other elements stored after y by -1.

x.Reverse();

Reverses the order of all items in list x.

x.Sort();

Sorts all the items in list x into ascending order using a Quicksort.

Investigate

Questions to think about with this program to check your understanding:

Item question

Identify a list in the program.

Reveal answer

inventory is a list. It can store more than one value with a single identifier. Each value is assigned an index.

Structure question

State what is stored in index 1 in the data structure assigned in line 94.

Reveal answer

“shield” because indexes start at 0 in lists.

Make

Change the program so that it:

  1. Includes a new “craft” action that the player can take in the choose_action subprogram.
  2. Includes a new craft_item subprogram that:
  3. Asks the user which item they want to craft.
  4. If the item is a “charm” and there is both a “gem” and “leather” in the inventory, these two items are removed and a “charm” is added.
  5. Reports whether the item was crafted or whether the items do not exist in the inventory.

Typical inputs and outputs from the program would be:

Assuming [‘sword’, ‘shield’]

You have 2 items in your inventory.
What action do you want to take? (add/craft/look/drop/search/quit)
craft
What item do you want to craft?
charm
You do not have the items to do this.

Assuming [‘sword’, ‘shield’, ‘gem’, ‘leather’]

You have 4 items in your inventory.
What action do you want to take? (add/craft/look/drop/search/quit)
craft
What item do you want to craft?
charm
charm crafted.

Assuming [‘sword’, ‘shield’, ‘charm’]

You have 3 items in your inventory.
What action do you want to take? (add/craft/look/drop/search/quit)
look
Which inventory slot do you want to look at?
2
You have a charm in slot 2
🆘 If you're really stuck, use this Parsons code sorting exercise
choose_action, main program
// Choose an action (returns false when the player quits)
static bool choose_action(List inventory)
{
---
    Console.WriteLine($"You have {inventory.Count} items in your inventory.");
    Console.WriteLine("What action do you want to take? (add/craft/look/drop/search/quit)");
    string action = Console.ReadLine();
---
    switch (action)
    {
---
        case "add": add_item(inventory); break;
        case "craft": craft_item(inventory); break;
        case "drop": drop_item(inventory); break;
        case "look": look(inventory); break;
        case "search": search(inventory); break;
        case "quit": return false;
---
    }
---
    Console.WriteLine();
    return true;
---
}


---
public static void Main(string[] args)
{
---
    List inventory = new List { "sword", "shield" };
    while (choose_action(inventory))
    {
---
    }
}
</pre>
</dw-parsons-puzzle>

add_item, search, drop_item
// Add an item to the inventory
static void add_item(List inventory)
{
---
    Console.WriteLine("What item are you adding to your inventory?");
    string item = Console.ReadLine();
    inventory.Add(item);
    Console.WriteLine("Item added.");
---
}


---
// Search for an item in the inventory
static void search(List inventory)
{
---
    Console.WriteLine("What item do you want to see if you have?");
    string item = Console.ReadLine();
    if (inventory.Contains(item))
    {
---
        Console.WriteLine("You have the item.");
---
    }
---
    else
    {
---
        Console.WriteLine($"You do not have a {item}");
---
    }
---
}


---
// Remove an item from the inventory
static void drop_item(List inventory)
{
---
    Console.WriteLine("What item do you want to drop?");
    string item = Console.ReadLine();
    if (inventory.Contains(item))
    {
---
        inventory.Remove(item);
        Console.WriteLine("Item dropped.");
---
    }
---
    else
    {
---
        Console.WriteLine("You do not have this item.");
---
    }
}
</pre>
</dw-parsons-puzzle>

look, craft_item
// Show an item in an inventory slot
static void look(List inventory)
{
---
    Console.WriteLine("Which inventory slot do you want to look at?");
    int slot = Convert.ToInt32(Console.ReadLine());
    if (slot < inventory.Count)
    {
---
        Console.WriteLine($"You have a {inventory[slot]} in slot {slot}");
---
    }
---
    else
    {
---
        Console.WriteLine("Invalid slot.");
---
    }
---
}


---
// Craft an item
static void craft_item(List inventory)
{
---
    Console.WriteLine("What item do you want to craft?");
    string item = Console.ReadLine();
    if (item == "charm" && inventory.Contains("gem") && inventory.Contains("leather"))
    {
---
        inventory.Remove("gem");
        inventory.Remove("leather");
        inventory.Add("charm");
        Console.WriteLine($"{item} crafted.");
---
    }
---
    else
    {
---
        Console.WriteLine("You do not have the items to do this.");
---
    }
}
</pre>
</dw-parsons-puzzle>

</details>