All my knowledge I have got by testing on boring math lessons so I've tried a lot and know what to do and what you shouldn't do. :)
Parts in this series:
- Part 1 - The very basics
- Part 2 - If statements and loops
- Part 3 - Receiving Inputs
- Part 4 - Matrixes and Lists
- Part 5 - Creating a Game
Anyway, let's start with Part 5 - Creating a Game.
Now we have come as far as the last Part of this tutorial series. I will probably continue making TI 82 tutorials but not connected with these 5. Anyway, now when we've come this far, we'll try to use what we learned to create a game in the calculator. Let's go.
The game we will create is a textbased fifteen puzzle, this is a lot of code and maybe to much to wrote in your calculator(I have done that) so if you don't have a wire to connect with just take this as a way to learn how and where to use these things you have learned in the four other parts.
ClrHome
{4,4}->dim([A])
We start with something new, the first row just clear the board from everything that were there before. Then we declare a 4x4 matrix, this will be the board.
3->[A](1,1) 5->[A](2,1) 7->[A](3,1) 1->[A](4,1) 2->[A](1,2) 9->[A](2,2) 10->[A](3,2) 12->[A](4,2) 9->[A](1,3) 14->[A](2,3) 11->[A](3,3) 4->[A](4,3) 13->[A](1,4) 8->[A](2,4) 0->[A](3,4) 15->[A](4,4)
Since we haven't done anything about randomized numbers and it will probably only be a little bit confusing now I haven't added anything which randomly set the positions of all numbers, so this is just manually added which means the board will always look the same in the beginning.
-1->X Goto P Lbl B
The X variable is used together with the Y variable to keep track on where the empty spot is, now we set it to -1 just so we now neither X nor Y has been set. Then we go to the label P (stands for print) which will print out the board and then it will jump back to label B(stands for back). The P label is further down where it is used the most so we take that one later.
For (I,1,4,1) For (J,1,4,1) If [A](I,J) =0:Then I->X J->Y Stop End If X!=-1 Stop End
Here we go though two for loops (I and J) to look for the Empty spot. If this is found, X and Y is set and then we stop the loops with the Stop function.
Repeat([A](1,1)=1 and [A](2,1)=2 and [A](3,1)=3 and [A](4,1)=4 and [A](1,2)=5 and [A](2,2)=6 and [A](2,3)=7 and [A](2,4)=8 and [A](3,1)=9 and [A](3,2)=10 and [A](3,3)=11 and [A](3,4)=12 and [A](4,1)=13 and [A](4,2)=14 and [A](4,3)=15 and [A](4,4)=0)
This looks confusing, but it's basically the thing that keeps the game working until the user have completed it. It's the beginning of a repeat loop which will stop looping when all pieces are at the right position.
Repeat (K=24 or K=25 or K=26 or K=34) getKey->K End
This part of code just waits for the user to press one of the arrows.
If K=24 and X-1!=0:Then [A](X-1,Y)->[A](X,Y) 0->[A](X-1,Y) X-1->X Goto P End If K=25 and Y-1!=0:Then [A](X,Y-1)->[A](X,Y) 0->[A](X,Y-1) Y-1->Y Goto P End If K=26 and X+1!=5:Then [A](X+1,Y)->[A](X,Y) 0->[A](X+1,Y) X+1->X Goto P End If K=34 and Y+1!=5:Then [A](X,Y+1)->[A](X,Y) 0->[A](X,Y+1) Y+1->Y Goto P End
These blocks of code is the same thing, one for each direction. It first check if it possible to move in that direction(not at the edge) and then it swap the current position with the selected one(the selected one gets the value 0 and the old one gets the value of the selected). After updating X or Y to keep track of the empty spot we'll goto label P to print the changes.
Goto E
If we're still here it means none of the above block worked, then we go to label E(stands for End) to jump past label P.
Lbl P ClrHome For (I,1,4,1) For (J,1,4,1) If [A](I,J) <10 and [A](I,J) !=0:Then Output(J,I,[A](I,J)) Else If [A](I,J) = 10 Output(J,I,"A") If [A](I,J) = 11 Output(J,I,"B") If [A](I,J) = 12 Output(J,I,"C") If [A](I,J) = 13 Output(J,I,"D") If [A](I,J) = 14 Output(J,I,"E") If [A](I,J) = 15 Output(J,I,"F") End End End
First we just clears the board so we can add the new one. Then we go though the results in a nested for loop. If the value is between 1 and 9 we cab just print out the number, but if it's higher then that we need to replace them with a letter(since a 2 digit number will take to much space), so we prints out A to F instead.
Output is an alternative to Disp. Output prints out the selected text(parameter 3) at the current Y index(Parameter 1) and the current X index(parameter 2) on the screen.
If X=-1:Then Goto B Else Goto D End
If X is equals to -1 it means this printing is the one we're doing in the beginning so we have to go back to label B, or else we go to label D(stands for Done).
Lbl D End End Lbl E
This is where the code comes to in the end of each move. If we made a move the code will run from label D, or if we didn't made a move it will run from label E. The reason we do this is since when we moved we jumped over from some end tags with the GoTo function so we have to add two extra here.
End ClrHome Disp "Good Work"
Here's the end of the code. If we manage to complete the big repeat loop(means the user have completed the game) the boards is cleared and a message is printed out telling the user how good he/she was.
Here comes the whole block without comments:
ClrHome
{4,4}->dim([A])
3->[A](1,1)
5->[A](2,1)
7->[A](3,1)
1->[A](4,1)
2->[A](1,2)
9->[A](2,2)
10->[A](3,2)
12->[A](4,2)
9->[A](1,3)
14->[A](2,3)
11->[A](3,3)
4->[A](4,3)
13->[A](1,4)
8->[A](2,4)
0->[A](3,4)
15->[A](4,4)
-1->X
Goto P
Lbl B
For (I,1,4,1)
For (J,1,4,1)
If [A](I,J) =0:Then
I->X
J->Y
Stop
End
If X!=-1
Stop
End
Repeat([A](1,1)=1 and [A](2,1)=2 and [A](3,1)=3 and [A](4,1)=4 and [A](1,2)=5 and [A](2,2)=6 and [A](2,3)=7 and [A](2,4)=8 and
[A](3,1)=9 and [A](3,2)=10 and [A](3,3)=11 and [A](3,4)=12 and
[A](4,1)=13 and [A](4,2)=14 and [A](4,3)=15 and [A](4,4)=0)
Repeat (K=24 or K=25 or K=26 or K=34)
getKey->K
End
If K=24 and X-1!=0:Then
[A](X-1,Y)->[A](X,Y)
0->[A](X-1,Y)
X-1->X
Goto P
End
If K=25 and Y-1!=0:Then
[A](X,Y-1)->[A](X,Y)
0->[A](X,Y-1)
Y-1->Y
Goto P
End
If K=26 and X+1!=5:Then
[A](X+1,Y)->[A](X,Y)
0->[A](X+1,Y)
X+1->X
Goto P
End
If K=34 and Y+1!=5:Then
[A](X,Y+1)->[A](X,Y)
0->[A](X,Y+1)
Y+1->Y
Goto P
End
Goto E
Lbl P
ClrHome
For (I,1,4,1)
For (J,1,4,1)
If [A](I,J) <10 and [A](I,J) !=0:Then
Output(J,I,[A](I,J))
Else
If [A](I,J) = 10
Output(J,I,"A")
If [A](I,J) = 11
Output(J,I,"B")
If [A](I,J) = 12
Output(J,I,"C")
If [A](I,J) = 13
Output(J,I,"D")
If [A](I,J) = 14
Output(J,I,"E")
If [A](I,J) = 15
Output(J,I,"F")
End
End
End
If X=-1:Then
Goto B
Else
Goto D
End
Lbl D
End
End
Lbl E
End
ClrHome
Disp "Good Work"
That was it, I hope you learned a lot during these 5 parts, and I wish you good luck with your coding. :D
Edited by Vswe, 28 August 2009 - 03:22 PM.


Sign In
Create Account


Back to top










