-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4.js
417 lines (349 loc) · 13.6 KB
/
connect4.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
/////////////////////////////////////////////////////////////////////////////////
// CONNECT 4 - OO Version by: Etienne Deneault //
// //
// Player 1 and 2 alternate turns. On each turn, a piece is dropped down a //
// column until a player gets four -in -a - row(horiz, vert, or diag) or until //
// board fills(tie) //
/////////////////////////////////////////////////////////////////////////////////
// SELECTORS //
const startButton = document.querySelector('#start');
const resetButton = document.querySelector('#reset');
const p1Button = document.getElementById('p1-color');
const p2Button = document.getElementById('p2-color');
const compButton = document.getElementById('comp-color');;
const introContainer = document.querySelector('.intro-container');
const introDiv = document.createElement('div');
const introText = document.createElement('p');
const endContainer = document.querySelector('.end-container');
const endDiv = document.createElement('div');
const endText = document.createElement('p');
const sbBackground = document.querySelector('#sb-background');
const shortGameBoard = document.querySelector('#short-game');
const longGameBoard = document.querySelector('#long-game');
let shortGame = JSON.parse(localStorage.getItem("short-game"));
let longGame = JSON.parse(localStorage.getItem("long-game"));
const timeUpdate = document.querySelector('#current-time');
const snd = new Audio("Dragon-roar-sound.mp3"); // buffers automatically when created
// CLASSES Game, Player and Computer extends Player //
class Game {
constructor(p1, p2, width = 7, height = 6, min = 0) {
this.players = [p1, p2];
this.width = width;
this.height = height;
this.min = min;
this.currPlayer = p1;
this.setScoreboard();
this.startTime();
this.makeBoard();
this.makeHtmlBoard();
this.gameOver = false;
}
setScoreboard() {
getItemsStorage();
// COMMENT: set scoreboard values for the scoreboard mark-up from local storage
shortGameBoard.innerText = shortGame;
longGameBoard.innerText = longGame;
// COMMENT: if scoreboard values empty set 0 to local storage and the mark-up
// else set values from local storage to the mark-up
if (localStorage.getItem("short-game") === null) {
localStorage.setItem('short-game', JSON.stringify(0));
shortGame = 0;
shortGameBoard.innerText = shortGame;
};
if (localStorage.getItem("long-game") === null) {
localStorage.setItem('long-game', JSON.stringify(0));
longGame = 0;
longGameBoard.innerText = longGame;
};
};
makeBoard() {
this.board = [];
for (let y = 0; y < this.height; y++) {
this.board.push(Array.from({ length: this.width }));
}
}
makeHtmlBoard() {
const gameContainer = document.querySelector('#game'); // COMMENT: get the mark-up div element with id "board"
const table = document.createElement("table");
table.classList.add('col-sm-8', 'col-12', 'mx-auto');
table.setAttribute("id", "board");
gameContainer.append(table);
// COMMENT: Creates the top part of the game board, sets "id" property and eventListener
const board = document.getElementById("board");
const top = document.createElement("tr");
top.setAttribute("id", "column-top");
this.handleGameClick = this.handleClick.bind(this); // important bind here
top.addEventListener('click', this.handleGameClick);
for (let x = 0; x < this.width; x++) {
const headCell = document.createElement("td");
headCell.setAttribute("id", x);
top.append(headCell);
}
table.append(top);
// make main part of board
for (let y = 0; y < this.height; y++) {
const row = document.createElement("tr");
for (let x = 0; x < this.width; x++) {
const cell = document.createElement("td");
cell.setAttribute("id", `${y}-${x}`);
row.append(cell);
};
board.append(row); // COMMENT: append the row to the mark-up game board
}
}
findSpotForCol(x) {
for (let y = this.height - 1; y >= 0; y--) {
if (!this.board[y][x]) {
return y;
}
}
return null;
}
placeInTable(y, x) {
const pieceDiv = document.createElement('div');
pieceDiv.classList.add("gamePiece");
pieceDiv.style.backgroundColor = this.currPlayer.color;
pieceDiv.innerText = '*'
pieceDiv.style.top = -50 * (y + 2);
const pieceLoc = document.getElementById(`${y}-${x}`);
pieceDiv.innerHTML = '<h1 class="star">☆</h1>';
try {
pieceLoc.append(pieceDiv);
}
catch (e) {
alert('oops... something went wrong, please reset the game.')
return;
}
}
endGame(msg) {
clearInterval(this.time);
snd.play(); // Play Winning Sound Effect
// Update the localStorage if shortest or longest game values pass logic test
if (parseInt(timeUpdate.innerText) > parseInt(longGame) || parseInt(longGame) === 0) {
longGame = timeUpdate.innerText;
longGameBoard.innerText = longGame;
localStorage.setItem('long-game', JSON.stringify(parseInt(longGame)));
}
if (parseInt(timeUpdate.innerText) < parseInt(shortGame) || parseInt(shortGame) === 0) {
shortGame = timeUpdate.innerText;
shortGameBoard.innerText = shortGame;
localStorage.setItem('short-game', JSON.stringify(parseInt(shortGame)));
}
// Reset time on the scoreboard
this.timeOutput = 0;
timeUpdate.innerText = (this.timeOutput.toString());
document.querySelector('#board').remove();
gameStart = 0;
endDiv.classList.add('container', 'col-12', 'col-sm-8', 'msg');
endContainer.append(endDiv);
endDiv.append(endText);
// End of game message
endText.innerText = msg;
// COMMENT: remove end message after 3 sec.
setTimeout(function () {
endText.parentNode.remove();
introDiv.classList.add('container', 'col-12', 'col-sm-4', 'msgIntro');
introContainer.append(introDiv);
introDiv.append(introText);
introText.innerText = `Choose your Settings and Click the START Button`;
}, 3000);
clearInterval(this.time);
}
handleClick(evt) {
// Condition to determine if this instance of Game is a 2 player game //
if (this.currPlayer.name === "Player-One" || this.currPlayer.name === "Player-Two") {
const x = +evt.target.id;
const y = this.findSpotForCol(x);
if (y === null) {
return;
}
this.board[y][x] = this.currPlayer; // set identifier in memory
this.placeInTable(y, x); // add piece to the mark-up
// Check for tie //
if (this.board.every(row => row.every(cell => cell))) {
return this.endGame('Tie!');
}
// COMMENT: check for win //
if (this.checkForWin()) {
this.gameOver = true;
return this.endGame(`${this.currPlayer.name} wins!`);
};
// Player Switch and change the bg color of the scoreboard
this.currPlayer = this.currPlayer === this.players[0] ? this.players[1] : this.players[0];
sbBackground.style.backgroundColor = this.currPlayer.color;
}
// Using a Promise to build a delay function (flow control)
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Check if the Game is against the Computer Player //
sleep(1000).then(() => {
if (this.currPlayer.name === "Computer-Player") {
const top = document.querySelector("#column-top");
top.removeEventListener("click", this.handleGameClick); // Remove the top row event listener during the Computers Play
const x = this.getRandomArbitrary(); // Provide pseudo-random value
const y = this.findSpotForCol(x); // find value of y for the piece placement
if (y === null) {
return;
}
this.board[y][x] = this.currPlayer; // update board with Player identifier in position [x,y]
this.placeInTable(y, x); // Add to mark-up
// Check for tie //
if (this.board.every(row => row.every(cell => cell))) {
return this.endGame('Tie!');
}
// check for win //
if (this.checkForWin()) {
this.gameOver = true;
return this.endGame(`${this.currPlayer.name} wins!`);
};
// Switch Player effectively going back to the Player One's trun without having a click event for Computer Player //
this.currPlayer = this.currPlayer === this.players[0] ? this.players[1] : this.players[0];
sbBackground.style.backgroundColor = this.currPlayer.color; //change backgrond color of the scoreboardd
this.handleGameClick = this.handleClick.bind(this); // important bind here
top.addEventListener('click', this.handleGameClick); // add the event listener back to the top row for the P1 turn.
}
else {return} // ToDo something should be here for error handling
});
}
checkForWin() {
const _win = cells =>
// Check four cells to see if they're all color of current player
// - cells: list of four (y, x) cells
// - returns true if all are legal coordinates & all match currPlayer
// Determines if a winning state is true //
cells.every(
([y, x]) =>
y >= 0 &&
y < this.height &&
x >= 0 &&
x < this.width &&
this.board[y][x] === this.currPlayer
);
// Nested loop to traverse 2D array //
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
// create 4 winning game states//
const horiz = [[y, x], [y, x + 1], [y, x + 2], [y, x + 3]];
const vert = [[y, x], [y + 1, x], [y + 2, x], [y + 3, x]];
const diagDR = [[y, x], [y + 1, x + 1], [y + 2, x + 2], [y + 3, x + 3]];
const diagDL = [[y, x], [y + 1, x - 1], [y + 2, x - 2], [y + 3, x - 3]];
// find winner (only checking each win-possibility as needed)
if (_win(horiz) || _win(vert) || _win(diagDR) || _win(diagDL)) {
return true;
}
}
}
}
// Helper Functions //
startTime() {
let start = Date.now();
this.time = setInterval(function () {
let delta = Date.now() - start;
this.timeOutput = (Math.floor(delta / 1000)); // in seconds
timeUpdate.innerText = (this.timeOutput.toString());
}, 1000);
};
getRandomArbitrary() {
this.min = Math.ceil(this.min);
this.max = Math.floor(this.height);
return Math.floor(Math.random() * (this.max - this.min + 1)) + this.min;
// return Math.floor(Math.random() * (this.height - this.min) + this.min);
}
}
//Player class extended by Computer class
class Player {
constructor(color, name) {
this.color = color;
this.name = name;
}
}
class Computer extends Player {
constructor(color, name, height=7, min = 0 ) {
super(color, name);
this.height = height;
this.min = min;
}
}
// Global Functions //
function init() {
gameStart = 0;
p1Button.value = 'rgba(217, 83, 79, 100%)';
p2Button.value = 'rgba(240, 173, 78, 100%)';
compButton.value = 'rgba(91, 192, 222, 100%)';
}
// Function to set localStorage //
function getItemsStorage() {
let shortGame = JSON.parse(localStorage.getItem("short-game"));
let longGame = JSON.parse(localStorage.getItem("long-game"));
}
// Function to add colorPicker
function pickColor() {
const playerOne = p1Button,
popupP1 = new Picker({
parent: playerOne,
popup: 'bottom',
color: 'rgba(255, 0, 0, .5)',
editorFormat: 'rgb',
onDone: function(color) {
playerOne.style.backgroundColor = color.rgbaString;
playerOne.style.borderColor = color.rgbaString;
sbBackground.style.backgroundColor = color.rgbaString;
playerOne.value = color.rgbaString;
},
});
const playerTwo = p2Button,
popupP2 = new Picker({
parent: playerTwo,
popup: 'bottom',
color: 'rgba(34, 54, 170, 0.8)',
editorFormat: 'rgb',
onDone: function(color) {
playerTwo.style.backgroundColor = color.rgbaString;
playerTwo.style.borderColor = color.rgbaString;
sbBackground.style.backgroundColor = color.rgbaString;
playerTwo.value = color.rgbaString;
},
});
}
//Function to display the intro message //
function introMessage() {
introDiv.classList.add('container', 'col-12', 'col-sm-12', 'pb-2', 'pt-3', 'msgIntro');
introDiv.style.opacity = '1';
introContainer.append(introDiv);
introDiv.append(introText);
introText.innerText = `Choose your Settings and Click the START Button`;
}
////////////////////
// MAIN GAME //
////////////////////
init();
getItemsStorage();
introMessage();
pickColor();
// OnClick-start: add event to create a Game instance //
document.querySelector('#start').addEventListener('click', (e) => {
if (gameStart === 0) {
gameStart++;
introDiv.remove();
let p1 = new Player(p1Button.value, p1Button.name);
let p2 = new Player(p2Button.value, p2Button.name);
new Game(p1, p2);
}
else {return}
});
// OnClick - force page reload, resetting game //
resetButton.addEventListener('click', (e) => {
location.reload();
});
// OnClick - creates game instanc with playerOne and Computer as a player //
compButton.addEventListener('click', (e) => {
if (gameStart === 0) {
gameStart++;
introDiv.remove();
let p1 = new Player(p1Button.value, p1Button.name);
let p2 = new Computer(compButton.value, compButton.name);
new Game(p1, p2);
}
else {return}
});