TIC TAC TOE PROBLEM

generaltictactoe[simulate_,numiter_, size_] := Module[{},
(* simulate = 1 if simulating, if 0 brute force *)
(* numiter is the number of times we go through *)
(* doing it on size by size for the board *)
If[simulate > 1, Print["Bad choice of simulate."]];
wins[1] = 0; wins[2] = 0; (* keeps track of wins *)
list = {};
For[i = 1, i ≤ size, i++,
For[j = 1, j ≤ size, j++,
list = AppendTo[list, {i,j}]
]]; (* creates the board for the game *)
If[simulate == 0, biglist = Permutations[list]]; (* if brute force, store ALL games *)
If[simulate == 1, numdo = numiter, numdo = (size*size)!];
(* this sets how many times we do *)
For[n = 1, n ≤ numdo, n++,
{
If[Mod[n,numdo/10] == 0, Print["We have done ", 100.0 n / numdo, "%."]];
(* prints out status every 10% *)
If[simulate == 1, game = RandomSample[list],game = biglist[[n]]]; (* randomly choose a game, or take the next brute force game *)
(* reset game board; we have list of moves now *)
For[i = 1, i ≤ size, i++,
For[j = 1, j ≤ size, j++,
board[i,j] = 0; ]]; (* initialize all board squares to 0; no one took *)
gameover = 0; (* make this 1 as soon as game done *)
nummoves = 0; (* keeps track of what move we are on *)

While[nummoves < size * size && gameover < 1,
{
(* play till run out of moves or someone wins *)
nummoves = nummoves + 1; (* making a move! *)
currentmove = game[[nummoves]]; (* loads move *)
x = currentmove[[1]];
y = currentmove[[2]];
If[Mod[nummoves,2] == 1, board[x,y] = 1, board[x,y] = -1]; (* adds move to board *)

(* check to see if someone has won *)
win = 0;
For[i = 1, i ≤ size, i++,
If[Abs[Sum[board[i,j],{j,1,size}]] == size, win = 1];
]; (* ends i for loop; checks columns *)
For[j = 1, j ≤ size, j++,
If[Abs[Sum[board[i,j],{i,1,size}]] == size, win = 1];
]; (* ends i for loop; checks rows *)
If[Abs[Sum[board[i,i], {i,1,size}]] == size, win = 1]; 
If[Abs[Sum[board[size+1-i,i], {i,1,size}]] == size, win = 1];

If[win > 0,
{
gameover = 1; (* someone won!  record *)
If[Mod[nummoves,2] == 1, wins[1] = wins[1]+1, wins[2] = wins[2]+1];
}]; (* end if statement on win *)

}]; (* end of While loop *)

}]; (* end of n for loop *)

If[simulate == 1, Print["We are simulating and doing ", numiter, " trials."], Print["We are brute force enumerating all possibilities."]];

Print["Percentage of time player 1 won: ", 100. wins[1] / numdo, "(actual = ", wins[1]/numdo, ")"];
Print["Percentage of time player 2 won: ", 100. wins[2] / numdo, "(actual = ", wins[2]/numdo, ")"];
Print["Percentage of time of tied game: ", 100. - 100.(wins[1] + wins[2]) / numdo, "(actual = ", (numdo-wins[1]-wins[2])/numdo, ")"];
] (* end of module *)

Timing[generaltictactoe[1,1000,20]]

Timing[generaltictactoe[0,1000,2]]

Timing[generaltictactoe[0,1000,3]]

Timing[generaltictactoe[1,1000,20]]

fastgeneraltictactoe[simulate_,numiter_, size_] := Module[{},
(* simulate = 1 if simulating, if 0 brute force *)
(* numiter is the number of times we go through *)
(* doing it on size by size for the board *)
If[simulate > 1, Print["Bad choice of simulate."]];
wins[1] = 0; wins[2] = 0; (* keeps track of wins *)
list = {};
For[i = 1, i ≤ size, i++,
For[j = 1, j ≤ size, j++,
list = AppendTo[list, {i,j}]
]]; (* creates the board for the game *)
If[simulate == 0, biglist = Permutations[list]]; (* if brute force, store ALL games *)
If[simulate == 1, numdo = numiter, numdo = (size*size)!];
(* this sets how many times we do *)
For[n = 1, n ≤ numdo, n++,
{
If[Mod[n,numdo/10] == 0, Print["We have done ", 100.0 n / numdo, "%."]];
(* prints out status every 10% *)
If[simulate == 1, game = RandomSample[list],game = biglist[[n]]]; (* randomly choose a game, or take the next brute force game *)

gameover = 0; (* make this 1 as soon as game done *)
nummoves = 0; (* keeps track of what move we are on *)
(* initialize all sums to 0 *)
For[k = 1, k ≤ size, k++,
{
rowsum[k] = 0;
columnsum[k] = 0;
}];
maindiagsum = 0; oppdiagsum = 0;

While[nummoves < size * size && gameover < 1,
{
(* play till run out of moves or someone wins *)
nummoves = nummoves + 1; (* making a move! *)
currentmove = game[[nummoves]]; (* loads move *)
x = currentmove[[1]];
y = currentmove[[2]];
If[Mod[nummoves,2] == 1, player = 1, player = -1];

rowsum[x] = rowsum[x] + player;
columnsum[y] = columnsum[y] + player;
If[x == y, maindiagsum = maindiagsum + player];
If[x + y == size+1, oppdiagsum = oppdiagsum + player];

(* check to see if someone has won *)
win = 0;
(* only check where add new mark! *)
If[Abs[rowsum[x]]==size,win=1,
If[Abs[columnsum[y]]==size,win=1,
If[x==y&&Abs[maindiagsum]==size,win=1,If[x+y==size+1&&Abs[oppdiagsum]==size,win=1]]]];

If[win > 0,
{
gameover = 1; (* someone won!  record *)
If[Mod[nummoves,2] == 1, wins[1] = wins[1]+1, wins[2] = wins[2]+1];

}]; (* end if statement on win *)

}]; (* end of While loop *)

}]; (* end of n for loop *)

If[simulate == 1, Print["We are simulating and doing ", numiter, " trials."], Print["We are brute force enumerating all possibilities."]];

Print["Percentage of time player 1 won: ", 100. wins[1] / numdo, "(actual = ", wins[1]/numdo, ")"];
Print["Percentage of time player 2 won: ", 100. wins[2] / numdo, "(actual = ", wins[2]/numdo, ")"];
Print["Percentage of time of tied game: ", 100. - 100.(wins[1] + wins[2]) / numdo, "(actual = ", (numdo-wins[1]-wins[2])/numdo, ")"];
] (* end of module *)

Timing[fastgeneraltictactoe[0,1000,3]]

Timing[generaltictactoe[1,1000,20]]

Timing[fastgeneraltictactoe[1,1000,20]]

Timing[fastgeneraltictactoe[1,1000,200]]

Timing[generaltictactoe[1,1,200]]

Timing[generaltictactoe[1,1,100]]

Timing[fastgeneraltictactoe[1,1000,100]]