Tutorial 27: Lists
Using lists in games...
HOME >> Tutorials >> Tutorial 27: Lists

Great! We are now back to learning information that will help us in making good games. Now, the idea of lists should not be new to you because we initially came into contact with them way back in Tutorial 3 where we learnt how to store and delete them. In this tutorial, you not only learn basic manipulation of lists, but also how to use lists to save information from a game and store it so that it can be loaded the next time the game is played.

New Commands:

augment( Concatenates (puts together) the elements of two lists
ClrList Sets the dimension of a list to 0
Fill( Stores a specified value into each element of a list

augment(listA,listB) ClrList listname1[,listname2, ..., listname n] Fill(value,listname)

The Codes:
There will be a few codes, each one getting harder and more complex. This first one will be straightforward, just showing you how to use the new commands and refreshing your memory on some commands previously learnt. You may want to review the commands dim( and L.

: For AShell, SOS, and TI-Explorer
ClrHome Clears the home screen
3STOREdim(LBEN):6STOREdim(LBEN2) LBEN has 3 elements and LBEN2 has 5
{2,8,36}STOREBEN LBEN is given the value of {2,8,36}
Fill(5,LBEN2) Fills every element of LBEN2 with 5s
Disp LBEN,LBEN2 Displays LBEN and LBEN2
Pause Suspends program and waits for use to press ENTER
augment(LBEN,LBEN2)STORELBEN3 Concatenates LBEN2 to the end of LBEN and stores it to LBEN3
Output(4,1,LBEN3 Displays LBEN3 on the fourth line
Pause Suspends program and waits for use to press ENTER
ClrList LBEN,LBEN2,LBEN3 Makes the dimension of the three lists 0

press ENTER press ENTER

Run the program. First off, let me just tell you that you are supposed to get that "ERR: INVALID DIM" message. This is because the calculator cannot display a list with a dimension of 0. What would it display? I just added that last line in the program just so that you could see exactly what ClrList does. Let me just run down the entire code. The third line just initializes both lists, the first with a dimension of 3 and the second with a dimension of 4. Now, let's take a look at the next line: "{2,8,36}STOREBEN." You may have thought that I left out the L and taking initiative put it in on your calculator. Well, I left out the L on purpose because it is not necessary when you're storing list elements into a user-defined list. You can just write the name and leave out the L. This only works when you are storing list elements into the list as we are doing. Let me just explain one more line: "Output(4,1,LBEN3." You may not know this (I don't think I've told you before), but Output( and Disp act very differently. In one way that they act differently, which is shown in the code, is that text that is longer than 16 characters while using Output, gets wrapped onto the next line. Also, Output( displays lists with the commas instead of spaces like Disp. I just thought I should mention that. In the next code, I will show you how to concatenate three different-sized lists together and then split them up into 3 equal lists. Note: I will be using the same lists over again, and there is an easier way to write "LBEN" then first looking for L and then typing in B-E-N. If you just press 2ND and STAT [LIST], you'll see a list of all the user-created lists.

: For AShell, SOS, and TI-Explorer
ClrHome Clears the home screen
{2,8,36}STOREBEN LBEN is given the value of {2,8,36}
{7,3,11,24}STOREBEN2 LBEN2 is given the value of {7,3,11,24}
{9,13,95,33,61}STOREBEN3 LBEN3 is given the value of {9,13,95,33,61}
Disp LBEN,LBEN2,LBEN3 Displays LBEN, LBEN2, and LBEN3
Pause Suspends program and waits for use to press ENTER
augment(LBEN,augment(LBEN2,LBEN3))STORELBEN4 Concatenates all three lists together and stores it to LBEN4
Output(5,2,"CONCATENATED:" Displays "CONCATENATED:" on the fifth line
Output(7,1,LBEN4 Displays LBEN4 on the seventh line
Pause Suspends program and waits for use to press ENTER
ClrHome Clears the home screen
LBEN4(X)STORELNEW(X) Stores first four elements of LBEN4 into LNEW
LBEN4(X+4)STORELNEW2(X) Stores second four elements of LBEN4 into LNEW2
LBEN4(X+8)STORELNEW3(X) Stores third four elements of LBEN4 into LNEW3
End End of For loop
Disp "SPLIT APART:","" Displays "SPLIT APART:" and a blank line
Disp LNEW,LNEW2,LNEW3 Displays LNEW, LNEW2, and LNEW3

press ENTER press ENTER

So, did you like that? It may be a bit confusing, but after I explain, it should be come clearer. The third, fourth, and fifth lines, use the new method of storing into a list without using the L. The first part that might give you trouble is the line after the first Pause command. What actually happens is that the "augment(LBEN2,LBEN3)" part is done first. This leaves you with a list that looks like this: "{7 3 11 24 9 13 95 33 61}. Then the calculator concatenates that list to the end of LBEN, thus making a huge list, LBEN4, that reads: {2 8 36 7 3 11 24 9 13 95 33 61}. You should now understand that part. It wasn't too hard, was it? If you really want to prove to yourself that you completely understand that, try putting 4 lists together. LBEN4 gets displayed and then you come upon this For loop. It may look a little complex, but if you look closely at the logic, it is actually quite simple. Let me run you through what happens. First, X is initially stored as 1. So then it reads the next line, which actually says, "LBEN4(1)STORELNEW(1)" because X is 1. The line after that actually says,"LBEN4(5)STORELNEW2(1)" once again because X is 1. Finally, the last line in the For loop reads, "LBEN4(9)STORELNEW3(1)." So, after going through the entire For loop once, LNEW is {2}, LNEW2 is {3}, and LNEW3 is {13}. Then the For loop loops back around and now X becomes 2. So the three lines now read as: "LBEN4(2)STORELNEW(2)," "LBEN4(6)STORELNEW2(2)," and "LBEN4(10)STORELNEW3(3)." So now, after going through the entire loop twice, LNEW is {2 8}, LNEW2 is {3 11}, and LNEW3 is {13 95}. This process occurs two more times so that LNEW is {2 8 36 7}, LNEW2 is {3 11 24 9}, and LNEW3 is {13 95 33 61}. Not to complex now, huh? At the end of the code, all three newly user-created lists are displayed. In the final code that is coming up, I will show you how to save different pieces of information before quitting a game, and also how to load it when the game is played the next time. You may want to take a quick peak at How To Setup A High Score Function just as a reference.

Before you start on the code let me just explain a few things first. We are going to pretend as if the code is part of a RPG, where you have health, shield, bullets for your guns, knives, and nukes, among other things that don't need to be listed. User variable H will stand for health, S will stand for shield, B for bullets, K for knives, and finally N for nukes. First, I'm going to show you the "game" and how to save those 5 pieces of information. I'll explain everything in that code and then add on the loading function. I think it's better to do it separately so not to explain too much at one time.

: For AShell, SOS, and TI-Explorer
ClrHome Clears the home screen
Lbl 1
Menu("","PLAY",2) Sets up a menu with only one option: PLAY
Lbl 2
163STOREH Your health is at 163 units
57STORES Your shield is at 57 percent in tact
79STOREB You have 79 bullets for you gun
36STOREK You have 36 knives
2STOREN You have 2 nukes
Lbl 3
Output(1,6,"STATUS Displays "STATUS:" on the top line
Output(3,1,"HEALTH: Displays "HEALTH:" on the third line
Output(3,10,H Displays the value for health on the same line
Output(4,1,"SHIELD: Displays "SHIELD:" on the fourth line
Output(4,10,S Displays the value for shield on the same line
Output(5,1,"BULLETS: Displays "BULLETS:" on the fifth line
Output(5,10,B Displays the value for bullets on the same line
Output(6,1,"KNIVES: Displays "KNIVES:" on the sixth line
Output(6,10,K Displays the value for knives on the same line
Output(7,1,"NUKES: Displays "NUKES:" on the seventh line
Output(7,10,N Displays the value for nukes on the same line
Repeat G=22
getKeySTOREG Repeat until the MODE key is pressed
End End Repeat loop
Lbl 4
Menu("SAVE FIRST?","YES",5,"NO",9) A menu that asks whether to save before quitting
Lbl 5
ClrHome Clears the home screen
H-20STORELGAME(1) Store new health into the first entry of LGAME
S+3STORELGAME(2) Store new shield into the second entry of LGAME
B-9STORELGAME(3) Store new bullets into the third entry of LGAME
K+16STORELGAME(4) Store new knives into the fourth entry of LGAME
N+4STORELGAME(5) Store new nukes into the fifth entry of LGAME
Output(4,4,"GAME SAVED Displays "GAMED SAVED" on the fourth line
Pause Suspends program and waits for use to press ENTER
Lbl 9
DelVar H:DelVar S:DelVar B Delete from memory the contents of H, S & B
DelVar K:DelVar N Delete from memory the contents of K & N
ClrHome Clears the home screen

press ENTER press MODE
choose YES

So, did you know you could do that with Menu(...not even have a title? You'll hardly ever need to do something like that, but it is something that you should know how to do. Next, random values are stored into the 5 variables, which are then displayed. The next few lines weren't really necessary, but I thought I should add it in because you might want to do something similar in a game you make. The Repeat loop waits for the user to press the MODE button, which serves as the Quit button. Once MODE is pressed, the program goes to another menu, which asks if you want to save the 5 pieces of information before quitting. If you pick "YES," the 5 pieces of info are then stored into a newly created user-created list called GAME. The reason why I just didn't do, "HSTORELGAME(1)" is because I wanted the values that are saved to be different than how they originally start off because that's what will happen during the course of a regular game. It was not necessary for me to add those DelVar statements at the end, but I wanted to simulate those variables being changed by another program and to prove to you that this method works. In the next code, I will show you the loading function, which you'll need to add right after "Lbl 2."

5STOREdim(LGAME) LGAME has 5 elements
If LGAME(1)>0:Then If their is a value for health of LGAME, then
Menu("FINISH LAST GAME","YES",2A,"NO",2B) A menu that asks if you want to continue the last game
Lbl 2A
LGAME(1)STOREH Store first entry of LGAME into H (health)
LGAME(2)STORES Store second entry of LGAME into S (shield)
LGAME(3)STOREB Store third entry of LGAME into B (bullets)
LGAME(4)STOREK Store fourth entry of LGAME into K (knives)
LGAME(5)STOREN Store fifth entry of LGAME into N (nukes)
Fill(0,LGAME) Fills every element of LGAME with 0s
Goto 3 Go to Label 3
End End If-Then statement
Lbl 2B
Fill(0,LGAME) Fills every element of LGAME with 0s

press ENTER choose "YES"
press MODE choose "YES" and so on...

Okay, run the program with all this newly added code. If you've done exactly what I've said, that initial menu, which asks if you want to "FINISH LAST GAME," should appear. If you choose yes, those new values for the 5 pieces of information will display. If you choose no, the original values will be displayed. Now, let me explain the two "Fill(0,LGAME)." They are basically used to clear the information. So, if you do choose to load the info from the previous game, after the info is loaded, it is erased. If you choose not to load the last game's information, it is erased anyway. There are many ways you could do this part. You could get rid of the second "Fill(0,LGAME)" so that if the user chooses not to load the information that they'll have the option next time they play. You could remove both of them so that they could continue to load that same information over and over again. Doing this might actually be beneficial if you have a pretty hard game. Let's say for instance that the user has gotten to a level that he's never gotten to before and decides to save and quit. Then the next time he plays the game and loads the information, but dies quickly because he made a mistake. He might want to start from that place he had saved the next time he plays the game. He might want to keep on starting from that saved place until he gets significantly farther along and saves that place. Do you see what I'm saying? The way you use those "Fill(0,LGAME)" statements all depend on how you want to set up your game.

If you were able to fully grasp everything that was taught, you are becoming a really good programmer. Using the features taught in this tutorial will help make your game much better. Many people want the option to save/load their positions in the game. Using this knowledge will help you give the users of your games what they want. Coming up in the next tutorial, you will use another mathematical feature, the matrix, to aid you in making games.

If you do not understand a particular part in this lesson, have suggestions, or find any problems please contact me.

Previous Tutorial Next Tutorial
Tutorial 26 Tutorial 28

Problems with this page?
Contact the Webmaster.