+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: TI 82 calculator tutorials Part 5 - Creating a Game

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    TI 82 calculator tutorials Part 5 - Creating a Game

    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.



    Code:
    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.



    Code:
    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.




    Code:
    -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.




    Code:
    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.





    Code:
    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.




    Code:
    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.





    Code:
    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.





    Code:
    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.


    Code:
    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.




    Code:
    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).




    Code:
    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.




    Code:
    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:



    Code:
    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.
    Last edited by Vswe; 08-28-2009 at 04:22 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

    ewww, "goto". Have you created a large game yet?

    +rep

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

    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.

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

    What's wrong with goto Jordan

    I love using gotos they confuse people!

  6. #5
    Jordan Guest

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

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

  7. #6
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

    Does 5.3 have it? I should write that bot in 5.3 valid code with a ton of gotos!

  8. #7
    Jordan Guest

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

    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!

  9. #8
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

    Quote Originally Posted by Jordan View Post
    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

  10. #9
    Jordan Guest

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

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

  11. #10
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: TI 82 calculator tutorials Part 5 - Creating a Game

    It was the one about "More languages or deeper understanding" in general programming.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 08-28-2009, 07:32 PM
  2. Replies: 2
    Last Post: 08-28-2009, 07:30 PM
  3. Replies: 2
    Last Post: 08-28-2009, 07:28 PM
  4. Replies: 6
    Last Post: 08-28-2009, 07:27 PM
  5. Part I: Writing the basics - Calculator
    By Brandon W in forum PHP Tutorials
    Replies: 47
    Last Post: 10-11-2008, 02:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts