-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
96 lines (79 loc) · 2.78 KB
/
game.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
let currentTurn = 'X';
const reset = () => {
currentTurn = 'X';
let numberOfGrid = document.querySelectorAll(".grid-cell");
for (let i = 0; i < numberOfGrid.length; i++) {
numberOfGrid[i].textContent = "";
}
document.getElementById('player-turn').textContent = "Player 1 Start!";
document.body.style.backgroundColor = "white";
enableGrid();
}
const getBoard = () => {
const c1 = document.getElementById('cell1').textContent;
const c2 = document.getElementById('cell2').textContent;
const c3 = document.getElementById('cell3').textContent;
const c4 = document.getElementById('cell4').textContent;
const c5 = document.getElementById('cell5').textContent;
const c6 = document.getElementById('cell6').textContent;
const c7 = document.getElementById('cell7').textContent;
const c8 = document.getElementById('cell8').textContent;
const c9 = document.getElementById('cell9').textContent;
return {
c1, c2, c3, c4, c5, c6, c7, c8, c9
};
}
const checkWin = () => {
let win = false;
const c = getBoard();
if (c.c1 === c.c2 && c.c2 === c.c3 && c.c1 !== "") {
win = true;
} else if (c.c4 === c.c5 && c.c5 === c.c6 && c.c4 !== "") {
win = true;
} else if (c.c7 === c.c8 && c.c8 === c.c9 && c.c7 !== "") {
win = true;
} else if (c.c1 === c.c4 && c.c4 === c.c7 && c.c1 !== "") {
win = true;
} else if (c.c2 === c.c5 && c.c5 === c.c8 && c.c2 !== "") {
win = true;
} else if (c.c3 === c.c6 && c.c6 === c.c9 && c.c3 !== "") {
win = true;
} else if (c.c1 === c.c5 && c.c5 === c.c9 && c.c1 !== "") {
win = true;
} else if (c.c3 === c.c5 && c.c5 === c.c7 && c.c3 !== "") {
win = true;
}
if (win) {
document.body.style.backgroundColor = "white";
document.getElementById('player-turn').textContent = currentTurn + " Wins!";
disableGrid();
}
return win;
}
const gridContainer = document.getElementById('grid-container');
const enableGrid = () => {
gridContainer.classList.remove('disabled');
}
const disableGrid = () => {
gridContainer.classList.add('disabled');
}
const play = (cellNo) => {
if (!checkWin()) {
if (currentTurn === 'X') {
document.body.style.backgroundColor = "#4490BB";
document.getElementById('cell' + cellNo).textContent = currentTurn;
currentTurn = 'O';
} else {
document.body.style.backgroundColor = "#BB6F44";
document.getElementById('cell' + cellNo).textContent = currentTurn;
currentTurn = 'X';
}
updateContent();
}
if (checkWin()) {
checkWin();
}
}
const updateContent = () => {
document.getElementById('player-turn').textContent = currentTurn + "'s turn";
}