Newer
Older
window.game = (function() {
var setColor = "#79A6D8"
var fullBoard = [[], [], []];
var winningSet = null;
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;
var win = 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) {
console.log(document.getElementById('s1').innerText);
if (win == null) {
// if tile is empty
if (square.innerText == "") {
// Print move to board
square.innerText = document.turn;
// 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;
// Check if win then change turn
checkCompletedBoard(square);
changeColour(square);
switchTurn();
}
}
else {
console.log('Game over');
}
}
// 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";
}
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
110
111
112
113
114
115
116
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
function switchTurn() {
if (document.turn == "X") {
document.turn = "O";
} else {
document.turn = "X";
}
setMessage("It's " + document.turn + "'s turn!");
}
function changeColour(square) {
var boardID;
var tileID = square.id;
// set Next move's playable region boardID
switch (tileID) {
case "s1":
boardID = 'B00';
break;
case "s2":
boardID = 'B01';
break;
case "s3":
boardID = 'B02';
break;
case "s4":
boardID = 'B10';
break;
case "s5":
boardID = 'B11';
break;
case "s6":
boardID = 'B12';
break;
case "s7":
boardID = 'B20';
break;
case "s8":
boardID = 'B21';
break;
case "s9":
boardID = 'B22';
break;
}
if (win == null) {
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (fullBoard[boardID.charAt(1)][boardID.charAt(2)] != null) {
if (fullBoard[i][j] == null) {
// set all backgound colour
document.getElementById("B" + i + j).style.backgroundColor = setColor;
// set whole board clickable
document.getElementById("B" + i + j).style.pointerEvents = 'auto';
}
else {
// set all backgound colour
document.getElementById("B" + i + j).style.backgroundColor = 'none';
// set whole board clickable
document.getElementById("B" + i + j).style.pointerEvents = 'none';
}
}
else {
if (fullBoard[i][j] == null) {
// set all backgound colour
document.getElementById("B" + i + j).style.backgroundColor = 'white';
// set whole board clickable
document.getElementById("B" + i + j).style.pointerEvents = 'none';
}
// set playable region backgound colour to indicate active area.
document.getElementById(boardID).style.backgroundColor = setColor;
// set region clickable
document.getElementById(boardID).style.pointerEvents = 'auto'
}
}
}
}
else {
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (fullBoard[i][j] == null) {
document.getElementById("B" + i + j).style.backgroundColor = 'white';
}
document.getElementById("B" + i + j).style.pointerEvents = 'none';
}
}
}
}
function checkCompletedBoard(square) {
var color = null;
if (square.innerText == 'O') {
color = 'red'
}
else {
color = 'pink';
}
//id of the inner board
var boardID = square.parentNode.parentNode.parentNode.parentNode.id;
//the inner board element
var boardTable = document.getElementById(boardID).children[0].children[0].children;
//getting the inner board as a 2d array
var innerBoard = getBoard(boardTable);
//identifying the col and row of the innerBoard in terms of the fullBoard
var row = boardID.charAt(1);
var col = boardID.charAt(2);
// row 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[row][col] = square.innerText;
// console.log(fullBoard)
}
//row 2
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[row][col] = square.innerText;
// console.log(fullBoard)
}
//row 3
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[row][col] = square.innerText;
// console.log(fullBoard)
}
//col 1
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[row][col] = square.innerText;
// console.log(fullBoard)
}
//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[row][col] = square.innerText;
// console.log(fullBoard)
}
//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[row][col] = square.innerText;
// console.log(fullBoard)
}
// 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[row][col] = square.innerText;
// console.log(fullBoard)
}
//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;
//indicating on the full board that the inner board is won
fullBoard[row][col] = square.innerText;
// console.log(fullBoard)
}
else {
var flag = true;
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (innerBoard[i][j] == "") {
flag = false;
break;
}
}
}
if (flag) {
fullBoard[row][col] = "-";
// console.log(fullBoard)
document.getElementById(boardID).innerHTML = "-";
}
}
win = checkWin();
}
function checkWin() {
var winner;
var winLine;
// row 1
if (fullBoard[0][0] == fullBoard[0][1] && fullBoard[0][0] == fullBoard[0][2] && fullBoard[0][0] != null) {
winner = fullBoard[0][0];
window.alert(fullBoard[0][0] + " wins the game!!!");
winningSet = ['00', '01', '02'];
}
//row 2
else if (fullBoard[1][0] == fullBoard[1][1] && fullBoard[1][0] == fullBoard[1][2] && fullBoard[1][0] != null) {
winner = fullBoard[1][0];
window.alert(fullBoard[1][0] + " wins the game!!!");
winningSet = ['10', '11', '12'];
}
//row 3
else if (fullBoard[2][0] == fullBoard[2][1] && fullBoard[2][0] == fullBoard[2][2] && fullBoard[2][0] != null) {
winner = fullBoard[2][0];
window.alert(fullBoard[2][0] + " wins the game!!!");
winningSet = ['20', '21', '22'];
}
//col 1
else if (fullBoard[0][0] == fullBoard[1][0] && fullBoard[0][0] == fullBoard[2][0] && fullBoard[0][0] != null) {
winner = fullBoard[0][0];
window.alert(fullBoard[0][0] + " wins the game!!!");
winningSet = ['00', '10', '20'];
}
//col 2
else if (fullBoard[0][1] == fullBoard[1][1] && fullBoard[0][1] == fullBoard[2][1] && fullBoard[0][1] != null) {
winner = fullBoard[0][1];
window.alert(fullBoard[0][1] + " wins the game!!!");
winningSet = ['01', '11', '21'];
}
//col 3
else if (fullBoard[0][2] == fullBoard[1][2] && fullBoard[0][2] == fullBoard[2][2] && fullBoard[0][2] != null) {
winner = fullBoard[0][2];
window.alert(fullBoard[0][2] + " wins the game!!!");
winningSet = ['02', '12', '22'];
}
// diagonal
else if (fullBoard[0][0] == fullBoard[1][1] && fullBoard[0][0] == fullBoard[2][2] && fullBoard[0][0] != null) {
winner = fullBoard[0][0];
window.alert(fullBoard[0][0] + " wins the game!!!");
winningSet = ['00', '11', '22'];
}
//diagonal
else if (fullBoard[0][2] == fullBoard[1][1] && fullBoard[0][2] == fullBoard[2][0] && fullBoard[0][2] != null) {
winner = fullBoard[0][2];
window.alert(fullBoard[0][2] + " wins the game!!!");
winningSet = ['02', '11', '20'];
}
if (winningSet != null) {
for (var i = 0; i < winningSet.length; i++) {
console.log(winningSet[i]);
// set all backgound colour
document.getElementById("B" + winningSet[i].charAt(0) + winningSet[i].charAt(1)).style.backgroundColor = 'green';
// set whole board clickable
document.getElementById("B" + winningSet[i].charAt(0) + winningSet[i].charAt(1)).style.pointerEvents = 'none';
}
}
return winner;
}
function getBoard(boardTable) {
var innerBoard = [[], [], []];
for (var i = 0; i < boardTable.length; i++) {
//get the row element
var row = boardTable[i].children;
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;
}
})();
Pareek Ravi
committed
// console.log(fullBoard);