Newer
Older
var fullBoard = [[],[],[]];
fullBoard[0][0] = null;
fullBoard[0][1] = null;
fullBoard[0][2] = null;
fullBoard[1][0] = null;
fullBoard[1][1] = null;
fullBoard[1][2] = null;
fullBoard[2][0] = null;
fullBoard[2][1] = null;
fullBoard[2][2] = null;
// game setup. called at startup.
// determines who will start
function startGame() {
document.turn = "X";
if (Math.random() < 0.5) {
document.turn = "O";
}
setMessage(document.turn + " gets to start.");
}
function setMessage(msg) {
document.getElementById("message").innerText = msg;
}
function nextMove(square) {
if(square.innerText == ""){
square.innerText = document.turn;
checkCompletedBoard(square);
switchTurn();
}
function switchTurn() {
if (document.turn == "X") {
document.turn = "O";
} else {
document.turn = "X";
}
setMessage("It's " + document.turn + "'s turn!");
}
var color = null;
if (square.innerText == 'O'){
color = 'red'
}
else{
color = 'pink';
}
var boardID = square.parentNode.parentNode.parentNode.parentNode.id;
var boardTable = document.getElementById(boardID).children[0].children[0].children;
//identifying the col and row of the innerBoard in terms of the fullBoard
var row = boardID.charAt(1)-1;
var col = boardID.charAt(2);
if (innerBoard[0][0] == square.innerText && innerBoard [0][1] == square.innerText && innerBoard[0][2] == square.innerText){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
else if (innerBoard[1][0] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[1][2] == square.innerText){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
else if (innerBoard[2][0] == square.innerText && innerBoard [2][1] == square.innerText && innerBoard[2][2] == square.innerText){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
else if (innerBoard[0][0] == square.innerText && innerBoard [1][0] == square.innerText && innerBoard[2][0] == square.innerText){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
//col 2
else if (innerBoard[0][1] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[2][1] == square.innerText){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
//col 3
else if (innerBoard[0][2] == square.innerText && innerBoard [1][2] == square.innerText && innerBoard[2][2] == square.innerText){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
// diagonal
else if (innerBoard[0][0] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[2][2] == square.innerText){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
//diagonal
else if (innerBoard[0][2] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[2][0] == square.innerText){
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//indicating on the full board that the inner board is won
fullBoard[col][row] = square.innerText;
}
checkWin();
}
function checkWin(){
// row 1
if (fullBoard[0][0] == fullBoard[0][1] && fullBoard[0][0] == fullBoard[0][2] && fullBoard[0][0] !=null){
window.alert(fullBoard[0][0] + " wins the game!!!");
}
//row 2
else if (fullBoard[1][0] == fullBoard[1][1] && fullBoard[1][0] == fullBoard[1][2] && fullBoard[1][0]!=null){
window.alert(fullBoard[1][0] + " wins the game!!!");
}
//row 3
else if (fullBoard[2][0] == fullBoard[2][1] && fullBoard[2][0] == fullBoard[2][2] && fullBoard[2][0]!=null){
window.alert(fullBoard[2][0] + " wins the game!!!");
}
//col 1
else if (fullBoard[0][0] == fullBoard[1][0] && fullBoard[0][0] == fullBoard[2][0] && fullBoard[0][0]!=null){
window.alert(fullBoard[0][0] + " wins the game!!!");
}
//col 2
else if (fullBoard[0][1] == fullBoard[1][1] && fullBoard[0][1] == fullBoard[2][1] && fullBoard[0][1]!=null){
window.alert(fullBoard[0][1] + " wins the game!!!");
}
//col 3
else if (fullBoard[0][2] == fullBoard[1][2] && fullBoard[0][2] == fullBoard[2][2] && fullBoard[0][2]!=null){
window.alert(fullBoard[0][2] + " wins the game!!!");
}
// diagonal
else if (fullBoard[0][0] == fullBoard[1][1] && fullBoard[0][0] == fullBoard[2][2] && fullBoard[0][0]!=null){
window.alert(fullBoard[0][0] + " wins the game!!!");
}
//diagonal
else if (fullBoard[0][2] == fullBoard[1][1] && fullBoard[0][2] == fullBoard[2][0] && fullBoard[0][2]!=null){
window.alert(fullBoard[0][2] + " wins the game!!!");
}
}
function getBoard(boardTable){
var innerBoard= [[],[],[]];
for (var i = 0; i<boardTable.length;i++){
for(var j = 0; j<row.length;j++){
//get the inner text of every element and put it in a 2d array
innerBoard[i][j] = row[j].innerText;
}
}
return innerBoard;