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) {
// log player move.
console.log("player: " + document.turn + " Played at: " + square.parentNode.parentNode.parentNode.parentNode.id + " || " + square.id);
// display as last move
document.getElementById("sirep").innerText = "last move: " + square.parentNode.parentNode.parentNode.parentNode.id + " || " + square.id;
changeColour(square);
// Check if win then change turn
checkCompletedBoard(square);
switchTurn();
}
// test for replacing sub games into Winner Symbol
function testMove(){
document.getElementById("B00").innerHTML = "X";
document.getElementById("B01").innerHTML = "-";
document.getElementById("B02").innerHTML = "O";
document.getElementById("B10").innerHTML = "-";
document.getElementById("B11").innerHTML = "X";
document.getElementById("B12").innerHTML = "O";
document.getElementById("B20").innerHTML = "O";
document.getElementById("B21").innerHTML = "O";
document.getElementById("B22").innerHTML = "X";
}
function switchTurn() {
if (document.turn == "X") {
document.turn = "O";
} else {
document.turn = "X";
}
setMessage("It's " + document.turn + "'s turn!");
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
}
function changeColour(square){
var boardID = square.parentNode.parentNode.parentNode.parentNode.id;
var tileID = square.id;
// Clean Board
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
document.getElementById("B"+i+j) .style.backgroundColor = 'white';
}
}
// set Next move's playable region
if (tileID == "s1"){
boardID = 'B00';
}else if (tileID == "s2"){
boardID = 'B01';
}else if (tileID == "s3"){
boardID = 'B02';
}else if (tileID == "s4"){
boardID = 'B10';
}else if (tileID == "s5"){
boardID = 'B11';
}else if (tileID == "s6"){
boardID = 'B12';
}else if (tileID == "s7"){
boardID = 'B20';
}else if (tileID == "s8"){
boardID = 'B21';
}else if (tileID == "s9"){
boardID = 'B22';
}
document.getElementById(boardID) .style.backgroundColor = "#79A6D8";
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);
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;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
//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;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
//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;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
//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;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
//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;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
//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;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
//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;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
//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){
//changing the color of the inner board to show a win
document.getElementById(boardID) .style.backgroundColor = color;
//changing the label of the inner board to show a win
document.getElementById(boardID).innerHTML = document.turn;
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//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;