-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjss.js
177 lines (149 loc) · 4.71 KB
/
jss.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
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
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");
const gameArea = document.getElementById("game-area");
const modeSelection = document.getElementById("mode-selection");
const playWithFriendBtn = document.getElementById("play-with-friend");
const playWithAiBtn = document.getElementById("play-with-ai");
playAgain.addEventListener("click", startNewGame);
// Mode selection
let playMode = "friend"; // Default is friend mode
playWithFriendBtn.addEventListener("click", () => {
playMode = "friend";
startGame();
});
playWithAiBtn.addEventListener("click", () => {
playMode = "ai";
startGame();
});
function startGame() {
modeSelection.classList.add("hidden");
gameArea.classList.remove("hidden");
startNewGame();
}
// Sounds
const gameOverSound = new Audio("sounds/game_over.wav");
const clickSound = new Audio("sounds/click.wav");
tiles.forEach((tile) => tile.addEventListener("click", tileClick));
// AI move logic (random for now)
function aiMove() {
let availableTiles = [];
// Find available tiles
tiles.forEach((tile, index) => {
if (!tile.innerText) {
availableTiles.push(index);
}
});
if (availableTiles.length === 0) return; // No moves left
// Pick a random tile for the AI
const randomTileIndex = availableTiles[Math.floor(Math.random() * availableTiles.length)];
const tile = tiles[randomTileIndex];
tile.innerText = PLAYER_O;
boardState[randomTileIndex] = PLAYER_O;
clickSound.play();
checkWinner();
turn = PLAYER_X;
setHoverText();
}
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;
clickSound.play();
setHoverText();
checkWinner();
// If playing with AI, let the AI make a move
if (playMode === "ai" && !gameOverArea.classList.contains("visible")) {
setTimeout(aiMove, 500); // AI moves after 0.5 seconds
}
} else if (playMode === "friend") {
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) {
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();
}
// Winning combinations
const winningCombinations = [
{ combo: [1, 2, 3], strikeClass: "strike-row-1" },
{ combo: [4, 5, 6], strikeClass: "strike-row-2" },
{ combo: [7, 8, 9], strikeClass: "strike-row-3" },
{ combo: [1, 4, 7], strikeClass: "strike-column-1" },
{ combo: [2, 5, 8], strikeClass: "strike-column-2" },
{ combo: [3, 6, 9], strikeClass: "strike-column-3" },
{ combo: [1, 5, 9], strikeClass: "strike-diagonal-1" },
{ combo: [3, 5, 7], strikeClass: "strike-diagonal-2" },
];