Do you like tic-tac-toe? If yes, would you like it on your calculator?
Yes
 60%  [ 6 ]
No
 40%  [ 4 ]
Total Votes : 10

I literally(at 21 May 2015 05:56:25 am, to be exact) just looked to see if we had any tic-tac-toe games on Cemetech. Since the only one is for the Casio Prizm, and a few others, I decided to make one for the CSE (and maybe one for the SE).
My goals are as follows:
1. To be able to play tic-tac-toe
2.To have it less than 1 kb in size(probaby not going to happen)
3. To have a somewhat good AI
4. Have the ability to save a game and play it later
5. To have a board for normal tic-tac-toe, a board for super tic-tac-toe(5x5) and to have a board for the ultimate tic-tac-toe(9x9)
6. And, of course, to have a two-player mode.
7. I might put in a computer vs. computer mode. Just for laughs.
8. And I might decide to write it in Hybrid BASIC.

What do you think?

Will post code later.
I think this is a great idea! I tried to do something like this back in 2011, but never finished it. Again, as with your Flashcard Program, I'll be happy to translate it to monochrome.
Good deal then.. NoahK! If you decide to do this, then I will have you on the upload page as a coauthor.
Well, I can't say I don't see the file on cemetech, (and more for the monochrome) but it isn't that detailed and definitely could be improved on.
You could also make a perfect AI that never loses, but I guess there's no fun in that.

Now, I have qualms about 5x5 tic tac toe, or really anything higher than 3x3. Here's a good link to how many tic tac toe squares you need for winning for 4 in a row(5x6) and 5 in a row(15x15).
Here's some code:

