-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
138 lines (114 loc) · 3.25 KB
/
script.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
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
let cells = document.querySelectorAll(".cell");
let resetBtn = document.querySelector("#reset");
let newGmBtn = document.querySelector("#new");
let msgContainer = document.querySelector(".msg-container");
let msg = document.querySelector("#msg");
let turno;
let oWins = 0;
let xWins = 0;
let currentStartingPlayer = 'O';
const winPatterns = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
const updateTurnHighlight = () => {
if (turno) {
document.querySelector("#score-o").classList.add("highlight");
document.querySelector("#score-x").classList.remove("highlight");
} else {
document.querySelector("#score-o").classList.remove("highlight");
document.querySelector("#score-x").classList.add("highlight");
}
};
const resetGame = () => {
oWins = 0;
xWins = 0;
document.querySelector("#score-o").innerText = `O Wins: ${oWins}`;
document.querySelector("#score-x").innerText = `X Wins: ${xWins}`;
newGame();
}
const newGame = () => {
turno = (currentStartingPlayer === 'O');
enabledBoxes();
msgContainer.classList.add("hide");
updateTurnHighlight();
}
const toggleStartingPlayer = () => {
currentStartingPlayer = currentStartingPlayer === 'O' ? 'X' : 'O';
}
const endGame = () => {
const winner = checkWinner();
if (winner) {
showWinner(winner);
} else {
const isDraw = Array.from(cells).every(cell => cell.classList.contains('taken'));
if (isDraw) {
showDraw();
}
}
toggleStartingPlayer();
}
newGmBtn.addEventListener("click", newGame);
resetBtn.addEventListener("click", resetGame);
cells.forEach((cell) => {
cell.addEventListener("click", () => {
if (cell.classList.contains('taken')) {
return;
}
cell.innerText = turno ? "O" : "X";
cell.classList.add('taken');
endGame();
turno = !turno;
updateTurnHighlight();
});
});
const disabledBoxes = () => {
cells.forEach((cell) => {
cell.classList.add('taken');
cell.disabled = true;
});
}
const enabledBoxes = () => {
cells.forEach((cell) => {
cell.classList.remove('taken');
cell.innerText = "";
cell.disabled = false;
});
}
const showWinner = (winner) => {
msg.innerText = `${winner} won the game!`;
msgContainer.classList.remove("hide");
if (winner === "O") {
oWins++;
document.querySelector("#score-o").innerText = `O Wins: ${oWins}`;
} else if (winner === "X") {
xWins++;
document.querySelector("#score-x").innerText = `X Wins: ${xWins}`;
}
disabledBoxes();
}
const checkWinner = () => {
for (let pattern of winPatterns) {
const [a, b, c] = pattern;
if (cells[a].innerText &&
cells[a].innerText === cells[b].innerText &&
cells[a].innerText === cells[c].innerText) {
return cells[a].innerText;
}
}
return null;
}
const showDraw = () => {
msg.innerText = "It's a draw!";
msgContainer.classList.remove("hide");
disabledBoxes();
}
newGmBtn.addEventListener("click", newGame);
resetBtn.addEventListener("click", resetGame);
updateTurnHighlight();