//!-- List of all the variables:
> ʟM : A list which keeps track of the moves in order as game spots (1-9), prepended with 0.
> ʟMC : A list which is used for tracking the moves in order as primes, prepended with 0. Used for checking win conditions
> ʟMP : The list of probabilities to use when selecting a spot to play; AI use only
> GDB1 : A GDB which stores the initial graph settings, used for cleanup
> Xmin, Xmax, Ymin, Ymax : Graph variables which determine the screen scaling
> T : A real variable which keeps track of which turn it is, 1 or 2
> C : A real variable used to temporarily track the cell choice
> H : A real variable used to store the horizontal of the currently selected cell
> V : A real variable used to store the vertical of the currently selected cell
> K : A real variable used to track keypresses
> P : A real variable used to track the highlighted cell
> G : A real variable used to store where the Game Peice (X or O) gets displayed horizontally
> U : A real variable used to store where the Game Peice (X or O) gets displayed vertically
> E : A real variable used to track for loop iterations
> A : A real variable used to keep track of the prime multiples
> W : A real variable used as a flag to determine if win conditions have been met
--!//
SetUpEditor ʟM,ʟMC,ʟMP //Set up the lists we need
{0→ʟM //prepend 0's to the lists to initialize them
{0→ʟMC
StoreGDB GDB1 //store a GDB for cleanup
AxesOff //set up the graph screen
GridOff
CoordOff
Full
LabelOff
ExprOff
RectGC
Normal
Func
63→Ymax:0→Ymin //set our graph variables
93→Xmax:0→Xmin
ClrDraw //clear the graph screen
Text(⁻1,2,14,"M.E.N.A.C.E. //draw the gameboard
Line(29,49,65,49
Line(65,49,65,13
Line(65,13,29,13
Line(29,13,29,49
Line(32,47,62,47
Line(31,46,63,46
Line(63,46,63,16
Line(62,47,62,15
Line(62,15,32,15
Line(61,15,33,15
Line(31,16,31,46
Line(32,15,32,47
Line(42,45,42,15
Line(52,45,52,15
Line(33,25,61,25
Line(33,36,61,36
19→H //set the initial horizontal
35→V //set the initial vertical
1→P //set the initial highlighted cell
Repeat dim(ʟM)=10 //repeat until 9 turns have been played
1→T //Reset the turn variable
If fPart(dim(ʟM)/2):2→T //double check the turn variable with the list of moves played
If T=2:Text(⁻1,52,10,"CROSSES TURN! //display who's turn it is
If T=1:Text(⁻1,52,10,"NAUGHTS TURN!
If (T=2)(dim(ʟM)<10):Then //This is where the AI plays
{3,7,9,2,4,7,5,1,1}→ʟMP //this line is for testing and debugging only, it will be removed
If dim(ʟM)<4:{0,0,8,0,0,2,0,0,0}→ʟMP //this line is for testing and debugging only, it will be removed
prgmAI //this program will fetch the gameboard and recall the learning state into ʟMP
Repeat not(sum(C=ʟM //ensure we don't select a square that has already been filled
ʟMP //store this to Ans
1+sum(cumSum(Ans)<randsum(Ans→C //select a spot to play using partial probability
End
C→P //update the graphics for the selected cell
End
Line(H+14,V+2,H+22,V+2 //Honestly forgot what this was for, probably for highlighting the selected cell
Repeat C or sum(K={45,21,105 //repeat until the user makes a choice, or an exit key is pressed
Repeat C or sum(K={45,21,105,24,25,26,34 //wait until an arrow or exit key is pressed
getKey→K
End
If K=45:ClrHome //if K=45 immediately exit the program
If K=45:Stop
If C or sum(K={24,25,26,34:Then //if a choice was made or an arrow key was pressed
Line(H+14,V+2,H+22,V+2,0 //erase the selected cell highlighting
P+(K=26)(P<9)-(K=24)(P>1)+3(K=34)(P≤6)-3(K=25)(P≥4)→P //move the selected cell
P/3
If Ans≤1:35→V //calculate the V and H of the newly selected cell
If Ans≤2:24→V
If Ans≤3:14→V
If sum(P={1,4,7
19→H
If sum(P={2,5,8
29→H
If sum(P={3,6,9
39→H
Line(H+14,V+2,H+22,V+2 //redraw the cell highlighting on the new cell
If C:105→K //if a choice was made by the AI, simulate pressing enter
DelVar C //delete C to prevent errors
End
End
If (K=105)not(sum(P=ʟM:Then //if enter was pressed and the cell is not already chosen, then
P→ʟM(1+dim(ʟM //add the cell into the list of previously played moves
Line(H+14,V+2,H+22,V+2,0 //erase the cell highlighting
P/3
If Ans≤1:19→G //calculate the position of the game piece to output
If Ans≤2:29→G
If Ans≤3:39→G
If sum(P={1,4,7
35→U
If sum(P={2,5,8
46→U
If sum(P={3,6,9
56→U
Text(⁻1,G,U,sub("OX",T,1) //output the game piece based on the turn
expr(sub("020305071113171923",2P-1,2))→ʟMC(dim(ʟM //convert the position to the corresponding prime, and store it into the list of primes
If dim(ʟMC)≥6:Then //if more than 5 moves have been played, check to see if anyone has won
1→A //reset A
For(E,2,dim(ʟMC),2 //for each of the turn 1 plays
AʟMC(E)→A //multiply the prime corresponding to those plays
End
If not(sum(not(fPart(A/{30,1001,7429,238,627,1495,506,935}):Then //if the turn 1 plays did not win
1→A //reset A
For(E,3,dim(ʟMC),2 //for each of the turn 2 plays
AʟMC(E)→A //multiply the primes corresponding to those plays
End
End
sum(not(fPart(A/{30,1001,7429,238,627,1495,506,935})→W //if A is divisible by one of the prime multiplicatives, then a player has won
If W:10→dim(ʟM //simulate extra moves by expanding the main list
AxesOff //return to the graph, this is only precautionary
End
DelVar C
End
End
If T:Text(⁻1,52,10," " //erase the old text
If not(W:Then //if nobody won, it was a tie game
Text(⁻1,52,23,"TIE GAME!
Pause
Else //if somebody did win, display who won based on the turn
If T=2:Text(⁻1,52,15,"CROSSES WIN!
If T=1:Text(⁻1,52,15,"NAUGHTS WIN!
If T:Pause
End
RecallGDB GDB1 //clean up the graph settings
ClrDraw
ClrHome