-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
435 lines (374 loc) · 12.1 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* GameBoard module manages the state and operations of the tic-tac-toe game board.
* @module GameBoard
*/
const GameBoard = (function () {
let board = [
" ", " ", " ",
" ", " ", " ",
" ", " ", " "
];
/**
* Gets the current state of the game board.
* @returns {string[]} The current game board, represented as an array of strings.
*/
const getBoard = () => board;
/**
* Prints the current state of the game board to the console.
*/
const printBoard = () => {
console.log(board.slice(0, 3));
console.log(board.slice(3, 6));
console.log(board.slice(6, 9));
};
/**
* Checks if a cell at the specified position is empty.
* @param {number} pos - The position of the cell to check (0 to 8).
* @returns {boolean} True if the cell is empty, false otherwise.
*/
const checkCell = (pos) => {
return board[pos] === " ";
};
/**
* Updates the game board at a specific position with the player's mark.
* @param {number} pos - The position to update (0 to 8).
* @param {string} player - The player's mark ("X" or "O").
*/
const updateBoard = (pos, player) => {
board[pos] = player;
DisplayController.updateBoard(getBoard());
};
/**
* Resets the game board to its initial state with all cells empty.
*/
const resetBoard = () => {
board = [
" ", " ", " ",
" ", " ", " ",
" ", " ", " "
];
}
return {
getBoard,
printBoard,
checkCell,
updateBoard,
resetBoard
};
})();
/**
* Creates a player object.
* @param {string} name - The name of the player.
* @param {string} mark - The mark of the player ("X" or "O").
* @returns {object} The player object, which includes methods to increment the score and get the current score.
*/
function createPlayer(name, mark) {
let score = 0;
/**
* Increments the player's score by 1.
*/
const winGame = () => score++;
/**
* Gets the player's current score.
* @returns {number} The player's score.
*/
const getScore = () => score;
return { name, mark, winGame, getScore };
}
/**
* GameController module manages the game logic and player interactions.
* @param {string} [player1="p1"] - The name of the first player (default is "p1").
* @param {string} [player2="p2"] - The name of the second player (default is "p2").
* @returns {object} The game controller object with methods to play rounds, start a new game, and check the game state.
*/
function GameController(player1="p1", player2="p2") {
const board = GameBoard;
const display = DisplayController;
const p1 = createPlayer(player1, "X");
const p2 = createPlayer(player2, "O");
display.updateScore(p1, p2);
let activePlayer = Math.random() < 0.5 ? p1 : p2;
let gameEnded = false;
/**
* Switches the active player turn.
*/
const switchPlayerTurn = () => {
activePlayer = activePlayer === p1 ? p2 : p1;
display.highlightTurn(getActivePlayer().name);
};
/**
* Gets the currently active player.
* @returns {object} The active player object.
*/
const getActivePlayer = () => activePlayer;
/**
* Prints the current state of the game and updates the display.
*/
const printNewRound = () => {
board.printBoard();
display.updateBoard(board.getBoard());
if (!gameEnded) {
// console.log(`${getActivePlayer().name}'s turn.`);
}
};
/**
* Starts a new game by resetting the board and setting up the initial game state.
*/
const startNewGame = () => {
board.resetBoard();
gameEnded = false;
activePlayer = Math.random() < 0.5 ? p1 : p2;
display.highlightTurn(getActivePlayer().name);
printNewRound();
};
/**
* Plays a round by updating the board and checking for a winner or a tie.
* @param {number} pos - The position to play (0 to 8).
* @returns {boolean} True if the game has ended, false otherwise.
*/
const playRound = (pos) => {
if (gameEnded) {
// console.log("Game has already ended.");
return true;
}
//console.log(`${getActivePlayer().name} played in cell ${pos}`);
if (board.checkCell(pos)) {
board.updateBoard(pos, getActivePlayer().mark);
if (checkWinner()) {
endGame();
printNewRound();
return;
}
if (checkTie()) {
endGame(true);
printNewRound();
return;
}
switchPlayerTurn();
}
else {
// console.log(`Invalid move for ${activePlayer.mark}. Try again.`);
}
printNewRound();
};
/**
* Checks if there is a winner on the board.
* @returns {boolean} True if there is a winner, false otherwise.
*/
const checkWinner = () => {
const b = board.getBoard();
// Check diagonals
if ((b[0] === b[4] && b[4] === b[8] && b[0] !== " ") ||
(b[2] === b[4] && b[4] === b[6] && b[2] !== " ")) {
return true;
}
// Check rows and columns
for (let i = 0; i < 3; i++) {
if ((b[3 * i] === b[3 * i + 1] && b[3 * i + 1] === b[3 * i + 2] && b[3 * i] !== " ") ||
(b[i] === b[i + 3] && b[i + 3] === b[i + 6] && b[i] !== " ")) {
return true;
}
}
return false;
};
/**
* Checks if the game has ended in a tie.
* @returns {boolean} True if the game is a tie, false otherwise.
*/
const checkTie = () => {
const b = board.getBoard();
return b.every(cell => cell !== " ");
};
/**
* Ends the game by updating the score and showing a message.
* @param {boolean} [isTie=false] - Indicates if the game ended in a tie.
*/
const endGame = (isTie = false) => {
if (!isTie) {
getActivePlayer().winGame();
display.updateScore(p1, p2);
display.startNextGame(`${getActivePlayer().name} won the game!`);
switchPlayerTurn();
} else {
display.startNextGame("It's a tie!");
}
gameEnded = true;
};
/**
* Checks if the game has ended.
* @returns {boolean} True if the game has ended, false otherwise.
*/
const isGameEnded = () => gameEnded;
printNewRound();
return {
playRound,
getActivePlayer,
endGame,
isGameEnded,
startNewGame
};
}
/**
* DisplayController module handles the display and user interaction of the tic-tac-toe game.
* @module DisplayController
*/
const DisplayController = (() => {
let game = null;
let isClickedArray = [];
/**
* Initializes the game session by setting up event listeners for the start and reset buttons,
* and managing the display of player names and scores.
*/
const startSession = () => {
const startButton = document.querySelector(".start-button");
const resetButton = document.querySelector(".reset-button");
const p1Input = document.querySelector(".p1-name-input");
const p2Input = document.querySelector(".p2-name-input");
const p1Div = document.querySelector(".p1-name");
const p2Div = document.querySelector(".p2-name");
const p1Score = document.querySelector(".p1-score");
const p2Score = document.querySelector(".p2-score");
startButton.addEventListener("click", () => {
p1Div.textContent = p1Input.value === "" ? "Player X" : p1Input.value + " [X]";
p2Div.textContent = p2Input.value === "" ? "Player O" : p2Input.value + " [O]";
p1Input.style.display = "none";
p2Input.style.display = "none";
p1Div.style.display = "block";
p2Div.style.display = "block";
p1Score.style.display = "block";
p2Score.style.display = "block";
game = GameController(p1Div.textContent, p2Div.textContent);
highlightTurn(game.getActivePlayer().name);
startButton.style.display = "none";
resetButton.style.display = "block";
});
};
const overlay = document.querySelector(".overlay");
/**
* Displays an overlay announcing the outcome of the game and sets up an event listener
* to start a new game when the overlay is clicked.
* @param {string} msg - The message to display on the overlay (e.g., winner announcement or tie).
*/
const startNextGame = (msg) => {
const overlayText = document.querySelector(".overlay-text");
overlay.style.display = "flex";
overlayText.textContent = `${msg}`;
overlay.addEventListener("click", () => {
overlay.style.display = "none";
overlayText.textContent = "";
resetIsClicked();
game.startNewGame();
});
};
const cells = document.querySelectorAll(".cell");
/**
* Resets the `isClickedArray` to all `false` values, indicating that no cells have been clicked.
*/
const resetIsClicked = () => {
isClickedArray = isClickedArray.map(() => false); // Reset all to false
};
/**
* Updates the displayed scores for both players.
* @param {object} p1 - The first player object, which should have a `getScore` method.
* @param {object} p2 - The second player object, which should have a `getScore` method.
*/
const updateScore = (p1, p2) => {
const p1Score = document.querySelector(".p1-score");
const p2Score = document.querySelector(".p2-score");
p1Score.textContent = p1.getScore();
p2Score.textContent = p2.getScore();
}
/**
* Updates the game board display based on the current state of the board.
* @param {string[]} board - An array representing the current state of the game board.
*/
const updateBoard = (board) => {
cells.forEach((cell, index) => {
cell.textContent = board[index];
});
};
cells.forEach((cell, index) => {
let originalContent = "";
isClickedArray[index] = false;
cell.addEventListener("click", () => {
if (!game || game.isGameEnded()) return;
cell.style.color = "rgba(0, 0, 0, 1.0)";
if (!isClickedArray[index]) {
game.playRound(index);
isClickedArray[index] = true;
}
});
cell.addEventListener("mouseenter", () => {
if (!game || game.isGameEnded()) return;
originalContent = cell.textContent;
if (!isClickedArray[index] && cell.textContent === " ") {
cell.textContent = game.getActivePlayer().mark;
cell.style.color = "rgba(0, 0, 0, 0.5)";
}
});
cell.addEventListener("mouseleave", () => {
if (!game || game.isGameEnded()) return;
if (!isClickedArray[index] && originalContent === " ") {
cell.textContent = " "; // Revert to the original content if not clicked
}
cell.style.color = "rgba(0, 0, 0, 1.0)";
});
});
const resetButton = document.querySelector(".reset-button");
resetButton.addEventListener("click", () => {
resetSession();
});
/**
* Resets the game session, including the display of player names and scores, and reinitializes the session.
*/
const resetSession = () => {
game = null;
resetIsClicked();
const startButton = document.querySelector(".start-button");
const p1Input = document.querySelector(".p1-name-input");
const p2Input = document.querySelector(".p2-name-input");
const p1Div = document.querySelector(".p1-name");
const p2Div = document.querySelector(".p2-name");
const p1Score = document.querySelector(".p1-score");
const p2Score = document.querySelector(".p2-score");
p1Input.style.display = "block";
p2Input.style.display = "block";
p1Div.style.display = "none";
p2Div.style.display = "none";
p1Div.style.fontWeight = "normal";
p2Div.style.fontWeight = "normal";
p1Score.style.display = "none";
p2Score.style.display = "none";
startButton.style.display = "block";
resetButton.style.display = "none";
cells.forEach(cell => {
cell.textContent = " ";
cell.style.color = "rgba(0, 0, 0, 1.0)";
});
GameBoard.resetBoard();
startSession();
}
/**
* Highlights the name of the player whose turn it is.
* @param {string} player - The name of the player whose turn it is.
*/
const highlightTurn = (player) => {
const p1Div = document.querySelector(".p1-name");
const p2Div = document.querySelector(".p2-name");
if (player === p1Div.textContent) {
p1Div.style.fontWeight = "bold";
p2Div.style.fontWeight = "normal"
} else {
p2Div.style.fontWeight = "bold";
p1Div.style.fontWeight = "normal";
}
}
return {
updateBoard,
startSession,
updateScore,
startNextGame,
highlightTurn
};
})();
DisplayController.startSession();