Fun with the TI-89 |
||||||||||||||
Or, how I spent my spare time in Calculus class. Ever wonder what the TI-89's Program Editor did exactly? Hidden within the deapths of the manual was the god-sent Appendix A. Here resided a plethora of functions and instructions for use in the Program Editor. Basically, anything you could do regularly on the TI-89--and many things you could not--could be performed with the Program Editor.
Using the Editor is simple enough. Press APPS, then 7 (to access the Program Editor menu), and then 3 (to create a New program). Give your program a name (under Variable) and you're ready to start coding. The language the TI-89 uses reminds me of a Zorse. It's like a combination of C and BASIC, yet it somehow became even less C-like and less BASIC-ish in the process. It's pretty much designed to do just what you can already do manually but in a macro-like fashion. (But that doesn't mean you can't make games on it ;). Right! Now, let's dive right into programming the TI-89 with the pinnacle of strategy games, yes, the one and only: rock, paper, scissors. Just type the code for now. I'll explain it later. If you run into a character that is not on the keypad goto 2nd then the "+" key to access special characters. And it'll save you time if you utilize the CATALOG key to find and input most of the functions and the COPY and PASTE actions.
rps()
Prgm Lbl top Local x,y,x,t Define x=rand(3) ClrIO Disp "Rock (x)" Disp "Paper (y)" Disp "or Scissors (z)?" Define y=getKey() While y=0 or y≠120 and y≠121 and y≠122 Define y=getKey() EndWhile ClrIO Define t=0 While t<25 Define t=t+1 EndWhile Disp "Rock!" Define t=0 While t<25 Define t=t+1 EndWhile Disp "Paper!" Define t=0 While t<25 Define t=t+1 EndWhile Disp "Scissors!" Define t=0 While t<25 Define t=t+1 EndWhile ClrIO If x=1 Then Define z="Rock" ElseIf x=2 Then Define z="Paper" ElseIf x=3 Then Define z="Scissors" Endif Disp "Com: "&z If y=120 Then Define z="Rock" ElseIf y=121 Then Define z="Paper" ElseIf y=122 Then Define z="Scissors" Endif Disp "You: "&z If x=1 and y=120 Then Disp "Draw!" ElseIf x=1 and y=121 Then Disp "You win!" ElseIf x=1 and y=122 Then Disp "Com wins!" ElseIf x=2 and y=120 Then Disp "Com wins!" ElseIf x=2 and y=121 Then Disp "Draw!" ElseIf x=2 and y=122 Then Disp "You win!" ElseIf x=3 and y=120 Then Disp "You win!" ElseIf x=3 and y=121 Then Disp "Com wins!" ElseIf x=3 and y=122 Then Disp "Draw!" EndIf Disp "" Disp "Play again (ENTER)?" Define y=getKey() While y=0 Define y=getKey() EndWhile If y=13 Then Goto top Else ClrIO DispHome Endif EndPrgm Now run the program by pressing HOME, CATALOG, F4, selecting rps( ........main, closing the parenthesis, and pressing ENTER. Nifty, eh?
(By the way, if you own a TI-89 calculator you can download a TI-89 ROM image and then get a TI-89 emulator and start capturing your own awesome screenshots). Basically, the program starts by defining some variables, x, y, z, and t. Why these and not some other random variables? Laziness, of course. You don't have to press alpha to access x, y, z, and t! It is always a good idea to list the variables you are using in your program under Local before you actually use them. This prevents the program from modifying the actual variables in memory. rand(3) generates a random number between 1 and 3, inclusive (this is the number we will use for the computer's choice. ClrIO clears the input/output screen where the program runs--very useful for preventing your program from looking lika a command-line user interface :). Moving right along, Disp is used to send a line of output, usually a string, sometimes a variable or number, and sometimes a combination of both by joining them together with an ampersand (&). The While loop waits for the user to input either X (rock), Y (paper), or Z (scissors). Next, t is used to keep track of time. By setting t to 0 then staying in a While...EndWhile loop (a While...EndWhile loop executes a block of code until a certain condition following the While is met) while incrementing t, you are able to create a delay. The four delays in a row are just to build suspense in the game and simulate the real-life experience. Finally, some Disp's and If...Then statements show the reader who did what and who (if anyone) won. Oh, and if you've been wondering about the Lbl and Goto statements, Lbl name is used to mark a certain position in a program, and Goto name simply jumps to the corresponding Lbl position. A handy way of getting around, but as you'll see in the next game, a loop can get the job done as well. A nifty little game indeed, but how fun is playing games against a computer? So cold, so lifeless. What you need is a classic two-player game (and a person to play it with...but minor details). Onward to Connect Four! But first (yes, I must interrupt) a utility so amazing, so earth-shattering, so timesaving that it is bound to aid in your finding-the-integers-that-represent-keys-on-the-TI-89 endeavors: a program that displays the number of a pressed key. Try it out:
keyfind()
Prgm ClrIO Local x Define x=getKey() While x=0 Define x=getKey() EndWhile Disp x EndPrgm Yes, I know...you can thank me later. Onward once again to Connect Four! Don't think...just program. Explanations will come later.
connect4()
Prgm Local h,w,x,t,b,c,key,m,n Define h=6 Define w=7 Define x=1 Define t=1 Define b=[[0,0,0,0,0,0,0][0,0,0,0,0,0,0][0,0,0,0,0,0,0][0,0,0,0,0,0,0][0,0,0,0,0,0,0] [0,0,0,0,0,0,0]] Loop ClrIO Define c=" |" For n,1,w,1 If n=x Then Define c=c&"↓|" ElseIf b[1,n]=0 Then Define c=c&" |" ElseIf b[1,n]=1 Then Define c=c&"○|" ElseIf b[1,n]=2 Then Define c=c&"◆|" EndIf EndFor Define c=c&" →Turn=P"&string(t) Disp c For m,2,h,1 Define c=" |" For n,1,w,1 If b[m,n]=0 Then Define c=c&" |" ElseIf b[m,n]=1 Then Define c=c&"○|" ElseIf b[m,n]=2 Then Define c=c&"◆|" EndIf EndFor If m=h Then Define c=c&" P1=◆ P2=○" EndIf Disp c EndFor Define key=getKey() While key=0 Define key=getKey() EndWhile If key=337 and x>1 Then Define x=x-1 EndIf If key=340 and x<w Then Define x=x+1 EndIf If key=264 Then ClrIO DispHome Stop EndIf If key=13 Then If t=1 Then Define t=2 Else Define t=1 EndIf For n,0,h-1,1 If b[h-n,x]=0 Then Define b[h-n,x]=t Exit EndIf EndFor EndIf EndLoop EndPrgm
Hefty bugger, ain't she? Ah, but it's well-worth the excruciating pain of locating tiny, dark purple numbers on a black background (the folks at Texas Instruments did a great job solving this problem with the TI-89's sucessor, the TI-89 Titanium, by placing mint green letters on a silver/grey background--go figure). Anyway, as you have probably already guessed, Player 1 is the "◆" symbol and Player 2 is the "○" symbol; they alternate back and forth by changing the turn variable, t. The board is drawn one "slot" at a time. One thing new in this program is the use of arrays to hold data. Arrays for the TI-89 are defined as follows: name[[data_1.1,data_1.2,...,data_1.n][data_2.1,data_2.2,...,data_2.n]...[data_n.1,data_n.2,...,data_n.n]]. Think of arrays as arranged in rows and columns like this:
[[row1_col1,row1_col2,row1_col3] When you want to retreive data from an array you just say "name[row,col] to access the data at that location." Now, you'll understand why b, the board, looks and is recalled differently from the others. Another new thing is the For...EndFor statement which looks like this: For variable,lower_bound,upper_bound,step...EndFor. It just cycles through the block of code it encloses until variable equals upper_bound, starting with variable set to lower_bound. Step may by left out, but represents how much to increment variable each cycle, by default it is 1.h is the height of the board, and w is the width. c is the "slot" that is drawn. I'm not sure why I called it "c" and not "s"...maybe because "c" was so close to the other variables. n and m are just numbers used as variables in the program's For...EndFor loops. Finally, note that instead of using Lbl and Goto statements, the entire program in enclosed in a Loop...EndLoop statement which can be exited with the...need I even say it?...Exit function! Well, that's all I have to offer as far as the TI-89 is concerned. I leave you, budding TI-89 programmer, with this: add the fact that the TI-89 is able to transmit data through its I/O port plus a friend with another TI-89 plus a 7.5 foot I/O cable, and oh, the possibilities are endless. Cheers. |