-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
423 lines (360 loc) · 11.3 KB
/
scripts.js
File metadata and controls
423 lines (360 loc) · 11.3 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
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
const gameModel = (() => {
let gameStart = false;
let activePlayer;
let bot = false;
let gameover = false;
let gameboard = [];
const winningMoves = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
const getGameboardData = () => gameboard;
const setGameboardData = (value) => gameboard.push(value);
const resetGameboardData = () => gameboard.length = 0;
const getWinningMoves = () => winningMoves;
const getActivePlayer = () => activePlayer;
const setActivePlayer = (value) => {
activePlayer = value;
};
const getBot = () => bot;
const setBot = (value) => {
bot = value;
};
const getGameStart = () => gameStart;
const setGameStart = (value) => (gameStart = value);
const getGameover = () => gameover;
const setGameover = (value) => (gameover = value);
return {
getGameboardData,
setGameboardData,
getWinningMoves,
getActivePlayer,
setActivePlayer,
getBot,
setBot,
getGameStart,
setGameStart,
getGameover,
setGameover,
resetGameboardData,
};
})();
const gameView = (() => {
const boardContainer = document.querySelector("#gameboard-container");
const boardSquares = document.querySelectorAll(".square");
const botToggle = document.querySelector(".switch-button-checkbox");
const gameDisplay = document.querySelector("#game-display");
const player1Input = document.querySelector(".player1-input");
const player2Input = document.querySelector(".player2-input");
const markerPointer = document.querySelector(".marker-pointer");
const startBtn = document.querySelector("#start-btn");
const restartBtn = document.querySelector("#restart-btn");
const overlay = document.querySelector('#overlay');
const overlayNotification = document.querySelector('.overlay-notification');
const overlayBtn = document.querySelector('.overlay-btn');
const addMarkerToSquare = (square, marker) =>
(square.innerHTML = `<span class="marker">${marker}</span>`);
const displayMessage = (message) => {
gameDisplay.textContent = message;
};
const displayOverlayMessage = (message) => {
overlayNotification.textContent = message;
}
const togglePersonInput = () => {
player2Input.value = "";
player2Input.disabled = false;
}
const toggleBotInput = () => {
player2Input.value = "Bot";
player2Input.disabled = true;
}
const setPointerToPlayer1 = () => {
markerPointer.classList.remove("pointer-position2");
markerPointer.classList.add("pointer-position1");
}
const setPointerToPlayer2 = () => {
markerPointer.classList.remove("pointer-position1");
markerPointer.classList.add("pointer-position2");
}
const triggerConfetti = () => {
// do this for 30 seconds
let duration = 2 * 1000;
let end = Date.now() + duration;
(function frame() {
// launch a few confetti from the left edge
confetti({
particleCount: 5,
angle: 60,
spread: 55,
origin: { x: 0 },
});
// and launch a few from the right edge
confetti({
particleCount: 5,
angle: 120,
spread: 55,
origin: { x: 1 },
});
// keep going until we are out of time
if (Date.now() < end) {
requestAnimationFrame(frame);
}
})();
};
return {
boardContainer,
boardSquares,
botToggle,
gameDisplay,
displayOverlayMessage,
player1Input,
player2Input,
setPointerToPlayer1,
setPointerToPlayer2,
startBtn,
restartBtn,
addMarkerToSquare,
triggerConfetti,
displayMessage,
toggleBotInput,
togglePersonInput,
overlay,
overlayNotification,
overlayBtn
};
})();
//Controller
const gameController = (() => {
const createPlayer = (name, marker) => {
return { name, marker };
};
const toggleActivePlayer = () => {
if (gameModel.getActivePlayer() === player2) {
gameView.setPointerToPlayer1();
gameModel.setActivePlayer(player1);
} else {
gameView.setPointerToPlayer2()
gameModel.setActivePlayer(player2);
}
};
const disableElement = (element) => element.classList.add('disable');
const enableElement = (element) => element.classList.remove('disable');
const disableAllElements = (elements) => {
elements.forEach((element) => {
disableElement(element);
});
};
const disableAllSquaresTemp = () => {
gameView.boardSquares.forEach((element) => {
element.classList.add('disable-temp');
});
};
const enableAllSquaresTemp = () => {
gameView.boardSquares.forEach((element) => {
element.classList.remove('disable-temp');
});
};
const enableAllElements = (elements) => {
elements.forEach((element) => {
enableElement(element);
});
};
const clearSquareDisplay = () => {
gameView.boardSquares.forEach((square) => {
square.innerHTML = '';
});
}
const unlightSquareDisplay = () => {
gameView.boardSquares.forEach((square) => {
square.classList.remove("lightup");
});
};
const lightWinningSquares = (winningSquares) => {
winningSquares.forEach((id) => {
gameView.boardSquares[id].classList.add("lightup");
});
};
const checkWinner = () => {
const xBoardMoves = gameModel
.getGameboardData()
.map((index) => index)
.filter((items) => items.marker === "X");
const xBoardMovesMap = xBoardMoves.map((index) => index.square);
const oBoardMoves = gameModel
.getGameboardData()
.map((index) => index)
.filter((items) => items.marker === "O");
const oBoardMovesMap = oBoardMoves.map((index) => index.square);
//Checks both arrays for winning combos
const xHasWinningCombination = gameModel
.getWinningMoves()
.some((winningCombination) =>
winningCombination.every((value) => xBoardMovesMap.includes(value))
);
const xFindWinningCombination = gameModel
.getWinningMoves()
.find((winningCombination) =>
winningCombination.every((value) => xBoardMovesMap.includes(value))
);
const oHasWinningCombination = gameModel
.getWinningMoves()
.some((winningCombination) =>
winningCombination.every((value) => oBoardMovesMap.includes(value))
);
const oFindWinningCombination = gameModel
.getWinningMoves()
.find((winningCombination) =>
winningCombination.every((value) => oBoardMovesMap.includes(value))
);
//Returns results of matching items
if (xHasWinningCombination) {
disableAllElements(gameView.boardSquares);
lightWinningSquares(xFindWinningCombination);
gameView.triggerConfetti();
gameView.displayMessage(`🎉 ${player1.name} Wins!`);
gameView.displayOverlayMessage(`🎉 ${player1.name} Wins!`);
gameModel.setGameover(true);
gameView.overlay.classList.remove('hide');
}
if (oHasWinningCombination) {
disableAllElements(gameView.boardSquares);
lightWinningSquares(oFindWinningCombination);
gameView.triggerConfetti();
gameView.displayMessage(`🎉 ${player2.name} Wins!`);
gameView.displayOverlayMessage(`🎉 ${player2.name} Wins!`);
gameModel.setGameover(true);
gameView.overlay.classList.remove('hide');
}
if (
!oHasWinningCombination &&
gameModel.getGameboardData().length >= 9 &&
!xHasWinningCombination &&
gameModel.getGameboardData().length >= 9
) {
gameView.overlay.classList.remove('hide');
gameModel.setGameover(true);
gameView.displayMessage(`💩 Tie Game!`);
gameView.displayOverlayMessage(`💩 Tie Game!`);
}
};
const botTurn = () => {
gameView.setPointerToPlayer2()
if (gameModel.getGameover() === true) {
return;
}
disableAllSquaresTemp()
setTimeout(function() {
//your code here
const moves = gameModel.getGameboardData().map((x) => x.square);
const totalMoves = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const filteredMoves = totalMoves.filter((x) => moves.indexOf(x) === -1);
const randomIndex = Math.floor(Math.random() * filteredMoves.length);
const randomMove = filteredMoves[randomIndex];
gameModel.setGameboardData({ square: randomMove, marker: "O" });
gameView.boardSquares[randomMove].innerHTML =
'<span class="marker">O</span>';
disableElement(gameView.boardSquares[randomMove]);
gameModel.setActivePlayer(player1);
checkWinner();
gameView.setPointerToPlayer1()
enableAllSquaresTemp()
}, 1000);
};
const startGame = () => {
player1.name = gameView.player1Input.value
player2.name = gameView.player2Input.value
disableElement(gameView.player1Input);
disableElement(gameView.player2Input);
disableElement(gameView.botToggle)
disableElement(gameView.startBtn)
enableAllElements(gameView.boardSquares);
gameModel.setGameStart(true)
};
const restartGame = () => {
//Reset Model
gameModel.setGameboardData();
gameModel.setActivePlayer(player1);
gameModel.setBot(false);
gameModel.setGameStart(false);
gameModel.setGameover(false);
gameModel.resetGameboardData();
//Reset View
clearSquareDisplay();
unlightSquareDisplay();
gameView.togglePersonInput()
gameView.setPointerToPlayer1()
gameView.player1Input.value = "";
gameView.player2Input.value = "";
gameView.gameDisplay.innerHTML = "Enter Name's"
//Reset Controller
enableElement(gameView.player1Input);
enableElement(gameView.player2Input);
enableElement(gameView.startBtn)
gameView.botToggle.checked = false;
enableElement(gameView.botToggle);
disableAllElements(gameView.boardSquares);
}
//BUTTONS ----------------------
//Bot Toggle Button
gameView.botToggle.addEventListener("click", () => {
gameModel.setBot(gameView.botToggle.checked);
if (gameView.botToggle.checked)
{
gameView.toggleBotInput();
} else {
gameView.togglePersonInput()
}
});
//Start Button
gameView.startBtn.addEventListener("click", (e) => {
e.preventDefault();
startGame();
});
//Reset Button
gameView.restartBtn.addEventListener("click", (e) => {
restartGame();
});
gameView.overlayBtn.addEventListener('click', () => {
gameView.overlay.classList.add('hide');
})
//Gameboard Buttons
gameView.boardSquares.forEach((square, index) => {
square.addEventListener("click", () => {
if (gameModel.getBot()) {
gameModel.setGameboardData({
square: index,
marker: gameModel.getActivePlayer().marker,
});
gameView.addMarkerToSquare(square, gameModel.getActivePlayer().marker);
checkWinner();
botTurn();
console.log("if", square);
disableElement(square);
} else {
gameModel.setGameboardData({
square: index,
marker: gameModel.getActivePlayer().marker,
});
gameView.addMarkerToSquare(square, gameModel.getActivePlayer().marker);
toggleActivePlayer();
checkWinner();
console.log("else", square);
disableElement(square);
}
});
});
return {
createPlayer,
disableAllElements
};
})();
const player1 = gameController.createPlayer("Player 1", "X");
const player2 = gameController.createPlayer("Player 2", "O");
gameModel.setActivePlayer(player1);
gameController.disableAllElements(gameView.boardSquares)