-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
414 lines (346 loc) · 14.4 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
/**
* MAIN QUEST
* [ ] Determine Victory / Defeat / Null
* [ ] Choose color
* [X] EACH TURN: tell which player should play
* [ ] Animation ( disc fall from the sky )
* [ ] Cancel the last move before validating
*
* STARTING
* [X] Create container
* [X] Create rows and columns
* [X] Parameter can take x, y
* [ ] check x >= 7 && y >= 6
* [ ] check discColor1 isColor && discColor2 isColor
* [X] Parameter can take colors
*
* DURING GAME
* [X] Check click is not on already played case
* [ ] Mousenter / mouseleave: color trace
*
* BONUS
* [ ] Choose between Com OR Player
* [ ] Computer A.I
* [ ] Display color disk next to name of the player who should play and make the piece turn (just like tatami galaxy op)
* [ ] sound effect on click: ( naruto yooo~ )
* [ ] SSB voice: GAME ! ( when losing or winning )
*/
$.fn.connectFour = function(options) {
if (typeof options === 'object') {
var settings = $.extend({
rows: 7,
cols: 6,
discColor1: 'red',
discColor2: 'yellow',
mode: 'duo' // or 'solo'
}, options );
} else {
throw "Parameters are incorrect!";
}
/**
* Create Menu
* Close it on click and make it reappear when restart
*
*/
$('.start-button').on('click', function() {
/**
* hide only if color are not the same
* Create cols only after the button start click
* ( Make create of grid a function )
* Give color value to call function
*/
let $mainMenu = $('.main-menu-center');
/* TEST COLOR: NOT WORKING
settings.discColor1 = $('[name=p1-color]').val();
settings.discColor2 = $('[name=p2-color]').val();
console.log(settings.discColor1);
console.log(settings.discColor2);
*/
if (settings.discColor1 !== settings.discColor2) {
$mainMenu.hide();
$disc.css({'background': settings.discColor1});
sessionStorage.setItem('p1', 0);
sessionStorage.setItem('p2', 0);
}
});
/**
* Create Grid ( rows and cols )
*/
$( 'script' ).before( '<div class="grid"></div>' );
let $grid = $( '.grid' );
for (let i = settings.rows; i > 0 ; i--) {
$grid.append(`<div class="row" data-row="${i}"></div>`);
let $lastRowCreated = $(`div[data-row=${i}]`);
for (let j = 1; j <= settings.cols; j++) {
$lastRowCreated.append(`<div class="col empty" data-row="${i}" data-col="${j}"></div>`);
}
}
/**
* Vertical check for last empty cell
*/
function lastEmptyCol(rows, col) {
let $cells = $(`[data-col=${col}]`);
let $lastEmptyCol;
$.each($cells, function(index, cell) {
if (!cell.classList.contains('empty') || cell.dataset.row - 1 === '1') {
return false;
}
$lastEmptyCol = cell;
});
return $lastEmptyCol;
}
/**
* Game Menu
* Create bar, title, current disc color...
*/
$( '.grid' ).before("<div class=\"bar\"></div>");
let $bar = $('.bar');
$bar.append("<h1 class=\"who-play font-effect-fire-animation\">This is your turn </h1>");
$bar.append("<div class=\"disc\"></div>");
$bar.after("<div class=\"game-menu\"></div>");
let $gameMenu = $('.game-menu');
$('.game-menu').append("<h2 class=\"winner font-effect-fire-animation\"></h2>");
$gameMenu.append("<p class=\"p1-score\"></p>");
$gameMenu.append("<p class=\"p2-score\"></p>");
$gameMenu.append("<p class=\"null-score\"></p>");
$gameMenu.append("<button class=\"again\"><i class=\"fas fa-gamepad\"></i></button>");
$gameMenu.append("<button class=\"home\"><i class=\"fas fa-home\"></i></button>");
let $p1Score = $('.p1-score');
let $p2Score = $('.p2-score');
let $nullCount = $('.null-score');
var $disc = $('.disc');
/**
* after each clicked check whether the player has won or not
*/
function checkGame(params) {
/**
* Check top left - bottom right
* Check top right - bottom left
* @param bool alt is you want the top right - bottom left check
*/
function diagonalDirection(params, alt = false) {
let playerColor = params.playerColor;
let discsCount = 1;
let discColIndex = ( alt ) ? params.discCol : params.discCol - 2;
let thisDisc = params.thisDisc;
if (thisDisc.parentElement.previousElementSibling !== null) {
var currentDisc = thisDisc.parentElement.previousElementSibling.children[discColIndex];
} else {
var currentDisc = undefined;
}
while (currentDisc !== undefined && currentDisc.getAttribute('style') === `background: ${playerColor};`) {
discsCount++;
alt ? discColIndex++ : discColIndex--;
if ( currentDisc.parentElement.previousElementSibling ) {
currentDisc = currentDisc.parentElement.previousElementSibling.children[discColIndex];
} else {
break;
}
}
// reset index
discColIndex = ( alt ) ? params.discCol - 2 : params.discCol;
if ( thisDisc.parentElement.nextElementSibling ) {
currentDisc = thisDisc.parentElement.nextElementSibling.children[discColIndex];
} else {
currentDisc = undefined;
}
while (currentDisc !== undefined && currentDisc.getAttribute('style') === `background: ${playerColor};`) {
discsCount++;
alt ? discColIndex-- : discColIndex++;
if ( currentDisc.parentElement.nextElementSibling ) {
currentDisc = currentDisc.parentElement.nextElementSibling.children[discColIndex];
} else {
break;
}
}
return discsCount;
}
/**
* Main function for diagonal checking
*/
function checkDiagonal(params) {
if ( diagonalDirection(params) >= 4 || diagonalDirection(params, true) >= 4 ) {
return true;
} else {
return false;
}
}
function checkCol(params) {
let playerColor = params.playerColor;
let discsCount = 1;
let col = params.discCol - 1;
let currentDisc = params.thisDisc;
let thisDisc = currentDisc.parentElement.previousElementSibling;
while (thisDisc !== null && thisDisc.children[col].getAttribute('style') === `background: ${playerColor};`) {
discsCount++;
if (thisDisc.previousElementSibling !== null) {
thisDisc = thisDisc.previousElementSibling;
} else {
break;
}
}
thisDisc = currentDisc.parentElement.nextElementSibling;
while (thisDisc !== null && thisDisc.children[col].getAttribute('style') === `background: ${playerColor};`) {
discsCount++;
if (thisDisc.nextElementSibling !== null) {
thisDisc = thisDisc.nextElementSibling;
} else {
break;
}
}
return ( discsCount >= 4 ) ? true : false;
}
function checkRow(params) {
let discsCount = 1;
let playerColor = params.playerColor;
let thisDisc = params.thisDisc;
let currentDisc = params.thisDisc.previousElementSibling;
while (currentDisc !== null && currentDisc.getAttribute('style') === `background: ${playerColor};`) {
discsCount++;
if ( currentDisc.previousElementSibling !== null) {
currentDisc = currentDisc.previousElementSibling;
} else {
break;
}
}
currentDisc = params.thisDisc.nextElementSibling;
while (currentDisc !== null && currentDisc.getAttribute('style') === `background: ${playerColor};`) {
discsCount++;
if (currentDisc.nextElementSibling !== null) {
currentDisc = currentDisc.nextElementSibling;
} else {
break;
}
}
return ( discsCount >= 4 ) ? true : false;
}
if (checkRow(params) || checkCol(params) || checkDiagonal(params)) {
return true;
}
return false;
}
/**
* DURING GAME
* EACH TURN ( on click ):
* [X] Change player
* [X] Check column
* [X] Check game status ( vicotry / defeat / null )
* [X] Count disk to detect null ? ( and tell which turn it is )
* [X] Change title to Puissance 4 when in main menu
*/
let player1WinCount = 0;
let player2WinCount = 0;
let gameNullCount = 0;
var playerColor = settings.discColor1;
var counter = 0;
const totalCells = settings.rows * settings.cols;
var lastMove = null;
$( $grid ).on('click', '.col.empty', function() {
counter++;
const col = $( this ).data("col");
let $emptyCol = lastEmptyCol(settings.rows, col);
const row = $( $emptyCol ).data('row');
lastMove = $emptyCol;
$emptyCol.classList.remove('empty');
$emptyCol.style.background = playerColor;
let gameStatus = checkGame(
{
playerColor: playerColor,
thisDisc: $emptyCol,
discRow: row,
discCol: col,
rows: settings.rows,
cols: settings.cols
}
);
if (gameStatus) {
let winner = (playerColor === settings.discColor1 ? 1 : 2);
$('.winner').html(`Player ${winner} won the game !`);
playerColor === settings.discColor1 ? player1WinCount++ : player2WinCount++;
/**
* Update scoreboard
* Create discs item for p1, p2, nullCount
*/
sessionStorage.setItem('p1', player1WinCount);
sessionStorage.setItem('p2', player2WinCount);
sessionStorage.setItem('null', gameNullCount);
$p1Score.html(sessionStorage.getItem('p1'));
$p2Score.html(sessionStorage.getItem('p2'));
$nullCount.html(sessionStorage.getItem('null'));
$p1Score.html(`<span class=\"disc p1\"></span>`);
$('.disc.p1').css({background: settings.discColor1});
$('.disc.p1').html(sessionStorage.getItem('p1'));
$p2Score.html('<span class=\"disc p2\"></span>');
$('.disc.p2').css({background: settings.discColor2});
$('.disc.p2').html(sessionStorage.getItem('p2'));
$nullCount.html('<span class=\"disc null\"></span>');
$('.disc.null').html(sessionStorage.getItem('null'));
$gameMenu.show();
console.log(sessionStorage.getItem('p1'));
console.log(sessionStorage.getItem('p2'));
}
if ( counter >= totalCells ) {
gameNullCount++;
$('.winner').html(`No winner !`);
sessionStorage.setItem('p1', player1WinCount);
sessionStorage.setItem('p2', player2WinCount);
sessionStorage.setItem('null', gameNullCount);
$p1Score.html(sessionStorage.getItem('p1'));
$p2Score.html(sessionStorage.getItem('p2'));
$nullCount.html(sessionStorage.getItem('null'));
$p1Score.html(`<span class=\"disc p1\"></span>`);
$('.disc.p1').css({background: settings.discColor1});
$('.disc.p1').html(sessionStorage.getItem('p1'));
$p2Score.html('<span class=\"disc p2\"></span>');
$('.disc.p2').css({background: settings.discColor2});
$('.disc.p2').html(sessionStorage.getItem('p2'));
$nullCount.html('<span class=\"disc null\"></span>');
$('.disc.null').html(sessionStorage.getItem('null'));
$gameMenu.show();
}
playerColor = ( playerColor === settings.discColor1 ) ? settings.discColor2 : settings.discColor1;
$disc.css({'background': playerColor});
});
/**
* Remove last move
*/
// $('.disc').on('click', function() {
// if (lastMove !== null && counter > 0) {
// playerColor = $( lastMove ).css('background');
// console.log(playerColor);
// counter--;
// }
// });
/**
* Game Menu interactivity
* return to home menu and reset grid
*/
$('.home').on('click', function() {
let $mainMenu = $('.main-menu-center');
player1WinCount = 0;
player2WinCount = 0;
counter = 0;
$.each($('.col'), function(key, value) {
$( value ).removeAttr('style').addClass("empty");
});
$gameMenu.hide();
$mainMenu.show();
});
/**
* Game Menu: Play again button
*/
$('.again').on('click', function() {
playerColor = ( playerColor === settings.discColor1 ) ? settings.discColor1 : settings.discColor2;
$disc.css({'background': playerColor});
counter = 0;
$.each($('.col'), function(key, value) {
$( value ).removeAttr('style').addClass("empty");
});
$gameMenu.hide();
});
};
$('body').connectFour( {rows: 6, cols: 7, discColor1: 'red', discColor2: 'yellow'} );
// $('.grid').on('click', '.col', function() {
// // $(this)[0].parentNode.dataset.row
// console.log( $(this)[0].dataset.row, $(this)[0].dataset.col );
// });