|
Title: Tutorial Six: Tables Post by: Access_Denied on June 14, 2007, 03:22:30 AM In
today's lesson, we'll be focusing on tables. (Also known as arrays.) In
programming, a table is something that holds large amounts of data.
Here's and example:
myTable = {} myTable[1] = "I've " myTable[2] = "made " myTable[3] = "my " myTable[4] = "first " myTable[5] = "table!" You're probably wondering "How would I use a table?". Well, you could use it like this: screen:print(100,100,myTable[3],green) Now, what this would do is print the variable "myTable[3]" to the screen. Since myTable[3] = "my ", then it would print "my" to the screen. This comes in handy when you will be printing the same thing over and over again. Here's another type of table: ourTable = {} ourTable = {x = 27, y = 54, z = 78} What this says is that "x is equal to 27", "y is equal to 54", and "z is equal to 78". But you are probably asking yourself, "How do I print THAT to the screen??" Like this: screen:print(100,100,ourTable.y,green) In tables like the one above, all you have to do to use them is: tablename.variable_in_table It could even go like this: theTable = {14,39,27,30,28} Then to use one of those numbers: theTable[3] = theNumber "theTable[3]" would be 27, since 27 is the third nubmer in the table. It's that easy. Are you ready? Let's start our program. --Tables --August 12,2006 green = Color.new(0,255,0) Now we are going to do something different. You don't have to understand it until later, but for now, just put it in. math.randomseed(os.time()) Now we are going to make our table: LotteryNumbers = {2,12,16,19,32,48} Let's say that these are the numbers you have chosen for the lottery. We want to make it so a random number pops up, and if it matches one of your numbers, you win. So you are going to need a variable that is the winning number. winningNumber = 1 We'll just put that at 1 for now. We'll change it later. Now we need to make it so "winningNumber" is a random number everytime. oldpad = Controls.read() while true do screen:clear() pad = Controls.read() if pad:cross() and oldpad:cross() ~= pad:cross() then winningNumber = math.random(1,65) end Now, right now you're probably like "WTF?". Calm down, I'll explain. The first unusual thing you should see is the "oldpad = Controls.read()" Don't panic. Have you ever played a video game, where you have a menu, but when you press down, instead of going down only one choice, it skips down like 5 choices? This is because, everytime you press a button, the PSP reads it, and knows that you pressed a button, so it does what it is programmed to do. But if you hold that button down, the PSP thinks you are pressing the button more than once, so it does what it is supposed to do more than once. By adding this line, among others, we can make it so the PSP only does it once. The next line that is unfamiliar is "if pad:cross() and oldpad:cross() ~= pad:cross() then". This makes it so when you press the CROSS button, it only presses once instead of multiple times. The next line is: winningNumber = math.random(1,65)" What this does is, it pick a randome number between 1 and 65. So every time you press X, winningNumber is equal to a random number between 1 and 65. Remember that weird line at the beginning? That was for this. That line make it so the PSP chooses a different number every time. Now, back to some familiar stuff. Put these lines in, then I'll explain if winningNumber == Lotterynumbers[1] then screen:print(100,100,"You Win!",green) end if winningNumber == Lotterynumbers[2] then screen:print(100,100,"You Win!",green) end if winningNumber == Lotterynumbers[3] then screen:print(100,100,"You Win!",green) end if winningNumber == Lotterynumbers[4] then screen:print(100,100,"You Win!",green) end if winningNumber == Lotterynumbers[5] then screen:print(100,100,"You Win!",green) end if winningNumber == Lotterynumbers[6] then screen:print(100,100,"You Win!",green) end What these lines are saying is, that if the winningNumber is equal to any of our LotteryNumbers, it'll print you win. Notice you have to use two == signs if it's in the first line of an if/then statement. Now one last thing before we finish it off. Put this screen:print(10,10,winningNumber,green) That line prints the winningNumber to the screen, in case you want to see it. Now finish it up with screen.waitVblankStart() screen.flip() oldpad = pad end What? Oh, you want to know what that third line is. Well that just completes the thing that we did earlier with the "oldpad = Controls.read()". You don't need to know much about it, just know that if has to be there, if you're going to be doing what we are doing. BTW, to figure out what that whole "oldpad" thing does. Run your program, then take out that third line, and run it again, see what happens. |