-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (32 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const playingBoard = document.getElementById("board");
const symbols = "OXOXOXOXO";
let turn = 0;
playingBoard.addEventListener("click", function (event) {
const sell = event.target;
if (!sell.innerHTML.length){
sell.innerHTML = symbols[turn];
turn += 1;
}
const anyWinnerRes = anyWinner();
if(turn && turn >= 4 && anyWinnerRes){
playingBoard.innerHTML = `GAME OVER - "${anyWinnerRes}" has Won!`;
turn = undefined;
}
});
function anyWinner() {
const cell = document.querySelectorAll(".cell");
const board = [
[cell[0].innerText, cell[1].innerText, cell[2].innerText],
[cell[3].innerText, cell[4].innerText, cell[5].innerText],
[cell[6].innerText, cell[7].innerText, cell[8].innerText]
];
if((board[0][0] == board[0][1] && board[0][1] == board[0][2] ||
board[0][0] == board[1][0] && board[1][0] == board[2][0]) && board[0][0] != "") return board[0][0];
else if((board[1][0] == board[1][1] && board[1][1] == board[1][2] ||
board[0][1] == board[1][1] && board[1][1] == board[2][1] ||
board[0][2] == board[1][1] && board[1][1] == board[2][0] ||
board[0][0] == board[1][1] && board[1][1] == board[2][2]) && board[1][1] != "") return board[1][1];
else if((board[2][0] == board[2][1] && board[2][1] == board[2][2] ||
board[0][2] == board[1][2] && board[1][2] == board[2][2]) && board[2][2] != "") return board[2][2];
else return false;
}