-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (106 loc) · 3.09 KB
/
index.js
File metadata and controls
124 lines (106 loc) · 3.09 KB
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
const tiles = document.querySelectorAll(".tile");
const PLAYER_X = "X";
const PLAYER_O = "O";
let turn = PLAYER_X;
const boardState = Array(tiles.length);
boardState.fill(null);
//Elements
const strike = document.getElementById("strike");
const gameOverArea = document.getElementById("game-over-area");
const gameOverText = document.getElementById("game-over-text");
const playAgain = document.getElementById("play-again");
playAgain.addEventListener("click", startNewGame);
//Sounds
const gameOverSound = new Audio("sounds/game_over.wav");
const clickSound = new Audio("sounds/click.wav");
tiles.forEach((tile) => tile.addEventListener("click", tileClick));
function setHoverText() {
//remove all hover text
tiles.forEach((tile) => {
tile.classList.remove("x-hover");
tile.classList.remove("o-hover");
});
const hoverClass = `${turn.toLowerCase()}-hover`;
tiles.forEach((tile) => {
if (tile.innerText == "") {
tile.classList.add(hoverClass);
}
});
}
setHoverText();
function tileClick(event) {
if (gameOverArea.classList.contains("visible")) {
return;
}
const tile = event.target;
const tileNumber = tile.dataset.index;
if (tile.innerText != "") {
return;
}
if (turn === PLAYER_X) {
tile.innerText = PLAYER_X;
boardState[tileNumber - 1] = PLAYER_X;
turn = PLAYER_O;
} else {
tile.innerText = PLAYER_O;
boardState[tileNumber - 1] = PLAYER_O;
turn = PLAYER_X;
}
clickSound.play();
setHoverText();
checkWinner();
}
function checkWinner() {
//Check for a winner
for (const winningCombination of winningCombinations) {
//Object Destructuring
const { combo, strikeClass } = winningCombination;
const tileValue1 = boardState[combo[0] - 1];
const tileValue2 = boardState[combo[1] - 1];
const tileValue3 = boardState[combo[2] - 1];
if (
tileValue1 != null &&
tileValue1 === tileValue2 &&
tileValue1 === tileValue3
) {
strike.classList.add(strikeClass);
gameOverScreen(tileValue1);
return;
}
}
//Check for a draw
const allTileFilledIn = boardState.every((tile) => tile !== null);
if (allTileFilledIn) {
gameOverScreen(null);
}
}
function gameOverScreen(winnerText) {
let text = "Draw!";
if (winnerText != null) {
text = `Winner is ${winnerText}!`;
}
gameOverArea.className = "visible";
gameOverText.innerText = text;
gameOverSound.play();
}
function startNewGame() {
strike.className = "strike";
gameOverArea.className = "hidden";
boardState.fill(null);
tiles.forEach((tile) => (tile.innerText = ""));
turn = PLAYER_X;
setHoverText();
}
const winningCombinations = [
//rows
{ combo: [1, 2, 3], strikeClass: "strike-row-1" },
{ combo: [4, 5, 6], strikeClass: "strike-row-2" },
{ combo: [7, 8, 9], strikeClass: "strike-row-3" },
//columns
{ combo: [1, 4, 7], strikeClass: "strike-column-1" },
{ combo: [2, 5, 8], strikeClass: "strike-column-2" },
{ combo: [3, 6, 9], strikeClass: "strike-column-3" },
//diagonals
{ combo: [1, 5, 9], strikeClass: "strike-diagonal-1" },
{ combo: [3, 5, 7], strikeClass: "strike-diagonal-2" },
];