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

Question: I assume that the program needs DCS or DCSE, since I see DCS in the above code? And I will upload the version that you and PT_ have made, since it does seem better. And how did the win-check crash? Did it give an error? If so, what type of error? Or did it literally just stop working?
It does not need Doors, that is just a header I made to give it an icon (I like to have icons on all my programs, and the icon would have to be changed for the Color)
The whole program is in pure basic, no shell is needed to run it (Doors, Mirage, Ion, etc.

Win-check broke as in I won a couple of times but it never detected it, having me to clear/on-break the program to exit. It just didn't pick up on the win....We'll fix it.
Ok.

Little problem: I have to go and delete all of the spaces on the code before I test it.....
This little project is coming to an end soon....especially with the AI being done* (*maybe) and the final optimizations being made to the head program.

Unless you want to implement other ideas......
No... This is already better than I expected.
Smile Glad to hear it. As soon as PT and I figure out how to fix the win-check, you and I will get to work converting it to color (shouldn't take much, resizing the window, changing the Text( pixel locations, and adding different colors) and then the two programs (one monochrome, one color) will be ready for upload!
Sounds good!
NoahK wrote:
Note to PT_: your most recent optimizations have 2 bugs, 1) "Tie" does not cover over the text already on screen, so "Tie "(12 or so spaces) would have to be used. 2) Win detection broke. I don't know why, but it broke.

1) That is pretty easy to solve, but thanks!!
2) I have no idea why that comes, but I will take a look. Oh, and I have a color-calc, so it should be pretty easy to make it.

@caleb1997: see also http://www.cemetech.net/forum/viewtopic.php?t=11554&highlight= for deleting spaces.

@NoahK: I will also take a look at your AI Smile

EDIT: I think I found the problem for win-checking, I will solve it!
I found some issues with my AI, somethings it won't block/complete a set, causing it to be incompetent. I am having a look-see now...

I'll be glad to hear your solution to the win-check.

