Tic-tac-toe
by Cameron Miller
and Steven (Daddy/Uncle) Miller
and Eli Krantz

tictactoe[] := Module[{},
numplayers = Input["Press 1 for 1 player, 2 for 2 players."];
If[numplayers ≠ 2, numplayers = 1];
If[numplayers == 1,name=InputString["What is your name?"], name = InputString["What is player 1's name?"]];
If[numplayers == 2, name2=InputString["What is player 2's name?"]];
Print["Hello ",name,"."];
If[numplayers == 2, Print["Hellow ", name2, "."]];
(*numgames = Input["How many times would you like to play?"];
Print["Okay, we'll play ", numgames, " games."];*)
totalwins = 0;
ywin = 0;
xwin = 0;
nummoves = 0;
humanwin = 0;
computerwin = 0;

gameover = 0;
Print[" "];
Print["You get to go first."];
board = {{1,2,3},{4,5,6},{7,8,9}};
free = {1,2,3,4,5,6,7,8,9};

While[gameover < 1,
{
goodmove = 0;
While[goodmove < 1,
{
move = Input[name ": Enter the number of the square you want to go in.\n" MatrixForm[board]];
If[MemberQ[free,move] == True, goodmove = 10];
If[move == 0, gameover = 10];
If[goodmove == 0, Print["Invalid move, try again."]];
}]; (* end of while loop *)
nummoves = nummoves + 1;
free = DeleteCases[free, move];
If[move == 1, board[[1,1]] = "X"];
If[move == 2, board[[1,2]] = "X"];
If[move == 3, board[[1,3]] = "X"];
If[move == 4, board[[2,1]] = "X"];
If[move == 5, board[[2,2]] = "X"];
If[move == 6, board[[2,3]] = "X"];
If[move == 7, board[[3,1]] = "X"];
If[move == 8, board[[3,2]] = "X"];
If[move == 9, board[[3,3]] = "X"];
If[board[[1,1]] == "X" && board[[2,2]] == "X" && board[[3,3]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[board[[3,1]] == "X" && board[[2,2]] == "X" && board[[1,3]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[board[[1,1]] == "X" && board[[1,2]] == "X" && board[[1,3]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[board[[2,1]] == "X" && board[[2,2]] == "X" && board[[2,3]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[board[[3,1]] == "X" && board[[3,2]] == "X" && board[[3,3]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[board[[1,1]] == "X" && board[[2,1]] == "X" && board[[3,1]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[board[[1,2]] == "X" && board[[2,2]] == "X" && board[[3,2]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[board[[1,3]] == "X" && board[[2,3]] == "X" && board[[3,3]] == "X",
{
humanwin = 1;
computerwin = 0;
gameover = 1;
}];
If[nummoves == 9, gameover  = 1];

If[gameover < 1,
{
temp= RandomSample[free,1];
move = temp[[1]];

If[numplayers == 2,
{
goodmove = 0;
While[goodmove < 1,
{
move = Input[name2": Enter the number of the square you want to go in.\n" MatrixForm[board]];
If[MemberQ[free,move] == True, goodmove = 10];
If[move == 0, gameover = 10];
If[goodmove == 0, Print["Invalid move, try again."]];
}]; (* end of while loop *)
}];
If[numplayers ≠ 2,
{
If[board[[1,1]] == "X" && board[[2,2]] == "X" && board[[3,3]] == 9, move = 9];
If[board[[3,3]] == "X" && board[[2,2]] == "X" && board[[1,1]] == 1, move = 1];
If[board[[1,1]] == "X" && board[[2,2]] == 5 && board[[3,3]] == "X", move = 5];

If[board[[1,3]] == "X" && board[[2,2]] == "X" && board[[3,1]] == 7, move = 7];
If[board[[3,1]] == "X" && board[[2,2]] == "X" && board[[1,3]] == 3, move = 3];
If[board[[1,3]] == "X" && board[[2,2]] == 5 && board[[3,1]] == "X", move = 5];

If[board[[1,1]] == "X" && board[[1,2]] == "X" && board[[1,3]] == 3, move = 3];
If[board[[1,1]] == "X" && board[[1,2]] == 2 && board[[1,3]] == "X", move = 2];
If[board[[1,1]] == 1 && board[[1,2]] == "X" && board[[1,3]] == "X", move = 1];

If[board[[2,1]] == "X" && board[[2,2]] == "X" && board[[2,3]] == 6, move = 6];
If[board[[2,1]] == "X" && board[[2,2]] == 5 && board[[2,3]] == "X", move = 5];
If[board[[2,1]] == 4 && board[[2,2]] == "X" && board[[2,3]] == "X", move = 4];

If[board[[3,1]] == "X" && board[[3,2]] == "X" && board[[3,3]] == 9, move = 9];
If[board[[3,1]] == "X" && board[[3,2]] == 8 && board[[3,3]] == "X", move = 8];
If[board[[3,1]] == 7 && board[[3,2]] == "X" && board[[3,3]] == "X", move = 7];

If[board[[1,1]] == "X" && board[[2,1]] == "X" && board[[3,1]] == 7, move = 7];
If[board[[1,1]] == "X" && board[[2,1]] == 4 && board[[3,1]] == "X", move = 4];
If[board[[1,1]] == 1 && board[[2,1]] == "X" && board[[3,1]] == "X", move = 1];

If[board[[1,2]] == "X" && board[[2,2]] == "X" && board[[3,2]] == 8, move = 8];
If[board[[1,2]] == "X" && board[[2,2]] == 5 && board[[3,2]] == "X", move = 5];
If[board[[1,2]] == 2 && board[[2,2]] == "X" && board[[3,2]] == "X", move = 2];

If[board[[1,3]] == "X" && board[[2,3]] == "X" && board[[3,3]] == 9, move = 9];
If[board[[1,3]] == "X" && board[[2,3]] == 6 && board[[3,3]] == "X", move = 6];
If[board[[1,3]] == 3 && board[[2,3]] == "X" && board[[3,3]] == "X", move = 3];

If[board[[1,1]]=="O"&&board[[2,2]]=="O"&&board[[3,3]]==9,move=9];
If[board[[3,3]]=="O"&&board[[2,2]]=="O"&&board[[1,1]]==1,move=1];If[board[[1,1]]=="O"&&board[[2,2]]==5&&board[[3,3]]=="O",move=5];

If[board[[1,3]]=="O"&&board[[2,2]]=="O"&&board[[3,1]]==7,move=7];If[board[[3,1]]=="O"&&board[[2,2]]=="O"&&board[[1,3]]==3,move=3];If[board[[1,3]]=="O"&&board[[2,2]]==5&&board[[3,1]]=="O",move=5];

If[board[[1,1]]=="O"&&board[[1,2]]=="O"&&board[[1,3]]==3,move=3];If[board[[1,1]]=="O"&&board[[1,2]]==2&&board[[1,3]]=="O",move=2];If[board[[1,1]]==1&&board[[1,2]]=="O"&&board[[1,3]]=="O",move=1];

If[board[[2,1]]=="O"&&board[[2,2]]=="O"&&board[[2,3]]==6,move=6];If[board[[2,1]]=="O"&&board[[2,2]]==5&&board[[2,3]]=="O",move=5];If[board[[2,1]]==4&&board[[2,2]]=="O"&&board[[2,3]]=="O",move=4];

If[board[[3,1]]=="O"&&board[[3,2]]=="O"&&board[[3,3]]==9,move=9];If[board[[3,1]]=="O"&&board[[3,2]]==8&&board[[3,3]]=="O",move=8];If[board[[3,1]]==7&&board[[3,2]]=="O"&&board[[3,3]]=="O",move=7];

If[board[[1,1]]=="O"&&board[[2,1]]=="O"&&board[[3,1]]==7,move=7];If[board[[1,1]]=="O"&&board[[2,1]]==4&&board[[3,1]]=="O",move=4];If[board[[1,1]]==1&&board[[2,1]]=="O"&&board[[3,1]]=="O",move=1];

If[board[[1,2]]=="O"&&board[[2,2]]=="O"&&board[[3,2]]==8,move=8];If[board[[1,2]]=="O"&&board[[2,2]]==5&&board[[3,2]]=="O",move=5];If[board[[1,2]]==2&&board[[2,2]]=="O"&&board[[3,2]]=="O",move=2];

If[board[[1,3]]=="O"&&board[[2,3]]=="O"&&board[[3,3]]==9,move=9];If[board[[1,3]]=="O"&&board[[2,3]]==6&&board[[3,3]]=="O",move=6];If[board[[1,3]]==3&&board[[2,3]]=="O"&&board[[3,3]]=="O",move=3];


}];

If[MemberQ[free,move] == False, 
{
temp= RandomSample[free,1];
move = temp[[1]];
}]; (* this makes sure we don't assign it a move on a square filled in *)

nummoves = nummoves + 1;
free = DeleteCases[free, move];
If[move == 1, board[[1,1]] = "O"];
If[move == 2, board[[1,2]] = "O"];
If[move == 3, board[[1,3]] = "O"];
If[move == 4, board[[2,1]] = "O"];
If[move == 5, board[[2,2]] = "O"];
If[move == 6, board[[2,3]] = "O"];
If[move == 7, board[[3,1]] = "O"];
If[move == 8, board[[3,2]] = "O"];
If[move == 9, board[[3,3]] = "O"];
}];
If[board[[1,1]] == "O" && board[[2,2]] == "O" && board[[3,3]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];
If[board[[3,1]] == "O" && board[[2,2]] == "O" && board[[1,3]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];
If[board[[1,1]] == "O" && board[[1,2]] == "O" && board[[1,3]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];
If[board[[2,1]] == "O" && board[[2,2]] == "O" && board[[2,3]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];
If[board[[3,1]] == "O" && board[[3,2]] == "O" && board[[3,3]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];
If[board[[1,1]] == "O" && board[[2,1]] == "O" && board[[3,1]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];
If[board[[1,2]] == "O" && board[[2,2]] == "O" && board[[3,2]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];
If[board[[1,3]] == "O" && board[[2,3]] == "O" && board[[3,3]] == "O",
{
humanwin = 0;
computerwin = 1;
gameover = 1;
}];

}]; (*end of while loop involving gameover*)

Print[MatrixForm[board]];
If[humanwin == 1, Print["Congratulations -- ", name, " wins!"]];
If[computerwin== 1 && numplayers == 2, Print["Congratulations -- ", name2, " wins!"]];
If[computerwin == 1 && numplayers ≠ 2, Print["I win -- better luck next time, " name, "."]];
If[humanwin ≠ 1 && computerwin ≠ 1 && numplayers ≠ 2, Print["Hey -- it's a tie!"];

];
];

tictactoe[]

w

?Sound

Sound[SoundNote[#,1,RandomChoice[{"Piano","Cello","Tuba"}]]& /@ RandomInteger[12, 30], 4]