Jump to content

TI 82 calculator tutorials Part 5 - Creating a Game

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
In this series of tutorials I will teach you how to program in a TI 82 calculator from the very basics. I use a TI 82 calculator and therefor I also use it for my examples but many different Texas Instruments calculator has nearly the same syntax so you don't have to use it with a TI 82.
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:


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.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
ewww, "goto". Have you created a large game yet?

+rep

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I have created Pong, Snake and a text based RPG game, but I didn't had time to finish the last one and I don't have the code now.

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
What's wrong with goto Jordan :P

I love using gotos they confuse people! :D

#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
lol, exactly. If I ever get a piece of PHP 6 code from you with GOTO I'll make you pay me! :)

#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Does 5.3 have it? I should write that bot in 5.3 valid code :P with a ton of gotos!

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I don't believe it is available until 6, thank god. You know, that is a good discussion topic for +5 points "Why is Goto going to be available in PHP 6". You better hope relapse doesn't see this.

If you did write it using gotos I'd charge you $1 per submission!

#8
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts

Jordan said:

I don't believe it is available until 6, thank god. You know, that is a good discussion topic for +5 points "Why is Goto going to be available in PHP 6". You better hope relapse doesn't see this.

If you did write it using gotos I'd charge you $1 per submission!

I think I had another post somewhere, I had 5 points for it but they disappeared :P

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Which was it? The entire point system erased itself for some unknown reason. I'll restore it if so.

#10
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
It was the one about "More languages or deeper understanding" in general programming.

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Well done. +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog