-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
332 lines (304 loc) · 9.88 KB
/
index.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
const createGameBoard = () => {
const board = ["", "", "", "", "", "", "", "", ""];
return board;
};
let Gameboard = createGameBoard();
const cells = [...document.querySelectorAll(".cell")];
const resetBtn = document.querySelector(".reset-btn");
const ambientBtn = document.querySelector("#ambient-btn");
const header = document.querySelector("header");
const settings = document.querySelector(".settings-btn");
const turn = document.querySelector(".turn-message");
const message = document.querySelector(".message");
const turnMessage = document.querySelector(".turn-message");
const xIcons = document.getElementsByClassName("fa-x");
const OIcons = document.getElementsByClassName("fa-circle");
const footer = document.querySelector(".footer-link");
const form = document.querySelector("#form");
let gameMode = document.querySelector("#game-mode");
let playerOneNameInput = document.querySelector("#player-name-one");
let playerOneTokenInput = document.querySelector("#token-one");
let playerTwoNameInput = document.querySelector("#player-name-two");
function handleCellClick(e) {
e.stopImmediatePropagation();
let pos = e.target.id.charAt(5); // Only get the number of the id of the cell
Gameboard.setCell(pos);
}
Gameboard.setCell = (pos) => {
Game().getCurrentPlayer().token === "X" ? (token = "X") : (token = "O");
Game().currentPlayer = Game().getCurrentPlayer();
let tokenIsSet = false;
if (Gameboard[pos] === "") {
Gameboard[pos] = token;
tokenIsSet = true;
displayController().showsGameBoard();
Gameboard.checkForWin(Game().getCurrentPlayer().token);
if (
Gameboard.checkForDraw() === false &&
Gameboard.checkForWin(Game().getCurrentPlayer().token) === false
) {
displayController().showsTie();
}
if (
tokenIsSet &&
Gameboard.checkForWin(Game().getCurrentPlayer().token) === false &&
Gameboard.checkForDraw() === true
) {
//If there is no tie or winner, change turn
Game().switchPlayerTurn();
displayController().showsTurn();
}
return tokenIsSet;
} else {
return;
}
};
Gameboard.reset = () => {
for (let i = 0; i < Gameboard.length; i++) {
Gameboard[i] = "";
}
return true;
};
Gameboard.checkForWin = (token) => {
if (
(Gameboard[0] === token &&
Gameboard[1] === token &&
Gameboard[2] === token) ||
(Gameboard[3] === token &&
Gameboard[4] === token &&
Gameboard[5] === token) ||
(Gameboard[6] === token &&
Gameboard[7] === token &&
Gameboard[8] === token) ||
(Gameboard[0] === token &&
Gameboard[3] === token &&
Gameboard[6] === token) ||
(Gameboard[1] === token &&
Gameboard[4] === token &&
Gameboard[7] === token) ||
(Gameboard[2] === token &&
Gameboard[5] === token &&
Gameboard[8] === token) ||
(Gameboard[0] === token &&
Gameboard[4] === token &&
Gameboard[8] === token) ||
(Gameboard[0] === token &&
Gameboard[1] === token &&
Gameboard[2] === token) ||
(Gameboard[2] === token && Gameboard[4] === token && Gameboard[6] === token)
) {
displayController().showsWinner();
return true;
} else {
return false;
}
};
Gameboard.checkForDraw = () => {
// If there are no moves available it returns false otherwise it returns true
for (let i = 0; i < Gameboard.length - 1; i++) {
if (Gameboard.indexOf("") !== -1) {
return true; // Movements are available
} else {
return false; //No moves available
}
}
};
const createPlayer = (name, token) => {
return { name, token };
};
const displayController = () => {
const showResetMessage = () => {
document.querySelector(".turn-message").textContent =
"Click on Reset or Settings for to play again!";
};
const startMessage = () => {
document.querySelector(".turn-message").textContent =
"Click on Settings for to play!";
};
const showsGameBoard = () => {
for (let i = 0; i < 9; i++) {
const cell = document.querySelector(`#cell-${[i]}`);
if (Gameboard[i] === "X") {
Gameboard[i] = `X`;
}
if (Gameboard[i] === "O") {
Gameboard[i] = `O`;
}
cell.innerHTML = Gameboard[i];
if (Gameboard[i] === "X") {
cell.innerHTML = '<i class="fa-solid fa-x"></i>';
}
if (Gameboard[i] === "O") {
cell.innerHTML = '<i class="fa-regular fa-circle"></i>';
}
}
};
const showsWinner = () => {
message.textContent = `${Game().getCurrentPlayer().name} wins!`;
displayController().showResetMessage();
cells.forEach((cell) => cell.removeEventListener("click", handleCellClick)); // Stop listener on cells if there are winner
};
const showsTie = () => {
const message = document.querySelector(".message");
message.textContent = `The Game Was Tied`;
displayController().startMessage();
};
const showsTurn = () => {
let randomPos = Math.round(Math.random() * 8);
if (
Game().getCurrentPlayer().name === "Mr. Robot" &&
Gameboard[randomPos] !== playerOne.token &&
Gameboard[randomPos] === "" &&
Gameboard[randomPos] !== playerTwo.token
) {
Gameboard[randomPos] = playerTwo.token;
Game().switchPlayerTurn();
displayController().showsGameBoard();
Gameboard.checkForWin(playerTwo.token);
if (
Gameboard.checkForDraw() === false &&
Gameboard.checkForWin(playerTwo.token) === false
) {
displayController().showsTie();
}
if (Gameboard.checkForWin(playerTwo.token) === true && Gameboard.checkForDraw() === true) {
message.textContent = `${playerTwo.name} Wins!`;
return;
}
} else {
turnMessage.textContent = `${
Game().getCurrentPlayer().name
}'s turn with token "${Game().getCurrentPlayer().token}" `;
}
if (Gameboard[randomPos] !== "") {
showsTurn(); // Recursive call
}
return randomPos;
};
const toggleTheme = () => {
ambientBtn.addEventListener("click", () => {
ambientBtn.className === "fa fa-sun"
? (ambientBtn.classList = "fa fa-moon")
: (ambientBtn.classList = "fa fa-sun");
document.body.classList.toggle("dark");
header.classList.toggle("header-dark");
settings.classList.toggle("btn-dark");
resetBtn.classList.toggle("btn-dark");
turn.classList.toggle("turn-dark");
message.classList.toggle("message-dark");
footer.classList.toggle("footer-dark");
});
};
const toggleDialog = () => {
const closeBtn = document.querySelector("#closeBtn");
const settingsDialog = document.querySelector("#settingsDialog");
const settingsBtn = document.querySelector(".settings-btn");
settingsBtn.addEventListener("click", () => {
settingsDialog.showModal();
turnMessage.textContent = "";
gameMode.value = "";
playerOneNameInput.value = "";
playerOneTokenInput.value = "";
playerTwoNameInput.value = "";
Game().currentPlayer = {};
Gameboard.reset();
displayController().cleanMessage();
displayController().showsGameBoard();
});
closeBtn.addEventListener("click", () => {
settingsDialog.close();
});
};
const togglePlayerTwoOptions = () => {
const gameMode = document.querySelector("#game-mode");
gameMode.addEventListener("change", () => {
if (gameMode.value === "player-machine") {
document.querySelector("#machine-group").style.visibility = "hidden";
document.querySelector("#player-name-two").removeAttribute("required");
} else {
document.querySelector("#player-name-two").setAttribute("required", "");
document.querySelector("#machine-group").style.visibility = "visible";
}
});
};
const cleanMessage = () => {
message.textContent = "";
};
const resetDisplay = () => {
resetBtn.addEventListener("click", () => {
Gameboard.reset();
displayController().showsGameBoard();
displayController().showsTurn();
displayController().cleanMessage();
});
};
return {
startMessage,
showsGameBoard,
showsWinner,
showsTurn,
showsTie,
toggleTheme,
toggleDialog,
togglePlayerTwoOptions,
showResetMessage,
cleanMessage,
resetDisplay,
};
};
let playerOne = {};
let playerTwo = {};
let currentPlayer = {};
let players = [];
const Game = () => {
form.addEventListener("submit", (e) => {
e.stopImmediatePropagation();
if (gameMode.value === "player-player") {
playerOne = createPlayer(
playerOneNameInput.value,
playerOneTokenInput.value
);
playerTwo = createPlayer(
playerTwoNameInput.value,
playerOne.token === "X" ? "O" : "X"
);
players = [playerOne, playerTwo];
currentPlayer = players[0];
displayController().showsTurn();
return currentPlayer;
}
if (gameMode.value === "player-machine") {
playerOne = createPlayer(
playerOneNameInput.value,
playerOneTokenInput.value
);
playerTwo = createPlayer(
"Mr. Robot",
playerOne.token === "X" ? "O" : "X"
);
players = [playerOne, playerTwo];
currentPlayer = players[0];
displayController().showsTurn();
return currentPlayer;
}
});
const switchPlayerTurn = () => {
currentPlayer = currentPlayer === players[0] ? players[1] : players[0];
};
const getCurrentPlayer = () => Game().currentPlayer;
if (gameMode.value !== "") {
resetBtn.removeAttribute("disabled"); // Enabled reset button
cells.forEach((cell) => cell.addEventListener("click", handleCellClick)); // Add click listener to the cells
} else {
resetBtn.setAttribute("disabled", ""); // Disabled reset button
}
return { switchPlayerTurn, getCurrentPlayer, currentPlayer };
};
(() => {
displayController().toggleTheme();
displayController().toggleDialog();
displayController().togglePlayerTwoOptions();
displayController().startMessage();
Game();
displayController().resetDisplay();
})();