7
\$\begingroup\$

So here we have a little game of Tic Tac Toe I've made. It runs well on TI-83/84 calculators and it doesn't seem to have any problems. However, I'd like to eliminate some of the code that seems bulky and repetitive as well as possibly add some checks so you can't overwrite a square that's already been played and to finish the game when a row of 3 has been made. If anyone could help me I'd appreciate it!

:ClrHome
:ClrDraw
:1->X
:0->I
:0->O
:Horizontal 9
:Horizontal 3
:Horizontal -3
:Horizontal -9
:Vertical 9
:Vertical 3
:Vertical -3
:Vertical -9
:Disp "WHO FIRST X OR O"
:Input F
:Disp "USE 1 THROUGH 9"
:Lbl 1
:Input M
:fPart(M/3)3-2->H
:If H=-2
:1->H
:H*6->H
:-6->V
:If M>3
:0->V
:If M>6
:6->V
:If F
:Then
:Line(H-2,V-2,H+2,V+2)
:Line(H-2,V+2,H+2,V-2)
:Else
:Circle(H,V,2)
:End
:I+1->I
:abs(F-1)->F
:If I=9
:Goto 2
:Pause
:Goto 1
:Lbl 2
\$\endgroup\$
3
\$\begingroup\$

Disclaimer: I only have a TI-85, whose commands are slightly different from the TI-83 and TI-84.

User Experience

  • At first, I was puzzled by the numbering of the squares. Then I realized that squares 1 to 9 corresponded nicely to the way the calculator number pad is laid out. Nice!
  • You could probably do without the lines at ±9, as a tic-tac-toe board is conventionally drawn as #, with no borders.
  • There's not much point in asking who goes first, in my opinion.
  • As you noted, you fail to check whether a square is already occupied. A nine-element list could easily be used to perform that check.

Code

  • Variables X and O are defined but never used.
  • I don't know why you chose to name your variables F and M. I would have picked P for "player" and S for "square".
  • The two-argument form of the Input command can also display the prompt.
  • fPart(M/3)3-2→H didn't work for me. Due to the way precedence of operations works on the TI-85, it is evaluated at fPart((M/3)3)-2→H. Rewriting it as 3fPart(M/3)-2→H fixes it.
  • The code to map M to H and V is excessively complicated. Each assignment should be done in one line:

    6(mod(M-1,3)-1)→H
    6 int((M-4)/3)→V
    

    The TI-84 has remainder() instead of mod().
    The TI-83 has neither remainder() nor mod(), and must use 6(3fPart((M-1)/3)-1)→H.
    I believe the latter is works for all three models.

  • Your use of Goto is unjustified. I should be initialized, tested, and incremented using a For( loop.
  • To toggle F between 0 and 1, just do 1-F→F. You don't need an abs function.
  • I'm not sure that ClrDraw adequately resets the drawing context. On the TI-85, I need ClDrw, ZStd (to set the bounds to the standard zoom), and AxesOff (to hide the axes).

Rewrite for the TI-85 dialect

I've noted what I believe the equivalent TI-83 / TI-84 commands are, based on reading the Guidebooks.

:ClLCD                       Equivalent to ClrHome?
:ClDrw                       Equivalent to ClrDraw?
:ZStd                        Equivalent to ZStandard?
:AxesOff
:DrawF -3                    Equivalent to Horizontal?
:DrawF 3
:Vert -3                     Equivalent to Vertical?
:Vert 3
:1→P
:For(I,1,9)
:Input "Square (1-9)? ",S
:6(mod(S-1,3)-1)→H           See portability note above.
:6 int((S-4)/3)→V
:If P
:Then
:Line(H-2,V-2,H+2,V+2)
:Line(H-2,V+2,H+2,V-2)
:Else
:Circl(H,V,2)                Equivalent to Circle?
:End
:Pause
:1-P→P
:End
\$\endgroup\$
  • \$\begingroup\$ Hey, thanks for your answer. Since then I've moved on to For loops and proper one-liners. Just wanted to mention that the idea behind assigning X and O was that the user could input X or O as who goes first, and it would either evaluate to true (X) or false (O) in the If F line. \$\endgroup\$ – Timtech Aug 3 '17 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.