Just for name-sake, I move to call the completion of a tic-tac-toe a "set"
I second that move.
I've fixed the 2 bugs as NoahK noted. Here's my code:
SourceCoder 3 (TTT) wrote:
::DCS6
:"0810081008100810FFFF081008500B100D500B500810FFFF0810081008100810
:AxesOff
:0->O
:0->N
:Lbl 0
:0identity(3->[A]
:Menu("Tic-Tac-Toe AI","Play",1,"Quit",2
:Lbl 2
:ClrHome
:Stop
: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
:2->A
:2->B
:0->W
:0->R
:Repeat W or theta=45 or (R=9 and not(W
:min(3,B->B
:Text(0,0,"O'S TURN
:Pt-On(20A-10,20B-10
:Repeat Ans
:getKey
:End
:Ans->theta
:Pt-Off(20A-10,20B-10
:min(3,max(1,A+(theta=26)-(theta=24->A
:min(3,max(1,B+(theta=25)-(theta=34->B
:Pt-On(20A-10,20B-10
:If theta=105 and not([A](A,B
:Then
:R+1->R
:Pt-Off(20A-10,20B-10
:Circle(20A-10,20B-10,7
:1->[A](A,B
:For(C,1,3
:sum(seq([A](X,C),X,1,3->D
:sum(seq([A](C,X),X,1,3->E
:If sum(3={D,E
:1->W
:If sum(12={D,E
:2->W
:End
:sum(seq([A](X,X),X,1,3->D
:sum(seq([A](4-X,X),X,1,3->E
:sum(2(12={D,E})+(3={D,E->W
:If R<9 and not(W
:Then
:R+1->R
:Text(0,0,"X'S TURN
:prgmOPTICODE
: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
:For(C,1,3
:sum(seq([A](X,C),X,1,3->D
:sum(seq([A](C,X),X,1,3->E
:If sum(3={D,E
:1->W
:If sum(12={D,E
:2->W
:End
:sum(seq([A](X,X),X,1,3->D
:sum(seq([A](4-X,X),X,1,3->E
:If sum(3={D,E
:1->W
:If sum(12={D,E
:2->W
:End
:Text(0,0,"TIE!
: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
:End
:Pause
:Goto 0


EDIT: @NoahK: the move in center:

Code:
If not([A](2,2) and dim(L1
2+2[i]->L1(1
Caleb, to make your code work with the AI, a simple fit would be to add:

Code:
pgrmOPTICODE
I->A
J->B

between the lines:

Code:
If theta=105 and P=1:Then
If not([A](A,B


That would have the AI select the next move, and then would play it.
If you wanted to have the AI play first, simply put that bit of code in the same place for Player 1.

I am still trying to make the AI perfect, but I am designing it to be modular, so an easier version of it could be made without a problem.




<Edit>

I've finished the AI. There were some bugs in the previous version, but this one is spot-on, and I can't beat it (though more testing should be done to make sure that is the case). The code is completely modular, so modules can be removed to make a weaker AI, as you (caleb) said you wanted a "good" AI, as to make it playable. A difficulty selector is certainly possible, a varible would stroe the difficulty, and before each module, it would check to see if that module is used for that difficulty (the 'impossible' difficulty would use all the modules, the 'trivial' one would just play random moves).

Here is the new AI code:


Code:
ClrList L1
0->T
4->U

//check for win/win-block
For(S,1,3
For(Q,1,3
If [A](S,Q-1+(Q=1))=[A](S,Q+1-(Q=3)) and [A](S,Q-1+(Q=1))not([A](S,4-Q
Then
S+(4-Q)[i]->L1(1+dim(L1
If U=[A](S,Q+1-(Q=3
dim(L1->T
End
If [A](Q-1+(Q=1),S)=[A](Q+1-(Q=3),S) and [A](Q-1+(Q=1),S)not([A](4-Q,S
Then
4-Q+S[i]->L1(1+dim(L1
If U=[A](Q+1-(Q=3),S
dim(L1->T
End
If [A](S-(S=2),S-(S=2))=[A](S+1-2(S=3),S+1-2(S=3)) and [A](S-(S=2),S-(S=2))not([A](4-S,4-S
Then
4-S+(4-S)[i]->L1(1+dim(L1
If U=[A](S+1-2(S=3),S+1-2(S=3
dim(L1->T
End
If [A](4-S+(S=2),S-(S=2))=[A](3-S+2(S=3),S+1-2(S=3)) and [A](4-S+(S=2),S-(S=2))not([A](S,4-S
Then
S+(4-S)[i]->L1(1+dim(L1
If U=[A](3-S+2(S=3),S+1-2(S=3)
dim(L1->T
End
End
End

//play in center
If not(dim(L1)) and not([A](2,2
2+2[i]->L1(1

//block douple trap
If not(dim(L1)) and (([A](1,1)=[A](3,3) and [A](1,1)=1) or ([A](3,1)=[A](1,3) and [A](3,1)=1)) or T
Goto 6

//opposite corner
If not(dim(L1
Then
For(S,1,3,2
For(Q,1,3,2
If [A](4-S,4-Q)=1 and not([A](S,Q
S+Q[i]->L1(1+dim(L1
End
End
End

//random corner
If not(dim(L1
Then
For(S,1,3,2
For(Q,1,3,2
If not([A](S,Q
S+Q[i]->L1(1+dim(L1
End
End
End

//random side
Lbl 6
If not(dim(L1
Then
For(S,1,3,2
If not([A](2,S
2+S[i]->L1(1+dim(L1
If not([A](S,2
S+2[i]->L1(1+dim(L1
End
End

//finish
not(T)randInt(1,dim(L1))+T->S
real(L1(S->I
imag(L1(S->J
NoahK wrote:
I believe the double-post rule is within the hour, but correct me if I'm wrong. I any event, this post is not super related to the last one, so here it is.


The double-post rule is for 24 hours
Here is my optimized version of the AI, it saves about 100 bytes.

Code:
ClrList L1
4->U
//check for win/win-block
For(S,1,3
For(Q,1,3
If [A](S,Q-1+(Q=1))=[A](S,Q+1-(Q=3)) and [A](S,Q-1+(Q=1))not([A](S,4-Q
S+(4-Q)[i]+5(U=[A](S,Q+1-(Q=3->L1(1+dim(L1
If [A](Q-1+(Q=1),S)=[A](Q+1-(Q=3),S) and [A](Q-1+(Q=1),S)not([A](4-Q,S
4-Q+S[i]+5(U=[A](Q+1-(Q=3),S->L1(1+dim(L1
If [A](S-(S=2),S-(S=2))=[A](S+1-2(S=3),S+1-2(S=3)) and [A](S-(S=2),S-(S=2))not([A](4-S,4-S
4-S+(4-S)[i]+5(U=[A](S+1-2(S=3),S+1-2(S=3->L1(1+dim(L1
If [A](4-S+(S=2),S-(S=2))=[A](3-S+2(S=3),S+1-2(S=3)) and [A](4-S+(S=2),S-(S=2))not([A](S,4-S
S+[i](4-S)+5(U=[A](3-S+2(S=3),S+1-2(S=3->L1(1+dim(L1
End
End
//play in center
If not(dim(L1)) and not([A](2,2
2+2[i]->L1(1
//block douple trap
If not(dim(L1)) and (([A](1,1)=[A](3,3) and [A](1,1)=1) or ([A](3,1)=[A](1,3) and [A](3,1)=1
Goto 6
//opposite corner
If not(dim(L1
Then
For(S,1,3,2
For(Q,1,3,2
If [A](4-S,4-Q)=1 and not([A](S,Q
S+Q[i]->L1(1+dim(L1
End
End
End
//random corner
If not(dim(L1
Then
For(S,1,3,2
For(Q,1,3,2
If not([A](S,Q
S+Q[i]->L1(1+dim(L1
End
End
End
//random side
Lbl 6
If not(dim(L1
Then
For(S,1,3,2
If not([A](2,S
2+S[i]->L1(1+dim(L1
If not([A](S,2
S+2[i]->L1(1+dim(L1
End
End
//finish
randInt(1,dim(L1->S
real(L1(S->I
imag(L1(S->J
For(D,1,dim(L1
If 5<real(L1(D
Then
real(L1(D))-5->I
imag(L1(D->J
End
End

I chop off variable T, and replaces it with adding 5 by a good move.
That AI optimization, along with your base-code optimization......I think this program is ready for release! It's all up to Caleb!
PT_ wrote:

Code:
If [A](S,Q-1+(Q=1))=[A](S,Q+1-(Q=3)) and [A](S,Q-1+(Q=1))not([A](S,4-Q
S+(4-Q)[i]+5(U=[A](S,Q+1-(Q=3->L1(1+dim(L1
If [A](Q-1+(Q=1),S)=[A](Q+1-(Q=3),S) and [A](Q-1+(Q=1),S)not([A](4-Q,S
4-Q+S[i]+5(U=[A](Q+1-(Q=3),S->L1(1+dim(L1
If [A](S-(S=2),S-(S=2))=[A](S+1-2(S=3),S+1-2(S=3)) and [A](S-(S=2),S-(S=2))not([A](4-S,4-S
4-S+(4-S)[i]+5(U=[A](S+1-2(S=3),S+1-2(S=3->L1(1+dim(L1
If [A](4-S+(S=2),S-(S=2))=[A](3-S+2(S=3),S+1-2(S=3)) and [A](4-S+(S=2),S-(S=2))not([A](S,4-S
S+[i](4-S)+5(U=[A](3-S+2(S=3),S+1-2(S=3->L1(1+dim(L1

Let's see how much we can shorten shorten this "much code" Wink


Code:
For(S,1,3
For(Q,1,3
If [A](S,Q-1+(Q=1))=[A](S,Q+1-(Q=3)) and [A](S,Q-1+(Q=1))not([A](S,4-Q
S+(4-Q)[i]+5(U=[A](S,Q+1-(Q=3->L1(1+dim(L1
If [A](Q-1+(Q=1),S)=[A](Q+1-(Q=3),S) and [A](Q-1+(Q=1),S)not([A](4-Q,S
4-Q+S[i]+5(U=[A](Q+1-(Q=3),S->L1(1+dim(L1
If [A](S-(S=2),S-(S=2))=[A](S+1-2(S=3),S+1-2(S=3)) and [A](S-(S=2),S-(S=2))not([A](4-S,4-S
4-S+(4-S)[i]+5(U=[A](S+1-2(S=3),S+1-2(S=3->L1(1+dim(L1
If [A](4-S+(S=2),S-(S=2))=[A](3-S+2(S=3),S+1-2(S=3)) and [A](4-S+(S=2),S-(S=2))not([A](S,4-S
S+[i](4-S)+5(U=[A](3-S+2(S=3),S+1-2(S=3->L1(1+dim(L1
End
End


Using Ans.

Code:
For(S,1,3
For(Q,1,3
[A](S,Q+1-(Q=3
If [A](S,Q-1+(Q=1))=Ans and [A](S,Q-1+(Q=1))not([A](S,4-Q
S+(4-Q)[i]+5(U=Ans->L1(1+dim(L1
[A](Q+1-(Q=3),S
If [A](Q-1+(Q=1),S)=Ans and [A](Q-1+(Q=1),S)not([A](4-Q,S
4-Q+S[i]+5(U=Ans->L1(1+dim(L1
[A](S+1-2(S=3),S+1-2(S=3
If [A](S-(S=2),S-(S=2))=Ans and [A](S-(S=2),S-(S=2))not([A](4-S,4-S
4-S+(4-S)[i]+5(U=Ans->L1(1+dim(L1
[A](3-S+2(S=3),S+1-2(S=3
If [A](4-S+(S=2),S-(S=2))=Ans and [A](4-S+(S=2),S-(S=2))not([A](S,4-S
S+[i](4-S)+5(U=Ans->L1(1+dim(L1
End
End


Store repeated code to an unallocated variable.

Code:

For(S,1,3
For(Q,1,3
Q-1+(Q=1->P
Q+1-(Q=3->V
S-(S=2->R
S+1-2(S=3->T
[A](S,V
If [A](S,P)=Ans and [A](S,P)not([A](S,4-Q
S+(4-Q)[i]+5(U=Ans->L1(1+dim(L1
[A](V,S
If [A](P,S)=Ans and [A](P,S)not([A](4-Q,S
4-Q+S[i]+5(U=Ans->L1(1+dim(L1
[A](T,T
If [A](R,R)=Ans and [A](R,R)not([A](4-S,4-S
4-S+(4-S)[i]+5(U=Ans->L1(1+dim(L1
[A](3-S+2(S=3),T
If [A](4-S+(S=2),R)=Ans and [A](4-S+(S=2),R)not([A](S,4-S
S+[i](4-S)+5(U=Ans->L1(1+dim(L1
End
End


Fix those math things. (Q+1-(Q=3)) is the same thing as 3-(Q=1) for example.
Now you can start to see how things are actually working. Hope this helps, and that you can make it even shorter. Wink (I still see quite a few things to do, if you want)

Code:
For(S,1,3
For(Q,1,3
1+(Q=3->P
3-(Q=1->V
S-(S=2->R
S+(S=2->B
2+(S=2->T
4-S->C:4-Q->A
[A](S,V
If [A](S,P)=Ans and [A](S,P)not([A](S,A
S+A[i]+5(U=Ans->L1(1+dim(L1
[A](V,S
If [A](P,S)=Ans and [A](P,S)not([A](A,S
A+S[i]+5(U=Ans->L1(1+dim(L1
[A](T,T
If [A](R,R)=Ans and [A](R,R)not([A](C,C
C+C[i]+5(U=Ans->L1(1+dim(L1
[A](2-(S=2),T
If [A](4-B,R)=Ans and [A](4-B,R)not([A](S,C
S+C[i]+5(U=Ans->L1(1+dim(L1
End
End

Good work so far it looks like though! Smile
Thanks Mateo! If you see more, please! We're not stopping you. Smile
Okay, sounds good. I wasn't sure if you guys wanted to try it on your own. Anywho, here's some more things:

Starting with the base code:

Code:
For(S,1,3
For(Q,1,3
1+(Q=3->P
3-(Q=1->V
S-(S=2->R
S+(S=2->B
2+(S=2->T
4-S->C
4-Q->A
[A](S,V
If [A](S,P)=Ans and [A](S,P)not([A](S,A
S+A[i]+5(U=Ans->L1(1+dim(L1
[A](V,S
If [A](P,S)=Ans and [A](P,S)not([A](A,S
A+S[i]+5(U=Ans->L1(1+dim(L1
[A](T,T
If [A](R,R)=Ans and [A](R,R)not([A](C,C
C+C[i]+5(U=Ans->L1(1+dim(L1
[A](2-(S=2),T
If [A](4-B,R)=Ans and [A](4-B,R)not([A](S,C
S+C[i]+5(U=Ans->L1(1+dim(L1
End:End


Next step is to do some matrice rotations:

Code:
[A](S,V
If [A](S,P)=Ans and [A](S,P)not([A](S,A
S+A[i]+5(U=Ans->L1(1+dim(L1
[A](V,S
If [A](P,S)=Ans and [A](P,S)not([A](A,S
A+S[i]+5(U=Ans->L1(1+dim(L1


Can be (it's a tad slower, but definitely smaller by 26 bytes. The speed loss is really negligible though; it's only a 3x3 matrice):

Code:
S+A[i]->L
For(N,0,1
[A](S,V
If [A](S,P)=Ans and [A](S,P)not([A](S,A
L+5(U=Ans->L1(1+dim(L1
A^^T->[A]:A+S[i]->L
End

Note how the matrice is rotated twice, so it handles reseting itself too.
The next step is just some more varaible replacement. This should also lead to a speed increase, and save bytes.


Code:
[A](T,T
If [A](R,R)=Ans and [A](R,R)not([A](C,C
C+C[i]+5(U=Ans->L1(1+dim(L1
[A](2-(S=2),T
If [A](4-B,R)=Ans and [A](4-B,R)not([A](S,C
S+C[i]+5(U=Ans->L1(1+dim(L1


Can be:

Code:
[A](R,R->H
[A](T,T
If H=Ans and Hnot([A](C,C
C+C[i]+5(U=Ans->L1(1+dim(L1
[A](4-B,R->F
[A](2-(S=2),T
If F=Ans and Fnot([A](S,C
S+C[i]+5(U=Ans->L1(1+dim(L1


So now our optimized code looks more like this:

Code:
For(S,1,3
For(Q,1,3
1+(Q=3->P
3-(Q=1->V
S-(S=2->R
S+(S=2->B
2+(S=2->T
4-S->C:4-Q->A
S+A[i]->L
For(N,0,1
[A](S,V
If [A](S,P)=Ans and [A](S,P)not([A](S,A
L+5(U=Ans->L1(1+dim(L1
A^^T->[A]
A+S[i]->L
End
[A](R,R->H
[A](T,T
If H=Ans and Hnot([A](C,C
C+C[i]+5(U=Ans->L1(1+dim(L1
[A](4-B,R->F
[A](2-(S=2),T
If F=Ans and Fnot([A](S,C
S+C[i]+5(U=Ans->L1(1+dim(L1
End:End


Then you can use the statistic variables to your advantage:

Code:
For(S,1,3
For(Q,1,3
5(U=Ans->u
1+(Q=3->P
3-(Q=1->V
S-(S=2->R
S+(S=2->B
2+(S=2->T
4-S->C:4-Q->A
S+A[i]->L
For(N,0,1
[A](S,P->J
[A](S,V
If J=Ans and Jnot([A](S,A
L+u->L1(1+dim(L1
A^^T->[A]
A+S[i]->L
End
[A](R,R->H
[A](T,T
If H=Ans and Hnot([A](C,C
C+C[i]+u->L1(1+dim(L1
[A](4-B,R->F
[A](2-(S=2),T
If F=Ans and Fnot([A](S,C
S+C[i]+u->L1(1+dim(L1
End:End


It's a little harder to read than the previous one, but it's optimized, I guess. I haven't really tested these too much yet; so I would recommend giving them a try and I will fix it if it doesn't fully work as intended.
Wow, Mateo, thanks! I'm sure you or someone has juts been watching this thread "from above", but it sure is nice to have some new minds looking in on it. I'm not on my computer right now, but I'll boot it up and give the code a try. Thanks for taking the time to work on this.
  
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 3 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