-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
575 lines (436 loc) · 13.5 KB
/
Copy pathlogic.js
File metadata and controls
575 lines (436 loc) · 13.5 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// console.log("Hello World");
// Declare our variable for our 2d array, score, row, and columns.
let board;
let score = 0;
let rows = 4;
let columns = 4;
let is2048Exist = false;
let is4096Exist = false;
let is8192Exist = false;
// create a function to set a game
// start of setaGame()
function setGame(){
// Initialize the 4x4 game board with all tiles set to 0
board =[
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
];
// Create the gameboard on the HTML document
// 0 < 4 (Y)
// 1 < 4 (Y)
// 2 < 4 (Y)
// 3 < 4 (Y)
// 4 < 4 (N)
// First loop is to create rows, second loop is to create columns
// inner loop will be executed to finish before outer loop
for(let r=0; r < rows; r++){
for(let c=0; c < columns; c++){
//console.log(`[r${r}-c${c}]`)
// create div element representing a tile
let tile = document.createElement("div");
// set a unique id for each tile based on its coordinate
// "+" is used to concatenata values if dealing with String.
tile.id = r.toString() + "-" + c.toString();
// get the number from the board
// wherein the board is currently set to 0
let num = board[r][c];
// Update the tiles appearance based on the value.
updateTile(tile, num);
//
document.getElementById("board").append(tile);
}
}
// Random tile
setTwo();
setTwo();
}
function updateTile(tile, num){
//clear the tile innerText
tile.innerText = "";
// clear the classList to avoid multiple classes
tile.classList.value = "";
// add class named "tile" to the classlist of the tile, for the styling
tile.classList.add("tile");
// to check if the current num parameter is not zero
if(num > 0){
// set the tile's text to the number based on the num value.
tile.innerText = num.toString();
if(num<=4096){
tile.classList.add("x"+num.toString());
}
else{
// if number is greater than 4096, a special class "x8192" will be added.
tile.classList.add("x8192");
// end of updateTile()
}
}
}
// start of window.onload
// event that triggers when a webpage finished loading
window.onload = function(){
// function
setGame();
// end of setGame()
}
// end of window.onload
// start of handleSlide()
// e representst the event object, which contains information about the event occured.
function handleSlide(e){
// check the keydown event.
console.log(e.code);
// Check if the pressed key's code is one of the arrow keys.
if(["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(e.code)){
// prevent default behavior, to avoid scrolling on keydown.
e.preventDefault();
// (=) assignment operator (to assign/change value of a variable), (==) to compare if value from left to right are equal.
if(e.code == "ArrowLeft"){
slideLeft();
setTwo();
}
else if(e.code == "ArrowRight"){
slideRight();
setTwo();
}
else if(e.code == "ArrowUp"){
slideUp();
setTwo();
}
else if(e.code == "ArrowDown"){
slideDown();
setTwo();
}
document.getElementById("Score").innerText = score;
// use setTimeout to delay the alert
setTimeout(()=>{
if(hasLost()){
alert("Game Over! You have lost the game. Game will restart.")
restartGame();
alert("Press any arrow key to restart.")
}
checkWin();
}, 100); //delay time in miliseconds
}
}
//EventListener, when any key is pressed the handleSlide() is called to handle the key press.
document.addEventListener("keydown", handleSlide);
// end of handleSlide()
// start of filterZero(tiles)
// removing empty tiles
function filterZero(tiles){
// create new array removing the zeroes
return tiles.filter(num => num != 0);
}
// end of filterZero(tiles)
// start of slide(tiles)
// function for sliding and merging tiles
function slide(tiles) {
// [0,2,2,2]
// [2,2,2]
tiles = filterZero(tiles);
for(let i = 0; i < tiles.length; i++){
// if two adjacent numbers are equal.
if(tiles[i] == tiles [i+1]){
// merge them by doubling the first one
tiles[i] *= 2;
// set the second one to zero
tiles[i+1] = 0;
// result: [2,2,2] -> [4,0,2]
// score = score + tiles[i];
// short hand version
score += tiles[i];
}
}
// [4,0,2] -> [4,2]
tiles = filterZero(tiles)
// add zeroes back
while(tiles.length < 4){
// add zero on the end of the array
tiles.push(0);
// [4,2,0,0]
}
return tiles;
}
// end of slide(tiles)
// start of slideLeft()
function slideLeft(){
for(let r=0; r<rows; r++){
// store current row in the row variable
let row = board[r]; // 0: [0,2,2,2]
// line for animation
// copy the content of the original row
let originalRow = row.slice();
// slide() function will return to a new value for a specific row. (merging of tiles)
row = slide(row);
// updated value in the board.
board[r] = row;
for(let c=0; c<columns; c++){
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
// line for animation
// if current tile != to the original tile, apply animation
if(originalRow[c] !== num && num !== 0){
// specify the animation style and duration
tile.style.animation = "slide-from-right 0.3s"
//remove the animation class after animation is complete
setTimeout(()=> {
tile.style.animation = "";
}, 300);
}
updateTile(tile, num);
}
}
}
// end of slideLeft()
// start of slideRight()
function slideRight(){
for(let r=0; r<rows; r++){
// store current row in the row variable
let row = board[r]; // 0: [0,2,2,2]
// original row for animation
let originalRow = row.slice();
// reverse the row array since it is sliding to right
// r = 0: [0,2,2,2] -> [2,2,2,0]
row.reverse();
// slide() function will return to a new value for a specific row. (merging of tiles)
row = slide(row); // [4,2.0.0]
row.reverse(); // [0,0,2,4]
// updated value in the board.
board[r] = row;
for(let c=0; c<columns; c++){
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
// line for animation
// if current tile != to the original tile, apply animation
if(originalRow[c] !== num && num !== 0){
// specify the animation style and duration
tile.style.animation = "slide-from-left 0.3s"
//remove the animation class after animation is complete
setTimeout(()=> {
tile.style.animation = "";
}, 300);
}
updateTile(tile, num);
}
}
}
// end of slideRight()
// start of slideUp()
function slideUp(){
for(let c=0; c<columns; c++){
// create a temporary array col that represents the column from top to bottom
let col =[board[0][c], board[1][c], board[2][c], board[3][c]];
// original col animation
let originalCol = col.slice();
col =slide(col);
for(let r=0; r<rows; r++){
// set the values of board array back to the values of the modified col.
board[r][c] = col[r];
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
if(originalCol[r] != num && num !== 0){
tile.style.animation = "slide-from-bottom 0.3s";
setTimeout(() =>{
tile.style.animation = "";
}, 300)
}
updateTile(tile, num);
}
}
}
// end of slideUp()
// start of slideDown()
function slideDown(){
for(let c=0; c<columns; c++){
// create a temporary array col that represents the column from top to bottom
let col =[board[0][c], board[1][c], board[2][c], board[3][c]];
// copy for animation
let originalCol = col.slice();
col.reverse();
col =slide(col);
col.reverse();
for(let r=0; r<rows; r++){
// set the values of board array back to the values of the modified col.
board[r][c] = col[r];
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
if(originalCol[r] != num && num !== 0){
tile.style.animation = "slide-from-top 0.3s";
setTimeout(() =>{
tile.style.animation = "";
}, 300)
}
updateTile(tile, num);
}
}
}
// end of slideDown()
// start of hasEmptyTile()
// check whether game board contains any empty space (0)
// return a boolean value (true/false)
function hasEmptyTile() {
for(let r=0; r<rows; r++){
for(let c=0; c<columns; c++){
// Check if current tile == 0, if yes it will return true
if(board[r][c] == 0){
return true;
}
}
}
// no tile == 0
return false;
}
// end of hasEmptyTile()
// start of setTwo()
// add a new random "2" tile in the game board.
function setTwo(){
// check if hasEmptyTile is false
// ! - opposite of boolean
if(!hasEmptyTile()){
return;
}
// declare a value found in tile
let found = false;
while(!found){
// Math.random() - generates random number base on the given condition
// Math.floor() - rounds down to the nearest integer.
// to get a random value for r and c from 1 - 4.
let r = Math.floor(Math.random() * rows);
let c = Math.floor(Math.random() * columns);
// if position value is 0, set the value to 2.
if(board[r][c] == 0){
board[r][c] = 2;
let tile = document.getElementById(r.toString()+"-"+c.toString());
tile.innerText = "2";
tile.classList.add("x2");
// set the found variable to tre.
found = true;
}
}
}
// end of setTwo()
// start of checkWin()
function checkWin() {
for(let r=0; r<rows; r++){
for(let c=0; c<columns; c++){
// check if the current tile is a winning tile
if(board[r][c] == 2048 && is2048Exist == false){
alert("You Win! You got 2048.")
is2048Exist = true; // once 2048 already eist alert message will only pop once.
}
else if(board[r][c] == 4096 && is4096Exist == false){
alert("You are unstoppable at 4096! You are fantastically unstoppable");
is4096Exist = true;
}
else if(board[r][c] == 8192 && is8192Exist == false){
alert("Victory! You have reached 8192! You are incredibly awesome!");
is8192Exist = true;
}
}
}
}
// end of checkWin()
// start of hasLost()
// check if the board is full
function hasLost(){
for(let r=0; r<rows; r++){
for(let c=0; c<columns; c++){
// found an empty tile, user has not lost
if(board[r][c] == 0){
return false;
}
const currentTile = board[r][c];
// check if the adjacent (up, down, left, right) cells have possible merge
if(
r > 0 && board[r - 1][c] === currentTile ||
r < rows - 1 && board[r + 1][c] === currentTile ||
c > 0 && board[r][c - 1] === currentTile ||
c < columns - 1 && board[r][c + 1] === currentTile
){
// Found adjacent cells with the same value, user has not lost.
return false;
}
}
}
// No possible moves left or empty tiles, user lost
return true;
}
// end of hasLost()
// start of restartGame()
// RestartGame by replacing all values into zero.
function restartGame(){
board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
];
setTwo(); // new tile
score = 0;
}
// end of restartGame()
// for mobile devices
// declare variable for touch input
let startX = 0;
let startY = 0;
// event listener to capture touch in the screen and assign the and y coordinates in the start and startY varibale.
document.addEventListener("touchstart", (e) =>{
startX = e.touches[0].clientX;
startY = e.touches[0].clientY;
// console.log(startX);
// console.log(startY);
})
// event listener to check where you touch the screen and prevents scrolling
// input targets any elements that includes the word tile
document.addEventListener("touchmove", (e) =>{
if(!e.target.className.includes("tile")){
return
}
e.preventDefault(); // disables scrolling
}, {passive: false});
// listen for the touchend event on the entire document.
document.addEventListener("touchend", (e) =>{
// check if the element triggered has a class name"tile"
if(!e.target.className.includes("tile")){
return; // exit the function.
}
// calculate the difference between the initial position and the final touch.
let diffX = startX - e.changedTouches[0].clientX;
let diffY = startY - e.changedTouches[0].clientY;
console.log(diffX);
console.log(diffY);
// Check if the swipe is for horizontal or vertical.
// horizontal > vertical
if(Math.abs(diffX) > Math.abs(diffY)){
// horizontal swipe
if(diffX > 0){
slideLeft();
setTwo();
}
else{
slideRight();
setTwo()
}
}
else{
// vertical swipe
if(diffY > 0){
slideUp();
setTwo()
}
else{
slideDown();
setTwo();
}
}
document.getElementById("Score").innerText = score;
setTimeout(()=>{
if(hasLost()){
alert("Game Over! You have lost the game. Game will restart.")
restartGame();
alert("Press any arrow key to restart.")
}
checkWin();
}, 100); //delay time in miliseconds
})