Code:
ClrHome
Menu("TIC-TAC-TOE","PLAY",1,"QUIT",2
Lbl 1
ClrDraw:0->Xmin:60->Xmax:0->Ymin:60->Ymax
For(A,0,60,20
Line(A,0,A,60
Line(0,A,60,A
End
End
For(B,10,50,20
For(A,10,50,20
Pt-On(A,B,2
End
End
1->P
30->A:30->B
Repeat theta=45
getKey->theta
A+(20(theta=26))-(20(theta=24))->A
B+(20(theta=25))-(20(theta=34))->B
Pt-Change(A,B,10
If theta=105 and P=1:Then
Circle(A,B,10,10
not(P)->P:0->theta
End
If theta=105 and P=0:Then
Line(A-10,B+10,A+10,B-10,11
Line(A+10,B+10,A-10,B-10,11
not(P)->P:0->theta
End
End
ClrHome
Menu("PLAY AGAIN?","YES",1,"NO",2
Lbl 2
ClrHome:Stop
End

Any ideas on how I should test when/if a player gets three in a row?
Well, notice that if you make the best AI, you can never win, whatever you will do. So a computer vs computer will always result in a tie, unless they have a bug or so. That is why I will stimulate you to make not the best AI, that's not fair for human beings. Smile
You'd want to use a matrix to do that.
I have taken your posted code and made a monochrome version on my calc. It does everything your code does, but it takes into account pixels and squareness, as those are important things on a monochrome calc. I have it also writing board info to Matrix [A], so a win-check can be easily implemented.
Here's the code so far:

Code:
ClrHome
Menu("TIC-TAC-TOE","PLAY",1,"QUIT",2
Lbl 1
ClrDraw:0->Xmin:60->Xmax:0->Ymin:60->Ymax
For(A,0,60,20
Line(A,0,A,60
Line(0,A,60,A
End
End
1->P
30->A:30->B:0->Z:3->dim(L1):Fill(0,L1):L1->L2:L2->L3:L3->L4:L4->L5:L5->L6:0->W
Repeat theta=45 or Z=9
If P=1:Text(0,0,"O
If P=2:Text(0,0,"X
getKey->theta
A+(20(theta=26))-(20(theta=24))->A
B+(20(theta=25))-(20(theta=34))->B
Pt-On(A,B
Pt-Off(A-20,B
Pt-Off(A+20,B
Pt-Off(A,B-20
Pt-Off(A,B+20
Pt-Off(A-20,B-20
Pt-Off(A+20,B-20
Pt-Off(A+20,B+20
Pt-Off(A-20,B+20
If theta=105 and P=1:Then
Circle(A,B,10,10
If A=10 and B=50:Then
1->L1(1):1->L4(1):K+1->K
End
If A=10 and B=30:Then
1->L5(1):1->L1(2)
End
If A=10 and B=10:Then
1->L1(3):1->L6(1):L+1->L
End
If A=30 and B=50:Then
1->L2(1):1->L4(2)
End
If A=30 and B=50:Then
1->L2(1):1->L4(2)
End
If A=30 and B=30:Then
1->L2(2):1->L5(2):K+1->K:L+1->L
End
If A=30 and B=10:Then
1->L2(3):1->L6(2)
End
If A=50 and B=50:Then
1->L3(2):1->L5(3)
End
If A=50 and B=10:Then
1->L3(3):1->L6(3):K+1->K
End
If sum(L1)=3 or sum(L2)=3 or sum(L3)=3 or sum(L4)=3 or sum(L5)=3 or sum(L6)=3 or K=3 or L=3
Then:9->Z:"PLAYER 1 WINS"->Str1:1->W
Else:not(P)->P:0->theta:Z+1->Z
End
If theta=105 and P=0:Then
Line(A-10,B+10,A+10,B-10,11
Line(A+10,B+10,A-10,B-10,11
If A=10 and B=50:Then
4->L1(1):4->L4(1):K+4->K
End
If A=10 and B=30:Then
4->L5(1):4->L1(2)
End
If A=10 and B=10:Then
4->L1(3):4->L6(1):L+4->L
End
If A=30 and B=50:Then
4->L2(1):4->L4(2)
End
If A=30 and B=50:Then
4->L2(1):4->L4(2)
End
If A=30 and B=30:Then
4->L2(2):4->L5(2):K+4->K:L+4->L
End
If A=30 and B=10:Then
4->L2(3):4->L6(2)
End
If A=50 and B=50:Then
4->L3(2):4->L5(3)
End
If A=50 and B=10:Then
4->L3(3):4->L6(3):K+4->K
End
If sum(L1)=12 or sum(L2)=12 or sum(L3)=12 or sum(L4)=12 or sum(L5)=12 or sum(L6)=12 or K=12 or L=12
Then:9->Z:"PLAYER 2 WINS"->Str1:1->W
Else:not(P)->P:0->theta
End
End
End
If W=1:Then
ClrHome:Pause Str1
End
ClrHome
Menu("PLAY AGAIN?","YES",1,"NO",2
Lbl 2
ClrHome:Stop
End


And that's all that I have. Unfortunately...... it amounts to 1418 bytes.

Can any of you guys help me optimize this and/or help me make a matrix alternative?
caleb1997 wrote:
Here's some code:

Code:
ClrHome
Menu("TIC-TAC-TOE","PLAY",1,"QUIT",2
Lbl 1
ClrDraw:0->Xmin:60->Xmax:0->Ymin:60->Ymax
For(A,0,60,20
Line(A,0,A,60
Line(0,A,60,A
End
End
1->P
30->A:30->B:0->Z:3->dim(L1):Fill(0,L1):L1->L2:L2->L3:L3->L4:L4->L5:L5->L6:0->W
Repeat theta=45 or Z=9
If P=1:Text(0,0,"O
If P=2:Text(0,0,"X
getKey->theta
A+(20(theta=26))-(20(theta=24))->A
B+(20(theta=25))-(20(theta=34))->B
Pt-On(A,B
Pt-Off(A-20,B
Pt-Off(A+20,B
Pt-Off(A,B-20
Pt-Off(A,B+20
Pt-Off(A-20,B-20
Pt-Off(A+20,B-20
Pt-Off(A+20,B+20
Pt-Off(A-20,B+20
If theta=105 and P=1:Then
Circle(A,B,10,10
If A=10 and B=50:Then
1->L1(1):1->L4(1):K+1->K
End
If A=10 and B=30:Then
1->L5(1):1->L1(2)
End
If A=10 and B=10:Then
1->L1(3):1->L6(1):L+1->L
End
If A=30 and B=50:Then
1->L2(1):1->L4(2)
End
If A=30 and B=50:Then
1->L2(1):1->L4(2)
End
If A=30 and B=30:Then
1->L2(2):1->L5(2):K+1->K:L+1->L
End
If A=30 and B=10:Then
1->L2(3):1->L6(2)
End
If A=50 and B=50:Then
1->L3(2):1->L5(3)
End
If A=50 and B=10:Then
1->L3(3):1->L6(3):K+1->K
End
If sum(L1)=3 or sum(L2)=3 or sum(L3)=3 or sum(L4)=3 or sum(L5)=3 or sum(L6)=3 or K=3 or L=3
Then:9->Z:"PLAYER 1 WINS"->Str1:1->W
Else:not(P)->P:0->theta:Z+1->Z
End
If theta=105 and P=0:Then
Line(A-10,B+10,A+10,B-10,11
Line(A+10,B+10,A-10,B-10,11
If A=10 and B=50:Then
4->L1(1):4->L4(1):K+4->K
End
If A=10 and B=30:Then
4->L5(1):4->L1(2)
End
If A=10 and B=10:Then
4->L1(3):4->L6(1):L+4->L
End
If A=30 and B=50:Then
4->L2(1):4->L4(2)
End
If A=30 and B=50:Then
4->L2(1):4->L4(2)
End
If A=30 and B=30:Then
4->L2(2):4->L5(2):K+4->K:L+4->L
End
If A=30 and B=10:Then
4->L2(3):4->L6(2)
End
If A=50 and B=50:Then
4->L3(2):4->L5(3)
End
If A=50 and B=10:Then
4->L3(3):4->L6(3):K+4->K
End
If sum(L1)=12 or sum(L2)=12 or sum(L3)=12 or sum(L4)=12 or sum(L5)=12 or sum(L6)=12 or K=12 or L=12
Then:9->Z:"PLAYER 2 WINS"->Str1:1->W
Else:not(P)->P:0->theta
End
End
End
If W=1:Then
ClrHome:Pause Str1
End
ClrHome
Menu("PLAY AGAIN?","YES",1,"NO",2
Lbl 2
ClrHome:Stop
End


And that's all that I have. Unfortunately...... it amounts to 1418 bytes.

Can any of you guys help me optimize this and/or help me make a matrix alternative?


Sure, I worked a little but on this last night, so here's the code I've got. It uses the matrix to store and check for a winning move.:

SourceCoder 3 (TCTACTO) wrote:
:~33->Xmin
:61->Xmax
:~1->Ymin
:61->Ymax
:Lbl 0
:ClrHome
:{3,3->dim([A]
:Fill(0,[A]
:Menu("TIC-TAC-TOE","PLAY",1,"QUIT",2
:Lbl 1
:ClrDraw
:For(A,0,60,20
:Line(A,0,A,60
:Line(0,A,60,A
:End
:2->A
:2->B
:1->P
:0->W
:Pt-On(30,30
:Repeat W or G=45
:Repeat Ans
:getKey
:End
:Ans->G
:If P=1
:Text(0,0,"O
:If P=2
:Text(0,0,"X
:Pt-Off(20A-10,20B-10
:A+(G=26 and A<3)-(G=24 and A>1->A
:B+(G=25 and B<3)-(G=34 and B>1->B
:Pt-On(20A-10,20B-10
:If G=105 and not([A](A,B
:Then
:If P=1
:Then
:Circle(20A-10,20B-10,7
:Else
:Line(20A-17,20B-17,20A-3,20B-3
:Line(20A-17,20B-3,20A-3,20B-17
:End
Razz->[A](A,B
:1+not(P-1->P
:End
:
:"WIN CHECK HERE"
:
:End
:If W
:Then
:ClrDraw
:Text(~1,0,0," PLAYER ",W," WINS!
:Pause
:Goto 0
:Lbl 2
:ClrHome
:Stop
Well, what you can do is, not to select a box, but to gave each box a number, like this:
[1][2][3]
[4][5][6]
[7][8][9]
where you do not use a matrix, but a list of 9 elements. For the keycodes, you could store that number into a value and check if the index of the list already stores a X or O. I hope you understand me, later I'll better explain it Laughing

EDIT:
Here's my better explanation. The Tic-Tac-Toe game has 3 rows and 3 columns, 9 boxes in total. I numbered them from 1 upto 9, as you can see above. Now, if I press a key, it finds the corresponding number and save it into a variable. Like this: if I press the number "4", the keycode is 82, then an algorithm would store 4 to the variable. This can be done like this:

Code:
If sum(K={72,73,74,82,83,84,92,83,84
10fPart(K/10)+26-3int(K/10->A

which stores here (if K=82) 4 to A
First you make a list like {0,0,0,0,0,0,0,0,0 with 9 times 0, this represents your 9 boxes.

So, A has the corresponding number of the key. Lets set P to the player, 1 for player1 and 2 for player2.
Then you would call:

Code:
P->L1(A


Now the most interesting part, how to check if a player has won Smile.
First, lets make a list of all the combinations where you can have a solution:
- 1,2,3 (horizontal)
- 4,5,6
- 7,8,9
- 1,4,7 (vertical)
- 2,5,8
- 3,6,9
- 1,5,9 (diagonal)
- 3,5,7
So, now I hope the fastest way to check if a player have 3 in a row:

Code:
For(A,1,2         // Players
For(B,1,3
If L1(3B)=A and L1(3B)=L1(3B+1)=L1(3B+2
Goto [won]
If L1(B)=A and L1(B)=L1(B+3)=L1(B+6
Goto [won]
// You could combine these 2
End
If L1(1)=A and L1(1)=L1(5)=L1(9
Goto [won]
If L1(3)=A and L1(3)=L1(5)=L1(7
Goto [won]
End

I hope this helps Smile
Now that the game finally works, I want to start working on the AI.
To do list:
1.First, figure out how to make it so that a X and a O can't go in the same spot
2. Figure out how the AI should work.
a. Rand?
3. Make it decently good, but not too powerful.
4. Lastly, make a computer vs. computer setting. Just for laughs.


Any ideas on where I should start?
I've got the x and o conflict figured out, it's already in the code I posted. It checks to see if, in the matrix, that spot had been filled already, and if it is, it ignores the keypress.

Now for the ai...
I have drawn out all of the possible combinations and which next move would yield a win or tie. I could program an ai to never loose, but that would be frustrating for the player. We could also go the rand route, but I feel that would be too easy. So those two in themselves could be the "easy" and "impossible" levels. A moderate would be to, as PT_ suggested in sax, have the ai do random moves until it could block you in one move (like you have 2 in a row). A hard could be to have the clac do the first move on a best-move and then alternate b/t random and best-move either randomly or by a percentage or every other move or 2 bests, one random.

My code so far for the monochrome (and will run as written on a C, just having to change where the text outputs pixel-wise) already has collision-detection, win-detection, and keeps score, and it's already 750bytes (note the code I have on my calc doesn't exactly correspond to the one I posted, but the one I posted does have the collision detection [your issue 1 in bold])
Well, NoahK and I have an idea to make it a kind of 3D game, but not the 3D in real life. We have height, width and time. I would try to implement time, so that you has, lets say, 5 seconds to do your move. There are some possibilities.

1:
Code:
startTmr->T
Repeat Ans
getKey
End
Ans->K
checkTmr(T
If Ans<6
[do_move]
Else
[do_random_move]
End


2:

Code:
startTmr->T
Repeat 5>checkTmr(T) and Ans
getKey
End
Ans->K
checkTmr(T
If Ans<6
[do_move]
Else
[do_random_move]
End

If possible much more, but I think this one are the best one.



EDIT: found this elegant and so beatiful program!!
SourceCoder 3 (TICTACTO) wrote:
:DelVar A
:0identity(3->[A]
:1-2int(2rand->B
:"+++
:ClrHome
:Disp Ans,Ans,Ans
:Repeat A=9
: Repeat Ans
: .1getKey->K
: End
: If 2Ans=9
: Goto Q
: iPart(Ans)-6->C
: 10fPart(K)-1
: If max(2<=abs({C,Ans}-2
: End
: If [A](C,Ans
: End
: Output(C,Ans,sub("X O",B+2,1
: B->[A](C,Ans
: For(C,0,1
: [A]^^T->[A]
: For(D,1,3
: Matr>list([A],D,T
: If min(|LT=B
: Goto Q
: End
: If min(B=seq([A](abs(4C-X),X),X,1,3
: Goto Q
: End
: ~B->B
: A+1->A
:End
:Lbl Q
:"TIE!
:If A!=9
: sub("X O",B+2,1)+" WINS!
:If 2K!=9
: Pause Ans
:ClrHome
:"

You should implement it!!!
PT_ wrote:
Well, NoahK and I have an idea to make it a kind of 3D game, but not the 3D in real life. We have height, width and time. I would try to implement time, so that you has, lets say, 5 seconds to do your move. There are some possibilities.

-code-

EDIT: found this elegant and so beatiful program!!

-some really optimized code-

You should implement it!!!


The timing routines are great and would add to the gameplay, maybe a countdown in the corner telling you how much time you have left to place a move. But the timer should be an option, imho, just as would selecting a difficulty. So at the beginning of the program, the user would select the mode; p-v-p, p-v-c, or c-v-c*; and the difficulty of the comp (if applicable), and would select if a timer would be in use.

I am still working on the AI code, it will be posted once I get it to a presentable state.

A quick note, caleb, we will continue writing the program based on a matrix. I would love to see a copy of your personal current code and help you modify it to do what you want it to, as the plan is to help you develop your code, not have us write it for you.
I do have the code, and it will be posted in full by 5/28/15. Sorry for the wait guys! (I just don't have 24/7 access to it.)
I have it a complete, playable program. It uses some of your base code, but I have it using Matrices and it has an AI that does "good-move". It plays randomly until it can either complete a tictac or stop you from playing a tictac. The user is 0, and goes first, the AI makes the second move. Here is the code!


Code:
:DCS6
:DSS6
"0810081008100810FFFF081008500B100D500B500810FFFF0810081008100810"
SetUpEditor
AxesOff
0->O
0->N
Lbl 0
ClrHome
{3,3->dim([A]
Fill(0,[A]
Menu("Tic-Tac-Toe AI","Play",1,"Quit",2
Lbl 1
ClrDraw
~33->Xmin
61->Ymax
~1->Ymin
61->Xmax
For(A,0,60,20
Line(A,0,A,60
Line(0,A,60,A
End
Text(20,0,"GAMES WON"
Text(26,0,"O: ",O
Text(32,0,"X: ",N
1->P
2->A
2->B
0->W
0->Q
Repeat W or theta=45
If B>3
3->B
Text(0,0,"O'S TURN"
Pt-On(20B-10,20B-10
Repeat theta
getKey->theta
End
Pt-Off(20A-10,20B-10
A+(theta=26 and A<3)-(theta=24 and A>1->A
B+(theta=25 and B<3)-(theta=34 and B>1->B
Pt-On(20A-10,20B-10
If theta=105 and not([A](A,B)
Then
Q+1->Q
Pt-Off(20A-10,20B-10
Circle(20A-10,20B-10,7
1->[A](A,B

//"Win-detection start"

0->D
0->E
0->W
For(C,1,3
[A](1,C)+[A](2,C)+[A](3,C->D
[A](C,1)+[A](C,2)+[A](C,3->E
If D=3 or E=3
1->W
If D=12 or E=12
2->W
End
[A](1,1)+[A](2,2)+[A](3,3->D
[A](3,1)+[A](2,2)+[A](1,3->E
If D=3 or E=3
1->W
If D=12 or E=12
2->W

//"Win-detection end"

If Q<9 and not(W
Then

//"AI start"


Text(0,0,"X'S TURN"
Q+1->Q
ClrList L1
ClrList L2
0->T
4->U
For(S,1,3
If [A](S,1)=[A](S,2) and [A](S,1)not([A](S,3
Then
S->L1(1+dim(L1
3->L2(1+dim(L2
If [A](S,2)=U
dim(L1->T
End
If [A](S,1)=[A](S,3) and [A](S,1)not([A](S,2
Then
S->L1(1+dim(L1
2->L2(1+dim(L2
If [A](S,3)=U
dim(L1->T
End
If [A](S,2)=[A](S,3) and [A](S,2)not([A](S,1
Then
S->L1(1+dim(L1
1->L2(1+dim(L2
If [A](S,3)=U
dim(L1->T
End
If [A](1,S)=[A](2,S) and [A](1,S)not([A](3,S
Then
3->L1(1+dim(L1
S->L2(1+dim(L2
If [A](2,S)=U
dim(L1->T
End
If [A](1,S)=[A](3,S) and [A](1,S)not([A](2,S
Then
2->L1(1+dim(L1
S->L2(1+dim(L2
If [A](3,S)=U
dim(L1->T
End
If [A](2,S)=[A](3,S) and [A](2,S)not([A](1,S
Then
1->L1(1+dim(L1
S->L2(1+dim(L2
If [A](3,S)=U
dim(L1->T
End
End
If [A](1,1)=[A](2,2) and [A](1,1)not([A](3,3
Then
3->L1(1+dim(L1
3->L2(1+dim(L2
If [A](2,2)=U
dim(L1->T
End
If [A](1,1)=[A](3,3) and [A](1,1)not([A](2,2
Then
2->L1(1+dim(L1
2->L2(1+dim(L2
If [A](3,3)=U
dim(L1->T
End
If [A](3,3)=[A](2,2) and [A](3,3)not([A](1,1
Then
1->L1(1+dim(L1
1->L2(1+dim(L2
If [A](2,2)=U
dim(L1->T
End
If [A](3,1)=[A](2,2) and [A](3,1)not([A](1,3
Then
1->L1(1+dim(L1
3->L2(1+dim(L2
If [A](2,2)=U
dim(L1->T
End
If [A](3,1)=[A](1,3) and [A](3,1)not([A](2,2
Then
2->L1(1+dim(L1
2->L2(1+dim(L2
If [A](1,3)=U
dim(L1->T
End
If [A](1,3)=[A](2,2) and [A](1,3)not([A](3,1
Then
3->L1(1+dim(L1
1->L2(1+dim(L2
If [A](2,2)=U
dim(L1->T
End
If dim(L1
Then
randInt(1,dim(L1->theta
L1(theta->I
L2(theta->J
If T
Then
L1(T->I
L2(T->J
End
Else
Repeat not([A](I,J
randInt(1,3->I
randInt(1,3->J
End
End

//"AI end"

4->[A](I,J
Line(20I-3,20J-3,20I-17,20J-17
Line(20I-3,20J-17,20I-17,20J-3
Pt-Off(20I-10,20J-10
End
End

//"Win-detection start"

0->D
0->E
0->W
For(C,1,3
[A](1,C)+[A](2,C)+[A](3,C->D
[A](C,1)+[A](C,2)+[A](C,3->E
If D=3 or E=3
1->W
If D=12 or E=12
2->W
End
[A](1,1)+[A](2,2)+[A](3,3->D
[A](3,1)+[A](2,2)+[A](1,3->E
If D=3 or E=3
1->W
If D=12 or E=12
2->W

//"Win-detection end"

If Q=9 and not(W
45->theta
End
If W
Then
O+(W=1->O
N+(W=2->N
Text(0,0,sub("OX",W,1)," WINS!         "
Text(26,7,O
Text(32,7,N
Pause
Else
Text(0,0,"TIE!             "
Pause
End
Goto 0
Lbl 2
ClrHome
Stop


It can more than likely be optimized in speed and size, it probably deserves work on program flow and memory management. But it's there and I works.
My code(which also works as well)

Code:
DelVar Str1:{3,3}->dim([A]:0->Z:1->P
Menu(TIC-TAC-TOE","PLAY",1,"QUIT",2
Lbl 1
ClrDraw:0->Xmin:60->Xmax:0->Ymin:60->Ymax
For(A,0,60,20
Line(A,0,A,60
Line(0,A,60,A
End
1->P
30->A:30->B:0->Z:3->dim(L1):Fill(0,L1):L1->L2:L2->L3:L3->L4:L4->L5:L5->L6:0->W
Repeat theta=45 or Z=9
If P=1:Text(0,0,"O
If P=2:Text(0,0,"X
getKey->theta
A+(20(theta=26))-(20(theta=24))->A
B+(20(theta=25))-(20(theta=34))->B
Pt-On(A,B
Pt-Off(A-20,B
Pt-Off(A+20,B
Pt-Off(A,B-20
Pt-Off(A,B+20
Pt-Off(A-20,B-20
Pt-Off(A+20,B-20
Pt-Off(A+20,B+20
Pt-Off(A-20,B+20
If theta=105 and P=1:Then
Circle(A,B,10,10
If A=10 and B=50:Then
1->[A](1,1)
End
If A=10 and B=30:Then
1->[A](2,1)
End
If A=10 and B=10:Then
1->[A](3,1)
End
If A=30 and B=50:Then
1->[A](1,2)
End
If A=30 and B=50:Then
1->[A](1,2)
End
If A=30 and B=30:Then
1->[A](2,2)
End
If A=30 and B=10:Then
1->[A](3,2)
End
If A=50 and B=50:Then
1->[A](1,3)
End
If A=50 and B=30:Then
1->[A](2,3)
End
If A=50 and B=10:Then
1->[A](3,3)
End
If [A](1,1)+[A](1,2)+[A](1,3)=3 or [A](2,1)+[A](2,2)+[A](2,3)=3 or [A](3,1)+[A](3,2)+[A](3,3)=3 or [A](1,1)+[A](2,2)+[A](3,3)=3 or [A](1,3)+[A](2,2)+[A](3,1)=3 or [A](1,1)+[A](2,1)+[A](3,1)=3 or [A](1,2)+[A](2,2)+[A](3,2)=3 or [A](1,3)+[A](2,3)+[A](3,3)=3
Then:9->Z:"PLAYER 1 WINS"->Str1:1->W
Else:not(P)->P:0->theta:Z+1->Z
End
If theta=105 and P=0:Then
Line(A-10,B+10,A+10,B-10,11
Line(A+10,B+10,A-10,B-10,11
If A=10 and B=50:Then
~1->[A](1,1)
End
If A=10 and B=30:Then
~1->[A](2,1)
End
If A=10 and B=10:Then
~1->[A](3,1)
End
If A=30 and B=50:Then
~1->[A](1,2)
End
If A=30 and B=50:Then
~1->[A](1,2)
End
If A=30 and B=30:Then
~1->[A](2,2)
End
If A=30 and B=10:Then
~1->[A](3,2)
End
If A=50 and B=50:Then
~1->[A](1,3)
End
If A=50 and B=30:Then
~1->[A](2,3)
End
If A=50 and B=10:Then
~1->[A](3,3)
End
If If [A](1,1)+[A](1,2)+[A](1,3)=~3 or [A](2,1)+[A](2,2)+[A](2,3)=~3 or [A](3,1)+[A](3,2)+[A](3,3)=~3 or [A](1,1)+[A](2,2)+[A](3,3)=~3 or [A](1,3)+[A](2,2)+[A](3,1)=~3 or [A](1,1)+[A](2,1)+[A](3,1)=~3 or [A](1,2)+[A](2,2)+[A](3,2)=~3 or [A](1,3)+[A](2,3)+[A](3,3)=~3
Then:9->Z:"PLAYER 2 WINS"->Str1:1->W
Else:not(P)->P:0->theta
End
End
End
If W=1:Then
ClrHome:Pause Str1
End
ClrHome
Menu("PLAY AGAIN?","YES",1,"NO",2
Lbl 2
ClrHome:Stop
End
First off, this new post looks very similar to your previous one, but now you use matrices. So you can get rid of this line:

Code:
3->dim(L1):Fill(0,L1):L1->L2:L2->L3:L3->L4:L4->L5:L5->L6


You are storing x and y values of the cursor into A and B, setting them equal to 30 (the middle of the board):

Code:
30->A:30->B

What would really help is to use A and B to address where the cursor is within the Matrix. This would make matrix manipulation easier and save speed and space. This is what that line would look like:

Code:
2->A:2->B

And this:

Code:
A+(20(theta=26))-(20(theta=24))->A
B+(20(theta=25))-(20(theta=34))->B
Pt-On(A,B
Pt-Off(A-20,B
Pt-Off(A+20,B
Pt-Off(A,B-20
Pt-Off(A,B+20
Pt-Off(A-20,B-20
Pt-Off(A+20,B-20
Pt-Off(A+20,B+20
Pt-Off(A-20,B+20

would be replaced with this (along with an addition of edge-detection):

Code:
Pt-Off(20A-10,20B-10       //This converts the 1,2,3 to 10,30,50
A+(theta=26 and A<3)-(theta=24 and A>1->A
B+(theta=25 and B<3)-(theta=34 and B>1->B
Pt-On(20A-10,20B-10
.....
Circle(20A-10,20B-10,10,10
.....
Line(20A-20,20B,20A,20B-20,11
Line(20A,20B,20A-20,20B-20,11

Nest to address is this bit of your code:

Code:
If A=10 and B=50:Then
1->[A](1,1)
End
If A=10 and B=30:Then
1->[A](2,1)
End
If A=10 and B=10:Then
1->[A](3,1)
End
If A=30 and B=50:Then
1->[A](1,2)
End
If A=30 and B=50:Then
1->[A](1,2)
End
If A=30 and B=30:Then
1->[A](2,2)
End
If A=30 and B=10:Then
1->[A](3,2)
End
If A=50 and B=50:Then
1->[A](1,3)
End
If A=50 and B=30:Then
1->[A](2,3)
End
If A=50 and B=10:Then
1->[A](3,3)
End

First, I notice that you are storing data to the matrix so as to make the matrix look like board, even though it would be addressed differently (points are (x,y), matrixes are (y,x), where y starts at the top). There is no need to convert the locations to make it look the same, you could leave it so that an X located on the second row from the bottom on the right side would be put into the matrix at (1,2), where it would look like it is on the top in the middle in the matrix editor. Your code also has and If-Then for each of the 9 possibilities, which is slow and large, and doesn't check to see if that spot is already occupied. To solve this, and keeping in mind the use of A and B as location in the matrix (from 1 to 3), the code should/could look like this:

Code:
If not([A](A,B     //check's to make sure that spot is empty, not letting an X being put on top of an O or vice versa
Then
1->[A](A,B)
End

27 lines of code are now 4, and it even checks for collisions. That's optimization. Just change that 1 to a ~1 and you have Player 2's code.

Your win-check is long, as it is only one If-Then, and it is fast, so it can stay as-is. But instead of storing 1 to W and a line to display to Str1, you could eliminate Str1 usage by storing the player number into W, so 1->W if Player 1 wins or 2->W if Player 2 wins. Then the end of your program:

Code:
If W=1:Then
ClrHome:Pause Str1
End

would be replaced with

Code:
If W:Then
ClrHome:Pause "Player "+sub("12",W,1)+" WINS"
End

The same little "sub(" trick can be used to optimize:

Code:
If P=1:Text(0,0,"O
If P=2:Text(0,0,"X

into

Code:
Text(0,0,sub("OX",P,1


This all makes your code smaller and faster, and would do you very well. You still don't have an AI, but my code from 2 posts back has AI code within "AI start" and "AI end" that can be placed right into you existing or this new optimized code. This project is going to be cool, but the program has to be fast and small.
Thank you for doing all this, NoahK! I will take all of this into consideration and add/subtract from my code. Again, thanks!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 4
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement