This is the code I have so far of my program:

```
:Delvar [A]{3,3->dim([A]
:ClrHome
:Asm(prgmALPHA // Assembly Program used for auto alpha lock
:Input "Player 1: ",Str1
:Asm(PrgmALPHA
:Input "Player 2: ",Str2
:ClrDraw
:AxesOff
:CoordOff
:PlotsOff
:FnOff
:0->Xmin
:15->Xmax
:0->Ymin
:15->Ymax
:Horizontal 5 // These lines create the tic tac toe board
:Horizontal 10
:Vertical 5
:Vertical 10
:2->A //A and B are used as row and column coords in [A] to record an 'X' or 'O'
:2->B
:0->P //Determines whose turn it is. 0=player1, 1=player2
:7.5->X // X and Y are coords of cursor on the screen
:7.5->Y
:Pt-On(X,Y,3
:Lbl A
:While 1
:getKey->K
:While K=0
:getKey->K
:End
:If K=105
:Goto B
:Pt-Off(X,Y,3
:X-5(K=24 and X>2.5)+5(K=26 and X<12.5->X // Moving the cursor around
:Y-5(K=34 and Y>2.5)+5(K=25 and Y<12.5->Y // and collision detection so
:B-(K=24 and X>2.5)+(K=26 and X<12.5->B // the cursor doesn't go off the screen
:A-(K=34 and Y>2.5)+(K=25 and Y<12.5->A
:Pt-On(X,Y,3
:End
:Lbl B
:If P=0 // If player 1
:Then
:1->[A](A,B // Store 1 into that matrix value
:Circle(X,Y,2
:Else // If player 2
:5->[A](A,B // Store 5 into that matrix value
:Line(X-2,Y+2,X+2,Y-2 // Makes an 'X' on
:Line(X+2,Y+2,X-2,Y-2 // the board
:End
:Matr►list([A],L1,L2,L3 // Convert all the rows and columns
:Matr►list([A]T,L4,L5,L6 // into lists
:[A](1,3)+[A](2,2)+[A](3,1->C // Add up the diagonals into variables
:[A](1,1)+[A](2,2)+[A](3,3->D
:If max(3={sum(L1),sum(L2),sum(L3),sum(L4),sum(L5),sum(L6),C,D // If the rows add up to 3
:Goto 1
:If max(15={sum(L1),sum(L2),sum(L3),sum(L4),sum(L5),sum(L6),C,D // If the rows add up to 15
:Goto 2
:not(P->P // Switch Players
:Goto A
:Lbl 1
:ClrHome
:Disp Str1," Wins!
:Pause
:Stop
:Lbl 2
:ClrHome
:Disp Str2," Wins!
:Pause
:Stop
```

Something is wrong with this program. When I run it, in the beginning it goes very smoothly. The cursor works fine and it draws out the shapes well. However, in certain cases when it draws out the fifth shape i get an error invalid dim and it takes me to the ':1->[A](A,B' line. Also, it is unable to end the program when 3 x's or o's are drawn in a row. It just acts as if the game is still going.

I need some suggestions on what to do and also how to optimize the code if necessary. Thanks