-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
288 lines (248 loc) · 8.48 KB
/
Copy pathscript.js
File metadata and controls
288 lines (248 loc) · 8.48 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -------------------------------------------------------------------------- */
/* PAGE SCRIPT */
/* -------------------------------------------------------------------------- */
const restart = document.querySelector("#restart");
const message = document.querySelector("#message");
const span = document.querySelectorAll(".player span");
const colorInputs = document.querySelectorAll("input[type='color']");
const board = document.querySelector("#board");
colorInputs.forEach((colorInput) => {
colorInput.addEventListener("input", (e) => {
colorInput.parentElement.style.backgroundColor = e.target.value;
});
});
span.forEach((span) => {
span.setAttribute("contenteditable", "true");
});
function checkLength(event) {
const maxChars = 1;
const target = event.target;
currentChars = target.textContent.length;
if (currentChars >= maxChars) {
/* event.preventDefault(); */ //since we use keypress event, we prevent its default behavior.
target.textContent = target.textContent.slice(0, -1);
}
}
/* -------------------------------------------------------------------------- */
/* TIC TAC TOE SCRIPT */
/* -------------------------------------------------------------------------- */
class Player {
#name;
#tilePiece;
#color;
constructor(name, tilePiece, color) {
this.#name = name;
this.#tilePiece = tilePiece;
this.#color = color;
}
get name() {
return this.#name;
}
get tilePiece() {
return this.#tilePiece;
}
get color() {
return this.#color;
}
}
class Game {
#isGameOver = false;
#winner = null;
get isGameOver() {
return this.#isGameOver;
}
get winner() {
return this.#winner;
}
startGame() {
throw new Error("start() method not implemented");
}
endGame() {
this.#isGameOver = true;
}
resetGame() {
throw new Error("restart() method not implemented");
}
setWinner(winner) {
this.#winner = winner;
}
resetWinner() {
this.#winner = null;
}
setIsGameOver(isGameOver) {
this.#isGameOver = isGameOver;
}
}
class TicTacToe extends Game {
#board = Array(9).fill({ tilePiece: "", color: "" }); //color accounts for same tilePieces but different colors
#currentPlayer = null;
#startingPlayer = 1;
player1 = null;
player2 = null;
constructor() {
super(); // Call the parent class's constructor to initialize isGameOver and winner
document.querySelector("#restart").addEventListener("click", (e) => {
restart.classList.add("disabled");
e.target.innerText === "Restart" ? this.resetGame() : this.startGame();
});
}
/* ------------------------------------------------------------------------ */
/* FUNCTIONS */
/* ------------------------------------------------------------------------ */
startGame() {
this.grabPlayers();
this.#currentPlayer =
this.#startingPlayer === 1 ? this.player1 : this.player2;
restart.innerText = "Restart";
this.updateHoverStyles();
this.updateMessage(
this.#currentPlayer.name,
this.#currentPlayer.tilePiece,
this.#currentPlayer.color
);
board.addEventListener("click", (event) => {
if (event.target.matches(".tile")) {
const tileIndex = event.target.dataset.tile;
if (this.isGameOver || this.#board[tileIndex].tilePiece !== "") return;
this.makeMove(tileIndex); //mark the board
this.checkWinner(); //check if we won
}
});
}
makeMove(tileIndex) {
if (this.#board[tileIndex].tilePiece === "") {
this.#board[tileIndex] = {
tilePiece: this.#currentPlayer.tilePiece,
color: this.#currentPlayer.color,
};
this.updateUI(tileIndex); //update tile on our board on the DOM
this.switchPlayer();
this.updateHoverStyles();
this.updateMessage(
this.#currentPlayer.name,
this.#currentPlayer.tilePiece,
this.#currentPlayer.color
);
}
}
checkWinner() {
const winningCombinations = [
[0, 1, 2], //horizontal
[3, 4, 5], //horizontal
[6, 7, 8], //horizontal
[0, 3, 6], //vertical
[1, 4, 7], //vertical
[2, 5, 8], //vertical
[0, 4, 8], //diagonal
[2, 4, 6], //diagonal
];
for (const combination of winningCombinations) {
const [a, b, c] = combination;
console.log(this.#board);
if (
this.#board[a].tilePiece !== "" &&
this.#board[a].tilePiece === this.#board[b].tilePiece &&
this.#board[b].tilePiece === this.#board[c].tilePiece &&
this.#board[a].color === this.#board[b].color &&
this.#board[b].color === this.#board[c].color
) {
this.setWinner(this.#board[a].tilePiece);
this.endGame();
return;
}
}
if (this.#board.every((tile) => tile.tilePiece !== "")) {
this.endGame();
}
}
updateUI(tileIndex) {
const tile = document.querySelector(`[data-tile="${tileIndex}"]`);
tile.textContent = this.#board[tileIndex].tilePiece;
if (this.#board[tileIndex]) {
tile.style.textShadow = `0 0 0 ${this.#board[tileIndex].color}`; /* `0 0 0 ${this.#currentPlayer.color}`; */
tile.style.color = this.#board[tileIndex].color === "rgb(0, 0, 0)" || this.#board[tileIndex].color === "black" ? "black" : "transparent";
}
}
endGame() {
this.setIsGameOver(true);
this.updateHoverStyles("");
restart.classList.remove("disabled");
this.switchPlayer(); //because i switched the player early in preparation for the next player's colors and tile piece, I have to switch back since the game is over and i need to grab the correct color.
const color = this.winner ? this.#currentPlayer.color : "black";
this.updateMessage(
this.#currentPlayer.name,
this.#currentPlayer.tilePiece,
color
);
}
resetUIBoard() {
const tiles = document.querySelectorAll(".tile");
tiles.forEach((tile) => {
const tileIndex = tile.dataset.tile;
tile.textContent = this.#board[tileIndex].tilePiece;
});
}
resetGame() {
this.grabPlayers(); //feel free to customize your name/tile piece/color before restarting the game.
this.#board = Array(9).fill({ tilePiece: "", color: "" });
this.#startingPlayer = this.#startingPlayer === 2 ? 1 : 2;
this.#currentPlayer =
this.#startingPlayer === 1 ? this.player1 : this.player2;
this.resetWinner();
this.setIsGameOver(false);
this.updateHoverStyles();
this.updateMessage(
this.#currentPlayer.name,
this.#currentPlayer.tilePiece,
this.#currentPlayer.color
);
this.resetUIBoard();
}
/* ------------------------------------------------------------------------ */
/* HELPER FUNCTIONS */
/* ------------------------------------------------------------------------ */
switchPlayer() {
this.#currentPlayer =
this.#currentPlayer === this.player2 ? this.player1 : this.player2;
}
grabPlayers() {
this.player1 = new Player(
document.querySelector(".player.one .name").innerText,
document.querySelector(".player.one .tilePiece").innerText,
getComputedStyle(document.querySelector(".player.one")).backgroundColor
);
this.player2 = new Player(
document.querySelector(".player.two .name").innerText,
document.querySelector(".player.two .tilePiece").innerText,
getComputedStyle(document.querySelector(".player.two")).backgroundColor
);
}
updateHoverStyles(
content = `"${this.#currentPlayer.tilePiece}"`,
color = `${this.#currentPlayer.color}`
) {
document.documentElement.style.setProperty("--hover-content", content);
document.documentElement.style.setProperty("--hover-shadow", color);
if (
this.#currentPlayer.color === "rgb(0, 0, 0)" ||
this.#currentPlayer.color === "black"
) {
document.documentElement.style.setProperty("--hover-color", "black");
} else {
document.documentElement.style.setProperty(
"--hover-color",
"transparent"
);
}
}
updateMessage(name, tilePiece, color) {
message.style.color = color;
message.innerHTML = !this.isGameOver
? `${name} ${tilePiece}'s Turn`
: this.winner
? `<span class="flip">🎉</span> ${name} ${tilePiece} <span style="color: #D4AF37;">Wins!</span> 🎉`
: "It's a Tie!";
}
}
// Create a new instance of the TicTacToe class
const game = new TicTacToe();