Typically in a role playing game, a player can add and remove items from their inventory.
00:07
An inventory is essentially what the player is carrying with them in the game.
00:12
This programme demonstrates how we can store a set of items in a data structure called a list, which is an alternative to having lots of variables.
00:21
We'll start with our boilerplate code with one exception.
00:24
The commands for using lists are in a library called system collections Generic.
00:31
So we need to say that we're using that in this programme in addition to using the usual system commands.
00:38
Let's start by defining the player inventory.
00:41
The player will have two items to begin with, a, sword and a shield.
00:45
Unlike a variable that can only hold one value at a time, a list can hold multiple values, all with the same identifier.
00:54
So to define a list, we use the data type list and inside chevrons we say what type of list it is.
01:01
In this case a list of strings.
01:04
So string, then the identifier or name of the list that will be inventory.
01:09
Now this will be assigned to be a new list of strings.
01:13
It's possible to state how many strings or elements we want our list to begin with.
01:17
And this would be enclosed in brackets.
01:20
But we won't do this.
01:21
So just open and close them.
01:23
But then assign two new elements, sword and shield.
01:27
These are strings, so they're enclosed in quotes, separated with a comma and curly brackets.
01:34
Curly brackets.
01:35
And the data are optional, but since we know we want to add them to the player's inventory, we might as well do that when we define the list.
01:45
A list can have any number of elements inside the computer.
01:48
These elements have been assigned a number automatically.
01:52
Sword is number or index 0 and shield is number or index 1.
01:58
The indexes start at 0, which can be confusing at first, but we also saw this with string formatting arguments in the SaveTheChange programme.
02:08
This programme is never going to end unless the user closes it.
02:12
So while true is a way to write an infinite loop, since true will always be true, we then want the player to choose one of the available actions using the inventory.
02:25
So we'll write a subprogram called chooseaction to do that, passing it the inventory.
02:31
Let's write that subprogram now.
02:33
Static void chooseaction.
02:37
Taking in the list of inventory items, we could have used a global list instead.
02:44
A handy prompt will tell the user, how many items are in their inventory.
02:48
Or put another way, how many elements are in the list.
02:52
The count method tells us how many items are in the list.
02:56
So that's perfect.
02:58
Now we need to prompt the user to choose an action they want to perform.
03:02
The string action becomes the input from the keyboard.
03:07
As there are four possible options, it would be best to use a switch case here.
03:12
Switch action.
03:13
But we could have used an if and an else instead.
03:18
In the case that the user, entered add, call the subprogram additem, to the inventory.
03:25
We'll write that in a moment.
03:27
In the case the user entered drop, we'll call the subprogram dropitem.
03:32
In the case that the user entered, look.
03:35
Call the subprogram look.
03:37
In the case that the user entered, search, Call the subprogram search.
03:42
Then output a blank line just to make it look nicer on the screen when it loops back around for the next action.
03:50
As our programmes are getting bigger, we begin to see the benefit of subprograms to make the code easier to break down, read and follow.
03:58
Ok, let's define what happens when the player adds an item.
04:02
Static void add item, taking in the list of strings, that is the inventory in brackets.
04:12
First, we'll ask the player what they want to add to their inventory.
04:16
And for simplicity, assume they can add anything that is stored in the variable item.
04:22
To add that to the inventory list, we use a method called Add inventory, add and then the data.
04:30
So in brackets item, that will add whatever the user entered at the keyboard to the end of the list and it will automatically be given a number or index by the computer.
04:43
Let's tell the player the item was added by outputting a simple message.
04:48
It doesn't really matter which order we write these subprograms, but let's do the dropping of items next, as it's the opposite to adding static void dropitem passing in the inventory list.
05:03
Again, ask the player which item they want to drop and store that in the variable called item.
05:09
Now we need to see if that item is in our list to be able to drop it.
05:14
So if inventory contains item inside the computer, it's performing a linear search by looking at each element in turn to see if the one that we're looking for is there.
05:28
If the result of that search is true, we can remove the item from the list by using the remove method, inventory, remove and then the data in brackets item.
05:42
The remove method does not remove all the matching elements, just the first one that it finds in the list starting from index zero.
05:50
If you wanted to remove all of the items that matched, you could use remove all instead.
05:56
Otherwise, else tell the player that they don't have that item.
06:01
The subprogram for searching is then pretty much the same code.
06:05
The difference is that if the item is found in the list, we don't want to remove it.
06:09
So we can quickly write that code.
06:11
Now, static void search with the inventory, a prompt, an input and a check.
06:19
If the item is in the list, then two relevant messages depending on the outcome of that check.
06:27
Finally, we can write the look subprogram.
06:29
This will allow the player to ask what have I got?
06:32
In inventory slot two, for example, we can see how internal indexes become useful.
06:39
Start with defining the subprogram and then ask the user, which inventory slot they want to look at.
06:46
We'll store this in an integer variable identified as slot.
06:50
Indexes are numbers, so we need to cast this input to an integer.
06:56
Now, if the slot they chose is less than the number of items in the list, that's great, but we need to check that to prevent an error.
07:05
Let's output what's in the chosen index.
07:08
Inventory, square bracket, slot, that's the index square bracket and then complete the sentence.
07:17
So what we get as an output would be you have a sword.
07:20
In slot one, we learned that a specific index can be referred to by using square brackets after the name of the list.
07:29
We used the slot variable here because we didn't know which slot the user was going to choose.
07:34
But we could also use a number instead of a variable inside the square brackets if we knew which index we wanted.
07:42
Otherwise.
07:43
Else the user must have chosen an invalid slot because the index is greater than or equal to the size of the list.
07:51
Remember, the first item is at index zero, not one.
07:54
That's our programme written.
07:56
Let's give it a run, try a few options and see what happens.