-
Notifications
You must be signed in to change notification settings - Fork 3
/
2048.js
1238 lines (1079 loc) · 36.5 KB
/
2048.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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global GameAssets: true */
(function() {
"use strict";
// TODO: Change animation calculations to include time for non-janky animation
var Metrics = (function() {
var PIXEL_RATIO = (function () {
//noinspection JSUnresolvedVariable
var ctx = document.createElement("canvas").getContext("2d"),
dpr = window.devicePixelRatio || 1,
bsr = ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1;
return dpr / bsr;
})();
var body = document.getElementById("body");
var Metrics = {
createCanvas: function(width, height) {
// Pixel Density for Devices - http://stackoverflow.com/a/15666143/372743
var pixelRatio = this.getPixelRatio();
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
canvas.width = width * pixelRatio;
canvas.height = height * pixelRatio;
canvas.style.width = (width + "px");
canvas.style.height = (height + "px");
// Seems to fuck up the iOS UIWebView by quartering the canvas
// context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
return canvas;
},
getPixelRatio: function() {
return PIXEL_RATIO;
},
getSize: function() {
return Math.min(this.getWidth(), this.getHeight());
},
getSlideSpeed: function() {
return (this.getSize() / 25) * this.getPixelRatio();
},
getGrowthSpeed: function() {
return (this.getSize() / 100) * this.getPixelRatio();
},
getHeaderSize: function() {
return Math.floor(this.getSize() / 8.33) * this.getPixelRatio();
},
getResetSize: function() {
return Math.floor(this.getSize() / 30) * this.getPixelRatio();
},
getTileSize: function(width, height) {
return Math.floor(Math.min(width, height) / 1.94);
},
getBorder: function() {
return (this.getSize() / 40) * this.getPixelRatio();
},
getWidth: function() {
var e = document.documentElement,
width = window.innerWidth || e.clientWidth || body.clientWidth;
return width;
},
getHeight: function() {
var e = document.documentElement,
height = window.innerHeight|| e.clientHeight|| body.clientHeight;
return height;
},
getCellWidth: function(canvas, grid) {
return (canvas.width - (Metrics.getBorder() * (grid.width + 1))) / grid.width;
},
getCellHeight: function(canvas, grid) {
return (canvas.height - (this.getBorder() * (grid.height + 1))) / grid.height;
}
};
// public interface
return {
createCanvas: function(width, height) {
return Metrics.createCanvas(width, height);
},
getPixelRatio: function() {
return Metrics.getPixelRatio();
},
getSize: function() {
return Metrics.getSize();
},
getSlideSpeed: function() {
return Metrics.getSlideSpeed();
},
getGrowthSpeed: function() {
return Metrics.getGrowthSpeed();
},
getHeaderSize: function() {
return Metrics.getHeaderSize();
},
getResetSize: function() {
return Metrics.getResetSize();
},
getTileSize: function(width, height) {
return Metrics.getTileSize(width, height);
},
getBorder: function() {
return Metrics.getBorder();
},
getWidth: function() {
return Metrics.getWidth();
},
getHeight: function() {
return Metrics.getHeight();
},
getCellWidth: function(canvas, grid) {
return Metrics.getCellWidth(canvas, grid);
},
getCellHeight: function(canvas, grid) {
return Metrics.getCellHeight(canvas, grid);
}
};
})();
var Input = (function() {
// Log key presses
var KEY_TO_COMMAND = {
"37": "LEFT",
"38": "UP",
"39": "RIGHT",
"40": "DOWN"};
var direction;
// Handle normal keypress events
addEventListener("keydown", function(event) {
// Only prevent the default action for valid moves
if (!!KEY_TO_COMMAND[event.keyCode]) {
event.preventDefault();
}
direction = KEY_TO_COMMAND[event.keyCode];
});
// Handle touch moves
var startCoordinates = { x: 0, y: 0 };
addEventListener("touchstart", function(event) {
// Needed to prevent undefined behavior on Android devices
event.preventDefault();
event.changedTouches = event.changedTouches || [{ pageX: 0, pageY: 0 }];
var touch = event.changedTouches[0];
startCoordinates.x = touch.pageX;
startCoordinates.y = touch.pageY;
});
addEventListener("touchend", function(event) {
// Needed to prevent undefined behavior on Android devices
event.preventDefault();
event.changedTouches = event.changedTouches || [{ pageX: 0, pageY: 0 }];
var touch = event.changedTouches[0];
// Get the horizontal and vertical differences
var horizontal = startCoordinates.x - touch.pageX;
var vertical = startCoordinates.y - touch.pageY;
// Clear the start/stop coordinates
startCoordinates.x = 0;
startCoordinates.y = 0;
// If the horizontal change was greater than the vertical change, horizontal swipe
if (Math.abs(horizontal) > Math.abs(vertical)) {
if (horizontal > 0) {
// Swiped left
direction = "LEFT";
} else {
// Swiped right
direction = "RIGHT";
}
} else {
// Otherwise vertical swipe
if (vertical > 0) {
// Swiped down
direction = "UP";
} else {
// Swiped up
direction = "DOWN";
}
}
});
var Input = {
getCommand: function() {
var command = direction;
direction = undefined;
return command;
},
resetClick: function(callback) {
var immediateCallback = function(event) {
event.preventDefault();
// Remove the event listeners
removeEventListener("click", immediateCallback);
removeEventListener("touchend", immediateCallback);
// Execute the callback
callback();
};
addEventListener("click", immediateCallback);
addEventListener("touchend", immediateCallback);
}
};
// Public interface
return {
getCommand: function() {
return Input.getCommand();
},
resetClick: function(callback) {
Input.resetClick(callback);
}
};
})();
var Animation = (function() {
var Animation = {
animate: function(canvas, context, grid) {
// Used to determine if we need to block user input for the duration of an animation
var blockInput = false;
for (var x = 0; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
var tile = grid.tiles[x][y];
var mergedTile = grid.mergedTiles[x][y];
if (!!tile) {
if (tile.triggerGrowth || !!tile.growing) {
this.animateGrowth(canvas, context, tile);
}
if (tile.triggerSlide || !!tile.sliding) {
this.animateSliding(canvas, context, tile);
blockInput = true;
}
}
if (!!mergedTile) {
if (mergedTile.triggerSlide || !!mergedTile.sliding) {
this.animateSliding(canvas, context, mergedTile);
blockInput = true;
}
}
}
}
return blockInput;
},
animateGrowth: function(canvas, context, tile) {
var MODIFIER = { GROW: "GROW", SHRINK: "SHRINK" };
var GROWTH_SPEED = Metrics.getGrowthSpeed();
if (tile.growing === undefined) {
// If we are not yet animating, setup the data
tile.growing = {
modifier: MODIFIER.GROW,
value: GROWTH_SPEED,
maxSize: 15,
minSize: 0
};
} else if (tile.growing.modifier === MODIFIER.GROW) {
// Increase the growth size
tile.growing.value += GROWTH_SPEED;
// If we've reached the max growth, start shrinking
if (tile.growing.value >= tile.growing.maxSize) {
tile.growing.modifier = MODIFIER.SHRINK;
}
} else if (tile.growing.modifier === MODIFIER.SHRINK) {
tile.growing.value -= GROWTH_SPEED;
// If we've shrunk back to normal, remove animation data
if (tile.growing.value <= tile.growing.minSize) {
// Remove the trigger value
delete tile.triggerGrowth;
delete tile.growing;
}
}
},
animateSliding: function(canvas, context, tile) {
var SLIDE_RATE = Metrics.getSlideSpeed();
if (tile.sliding === undefined) {
tile.sliding = { value: 0 };
} else {
var slidingData = tile.slidingData;
// Slide rate should be multiplied by number of cells moved across
switch (slidingData.direction) {
case "UP":
case "DOWN":
SLIDE_RATE += (Math.abs(slidingData.startY - slidingData.endY) * SLIDE_RATE);
break;
case "LEFT":
case "RIGHT":
SLIDE_RATE += (Math.abs(slidingData.startX - slidingData.endX) * SLIDE_RATE);
break;
}
tile.sliding.value += SLIDE_RATE;
}
}
};
// Public interface
return {
animate: function(canvas, context, grid) {
return Animation.animate(canvas, context, grid);
}
};
})();
var Renderer = (function() {
var assets = GameAssets.getClassicPackage();
var Renderer = {
render: function(canvas, context, grid) {
// Draw the background
context.fillStyle = assets.backgroundColor;
context.fillRect(0, 0, canvas.width, canvas.height);
var cellHeight = Metrics.getCellHeight(canvas, grid);
var cellWidth = Metrics.getCellWidth(canvas, grid);
// Draw the empty cells
this.drawCells(context, grid, cellWidth, cellHeight);
// Draw the merged tiles that are still animating
this.drawMergedTiles(context, grid, cellWidth, cellHeight);
// Draw tiles in their proper cells
this.drawTiles(context, grid, cellWidth, cellHeight);
},
renderScores: function(canvas, context, currentScore, topScore) {
var topScoreCoordinates = this.drawTopScore(canvas, context, topScore);
this.drawCurrentScore(canvas, context, topScoreCoordinates, currentScore);
},
showGameOver: function(canvas, context) {
this.showOverlay(canvas, context, assets.gameOver.backgroundColor);
// Draw "Game Over!" text
context.font = "bold " + Metrics.getHeaderSize() + "px Helvetica Neue";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = assets.gameOver.textColor;
context.fillText("Game Over!", (canvas.width / 2), (canvas.height / 2), canvas.width);
// Draw "Try again" button
this.showRestart(canvas, context);
},
showGameWon: function(canvas, context) {
this.showOverlay(canvas, context, assets.gameWon.backgroundColor);
// Draw "Game Won!" message
context.font = "bold " + Metrics.getHeaderSize() + "px Helvetica Neue";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = assets.gameWon.textColor;
context.fillText("You win!", (canvas.width / 2), (canvas.height / 2), canvas.width);
// Add "Try Again" button
this.showRestart(canvas, context);
},
roundedRectangle: function(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo((x + radius), y);
context.lineTo((x + width - radius), y);
context.quadraticCurveTo((x + width), y, (x + width), (y + radius));
context.lineTo((x + width), (y + height - radius));
context.quadraticCurveTo((x + width), (y + height), (x + width - radius), (y + height));
context.lineTo((x + radius), (y + height));
context.quadraticCurveTo(x, (y + height), x, (y + height - radius));
context.lineTo(x, (y + radius));
context.quadraticCurveTo(x, y, (x + radius), y);
context.fill();
context.closePath();
},
showOverlay: function(canvas, context, fill) {
// Draw semi-transparent background over the entire grid
context.fillStyle = fill;
context.fillRect(0, 0, canvas.width, canvas.height);
},
showRestart: function(canvas, context) {
var fontSize = Metrics.getResetSize();
var buttonWidth = Math.floor(fontSize * 6.67);
var buttonHeight = Math.floor(buttonWidth / 2.5);
var margin = Math.floor(buttonWidth / 10);
var buttonX = (canvas.width / 2) - (buttonWidth / 2);
var buttonY = (canvas.height / 2) + 50;
// Draw the rectangle button
context.fillStyle = assets.restart.backgroundColor;
this.roundedRectangle(context, buttonX, buttonY, buttonWidth, buttonHeight, 4);
// Show the message
context.font = "bold " + fontSize + "px Helvetica Neue";
context.textAlign = "center";
context.textAlign = "middle";
context.fillStyle = assets.restart.textColor;
context.fillText("Play Again?", buttonX + (buttonWidth / 2), buttonY + (buttonHeight / 2), buttonWidth - (margin * 2));
},
drawCells: function(context, grid, cellWidth, cellHeight) {
for (var x = 0; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
var coordinates = this.getCellCoordinates(grid, cellWidth, cellHeight, x, y);
this.drawCell(context, coordinates, cellWidth, cellHeight);
}
}
},
drawCell: function(context, coordinates, width, height) {
context.fillStyle = assets.cellBackgroundColor;
this.roundedRectangle(context, coordinates.x, coordinates.y, width, height, 5);
},
drawTiles: function(context, grid, cellWidth, cellHeight) {
// Iterate over all tiles and render on screen
for (var x = 0; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
if (!!grid.tiles[x][y]) {
var coordinates = this.getCellCoordinates(grid, cellWidth, cellHeight, x, y);
this.drawTile(context, grid, grid.tiles[x][y], coordinates, cellWidth, cellHeight);
}
}
}
},
drawTile: function(context, grid, tile, coordinates, width, height) {
var x = coordinates.x;
var y = coordinates.y;
// Handle tile sliding animation
if (!!tile.sliding) {
var direction = tile.slidingData.direction;
var value = tile.sliding.value;
var startCoordinates = this.getCellCoordinates(grid, width, height, tile.slidingData.startX, tile.slidingData.startY);
var stopCoordinates = this.getCellCoordinates(grid, width, height, tile.slidingData.endX, tile.slidingData.endY);
if (direction === "UP") {
y = startCoordinates.y - value;
y = (y <= stopCoordinates.y) ? stopCoordinates.y : y;
} else if (direction === "DOWN") {
y = startCoordinates.y + value;
y = (y >= stopCoordinates.y) ? stopCoordinates.y : y;
} else if (direction === "LEFT") {
x = startCoordinates.x - value;
x = (x <= stopCoordinates.x) ? stopCoordinates.x : x;
} else if (direction === "RIGHT") {
x = startCoordinates.x + value;
x = (x >= stopCoordinates.x) ? stopCoordinates.x : x;
}
if ((direction === "UP" && y <= stopCoordinates.y) ||
(direction === "DOWN" && y >= stopCoordinates.y) ||
(direction === "LEFT" && x <= stopCoordinates.x) ||
(direction === "RIGHT" && x >= stopCoordinates.x)) {
delete tile.sliding;
delete tile.slidingData;
delete tile.triggerSlide;
}
}
// Handle growth animation
if (!!tile.growing) {
var growth = (!!tile.growing && !tile.sliding) ? tile.growing.value : 0;
x -= (growth / 2);
y -= (growth / 2);
width += growth;
height += growth;
}
// Fill in the cell with the proper background color
context.fillStyle = assets.tileBackgroundColor[tile.value];
this.roundedRectangle(context, x, y, width, height, 5);
// Draw the tile value in the center of the cell
var textX = x + (width / 2);
var textY = y + (height / 2);
context.font = "bold " + Metrics.getTileSize(width, height) + "px Helvetica Neue";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = assets.tileTextColor[tile.value];
// Allow for 10px of padding on the bounding box
context.fillText(tile.value, textX, textY, width - 10);
},
drawMergedTiles: function(context, grid, cellWidth, cellHeight) {
// Iterate over the merged tiles and draw them on screen
for (var x = 0; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
if (!!grid.mergedTiles[x][y]) {
var tile = grid.mergedTiles[x][y];
var coordinates = this.getCellCoordinates(grid, cellWidth, cellHeight, tile.slidingData.endX,
tile.slidingData.endY);
this.drawTile(context, grid, tile, coordinates, cellWidth, cellHeight);
// If the tile is done sliding, remove it from the merged tile grid
if (!tile.sliding) {
grid.mergedTiles[x][y] = undefined;
}
}
}
}
},
getCellCoordinates: function(grid, cellWidth, cellHeight, row, column) {
// For some reason, row/column are sometimes a string which fucks the calculations
row = parseInt(row, 10);
column = parseInt(column, 10);
// Convert the row/column number into x/y coordinates on the actual canvas
return {
x: (row * cellWidth) + ((row + 1) * Metrics.getBorder()),
y: (column * cellHeight) + ((column + 1) * Metrics.getBorder())
};
},
drawCurrentScore: function(canvas, context, topScoreCoordinates, score) {
// TODO: Use font-metrics to determine size/positioning
var margin = 10;
var width = 180;
var height = canvas.height - (margin * 2);
var x = topScoreCoordinates.x - (width + margin);
var y = margin;
// Draw the score box
context.fillStyle = assets.score.backgroundColor;
this.roundedRectangle(context, x, y, width, height, 4);
// Draw the "Score:" label
context.fillStyle = assets.score.titleColor;
context.font = "bold 24px Helvetica Neue";
context.textAlign = "center";
context.textBaseline = "top";
context.fillText("Score:", x + (width / 2), y + 5, width);
// Draw the current score
context.fillStyle = assets.score.valueColor;
context.font = "bold 36px Helvetica Neue";
context.textBaseline = "bottom";
context.fillText(score, x + (width / 2), y + height - 5, width);
return {
x: 0,
y: 0
};
},
drawTopScore: function(canvas, context, score) {
// TODO: Use font-metrics to determine size/positioning
var margin = 10;
var width = 180;
var height = canvas.height - (margin * 2);
var x = canvas.width - width;
var y = margin;
// Draw the top score box
context.fillStyle = assets.score.backgroundColor;
this.roundedRectangle(context, x, y, width, height, 4);
// Draw the "Top Score:" text
context.fillStyle = assets.score.titleColor;
context.font = "bold 24px Helvetica Neue";
context.textAlign = "center";
context.textBaseline = "top";
context.fillText("Top Score:", x + (width / 2), y + 5, width);
// Draw the score under the heading
context.fillStyle = assets.score.valueColor;
context.font = "bold 36px Helvetica Neue";
context.textBaseline = "bottom";
context.fillText(score, x + (width / 2), y + height - 5, width);
return {
x: x,
y: y
};
}
};
// Public interface
return {
render: function(canvas, context, grid) {
return Renderer.render(canvas, context, grid);
},
renderScores: function(canvas, context, currentScore, topScore) {
return Renderer.renderScores(canvas, context, currentScore, topScore);
},
showGameOver: function(canvas, context) {
Renderer.showGameOver(canvas, context);
},
showGameWon: function(canvas, context) {
Renderer.showGameWon(canvas, context);
}
};
})();
var Movement = (function() {
var Movement = {
move: function(direction, grid) {
if (!!direction) {
var action = function(grid) {};
switch (direction) {
case "UP":
action = this.moveUp;
break;
case "DOWN":
action = this.moveDown;
break;
case "LEFT":
action = this.moveLeft;
break;
case "RIGHT":
action = this.moveRight;
break;
}
return action(grid);
}
return false;
},
canMove: function(direction, grid) {
switch (direction) {
case "UP":
return this.canMoveUp(grid);
case "DOWN":
return this.canMoveDown(grid);
case "LEFT":
return this.canMoveLeft(grid);
case "RIGHT":
return this.canMoveRight(grid);
}
return false;
},
canMoveUp: function(grid) {
// Check if there is an open space above any tile
for (var x = 0; x < grid.width; x++) {
// Skip `y = 0` because the top row cannot move up
for (var y = 1; y < grid.height; y++) {
// Check if there is a tile in the current coordinates
if (!!grid.tiles[x][y]) {
// If the cell above is unoccupied, we can move up
if (!grid.tiles[x][y-1]) {
return true;
} else if (grid.tiles[x][y].value === grid.tiles[x][y-1].value) {
// If the tile in the cell above, we can move up and combine
return true;
}
}
}
}
return false;
},
canMoveDown: function(grid) {
// Check if we can move downward
for (var x = 0; x < grid.width; x++) {
// Skip `y = grid.height` since the bottom cannot move down
for (var y = 0; y < grid.height - 1; y++) {
// Check if there is a tile in the current cell
if (!!grid.tiles[x][y]) {
// We can move down in the below cell is empty
if (!grid.tiles[x][y+1]) {
return true;
} else if (grid.tiles[x][y].value === grid.tiles[x][y+1].value) {
// We can move down if the below tile has the same value
return true;
}
}
}
}
return false;
},
canMoveLeft: function(grid) {
// Check if we can move to the left
// Skip `x = 0` because the leftmost cannot move left
for (var x = 1; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
if (!!grid.tiles[x][y]) {
// We're fine if the cell to the left is empty
if (!grid.tiles[x-1][y]) {
return true;
} else if (grid.tiles[x][y].value === grid.tiles[x-1][y].value) {
// We're also fine if the tile to the left is the same as this one
return true;
}
}
}
}
return false;
},
canMoveRight: function(grid) {
// Check if we can move to the right
// Skip `x = grid.width` since the rightmost cannot move right
for (var x = 0; x < grid.width - 1; x++) {
for (var y = 0; y < grid.height; y++) {
if (!!grid.tiles[x][y]) {
// Fine if the cell to the right is empty
if (!grid.tiles[x+1][y]) {
return true;
} else if (grid.tiles[x][y].value === grid.tiles[x+1][y].value) {
// Fine if the tile to the right is the same value as the current one
return true;
}
}
}
}
return false;
},
moveUp: (function() {
var getTopCell = function(column, currentY) {
return column.reduceRight(function(top, cell, index) {
return (index <= currentY && !cell) ? index : top;
}, currentY);
};
var getBottomTile = function(column, currentY) {
return column.reduce(function(bottom, cell, index) {
// If we're not yet below the current tile, skip
if (index <= currentY) { return undefined; }
// If a tile has not been found and we have a tile, return index
else if (!bottom && !!cell) { return index; }
// Otherwise return the already found tile
else { return bottom; }
}, undefined);
};
return function(grid) {
if (Movement.canMoveUp(grid)) {
var score = { score: 0 };
for (var x = 0; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
if (!!grid.tiles[x][y]) {
var currentTile = grid.tiles[x][y];
var topCellIndex = getTopCell(grid.tiles[x], y);
var bottomTileIndex = getBottomTile(grid.tiles[x], y);
Movement.attemptMove(grid, "UP", x, y, x, topCellIndex);
var mergeScore = Movement.attemptMerge(grid, "UP", x, currentTile.y, x, bottomTileIndex);
if (!!mergeScore && isFinite(mergeScore.score)) {
score.score += mergeScore.score;
}
}
}
}
return score;
}
return false;
};
})(),
moveDown: (function() {
var getBottomCell = function(column, currentY) {
return column.reduce(function(bottom, cell, index) {
return (index >= currentY && !cell) ? index : bottom;
}, currentY);
};
var getTopTile = function(column, currentY) {
return column.reduceRight(function(top, cell, index) {
// If we're not above the current tile, skip
if (index >= currentY) { return undefined; }
// If we don't have a top and we have a tile, return index
else if (!top && !!cell) { return index; }
// Otherwise return current tile
else { return top; }
}, undefined);
};
return function(grid) {
if (Movement.canMoveDown(grid)) {
var score = { score: 0 };
for (var x = 0; x < grid.width; x++) {
for (var y = (grid.height - 1); y >= 0; y--) {
if (!!grid.tiles[x][y]) {
var currentTile = grid.tiles[x][y];
var bottomCellIndex = getBottomCell(grid.tiles[x], y);
var topTileIndex = getTopTile(grid.tiles[x], y);
Movement.attemptMove(grid, "DOWN", x, y, x, bottomCellIndex);
var mergeScore = Movement.attemptMerge(grid, "DOWN", x, currentTile.y, x, topTileIndex);
if (!!mergeScore && isFinite(mergeScore.score)) {
score.score += mergeScore.score;
}
}
}
}
return score;
}
return false;
};
})(),
moveLeft: (function() {
var getLeftmostCell = function(grid, currentX, currentY) {
return grid.tiles.reduceRight(function(left, row, index) {
return (index <= currentX && !row[currentY]) ? index : left;
}, currentX);
};
var getRightTile = function(grid, currentX, currentY) {
return grid.tiles.reduce(function(right, row, index) {
// If we're still left of the current tile, skip
if (index <= currentX) { return undefined; }
// If we do not have a right and have a tile, we've found it
else if (!right && !!row[currentY]) { return index; }
// Otherwise keep passing the current right
else { return right; }
}, undefined);
};
return function(grid) {
if (Movement.canMoveLeft(grid)) {
var score = { score: 0 };
for (var y = 0; y < grid.height; y++) {
for (var x = 0; x < grid.width; x++) {
if (!!grid.tiles[x][y]) {
var currentTile = grid.tiles[x][y];
var leftCellIndex = getLeftmostCell(grid, x, y);
var rightTileIndex = getRightTile(grid, x, y);
Movement.attemptMove(grid, "LEFT", x, y, leftCellIndex, y);
var mergeScore = Movement.attemptMerge(grid, "LEFT", currentTile.x, y, rightTileIndex, y);
if (!!mergeScore && isFinite(mergeScore.score)) {
score.score += mergeScore.score;
}
}
}
}
return score;
}
return false;
};
})(),
moveRight: (function() {
var getRightCell = function(grid, currentX, currentY) {
return grid.tiles.reduce(function(right, row, index) {
return (index >= currentX && !row[currentY]) ? index : right;
}, currentX);
};
var getLeftTile = function(grid, currentX, currentY) {
return grid.tiles.reduceRight(function(left, row, index) {
// If we are right of the current tile, skip
if (index >= currentX) { return undefined; }
// If we do not yet have a left tile and we have found one, return it
else if (!left && !!row[currentY]) { return index; }
// Otherwise, keep passing the found left
else { return left; }
}, undefined);
};
return function(grid) {
if (Movement.canMoveRight(grid)) {
var score = { score: 0 };
for (var y = 0; y < grid.height; y++) {
for (var x = (grid.width - 1); x >= 0; x--) {
if (!!grid.tiles[x][y]) {
var currentTile = grid.tiles[x][y];
var rightCellIndex = getRightCell(grid, x, y);
var leftTileIndex = getLeftTile(grid, x, y);
Movement.attemptMove(grid, "RIGHT", x, y, rightCellIndex, y);
var mergeScore = Movement.attemptMerge(grid, "RIGHT", currentTile.x, y, leftTileIndex, y);
if (!!mergeScore && isFinite(mergeScore.score)) {
score.score += mergeScore.score;
}
}
}
}
return score;
}
return false;
};
})(),
setupSlide: function(grid, direction, startX, startY, endX, endY) {
var tile = grid.tiles[startX][startY];
tile.sliding = { value: 0 };
tile.triggerSlide = true;
tile.slidingData = {
direction: direction,
startX: startX,
startY: startY,
endX: endX,
endY: endY
};
},
attemptMove: function(grid, direction, currentX, currentY, newX, newY) {
if (isFinite(currentX) && isFinite(currentY) && isFinite(newX) && isFinite(newY)) {
if (currentX !== newX || currentY !== newY) {
var currentTile = grid.tiles[currentX][currentY];
currentTile.x = newX;
currentTile.y = newY;
// Setup data needed for slide animation
this.setupSlide(grid, direction, currentX, currentY, newX, newY);
grid.tiles[newX][newY] = currentTile;
grid.tiles[currentX][currentY] = undefined;
return true;
}
}
return false;
},
attemptMerge: function(grid, direction, currentX, currentY, neighborX, neighborY) {
if (isFinite(currentX) && isFinite(currentY) && isFinite(neighborX) && isFinite(neighborY)) {
if (grid.tiles[currentX][currentY].value === grid.tiles[neighborX][neighborY].value) {
// Double the value of the current cell
grid.tiles[currentX][currentY].value *= 2;
// Set growth animation flag for merged tile
grid.tiles[currentX][currentY].triggerGrowth = true;
// Setup the merged cell for the slide animation
this.setupSlide(grid, direction, neighborX, neighborY, currentX, currentY);
grid.mergedTiles[neighborX][neighborY] = grid.tiles[neighborX][neighborY];
// Erase the neighboring cell from the map
grid.tiles[neighborX][neighborY] = undefined;
return { score: grid.tiles[currentX][currentY].value };
}
}
return false;
}
};
// Public interface
return {
move: function(direction, grid) {
return Movement.move(direction, grid);
},
canMove: function(direction, grid) {
return Movement.canMove(direction, grid);
}
};
})();
var Game = (function() {
var Game = {
reset: function() {
// tile: {value: 2, x: 0, y: 0} - Includes value and coordinates (redundant, but error correcting)
var grid = {
// column x row
tiles: [],
// Used for tiles that are merged but need to be animated
mergedTiles: [],
width: 4,
height: 4,
winningValue: 2048
};
// Fill the set of tiles as empty
for (var x = 0; x < grid.width; x++) {
grid.tiles[x] = [];
grid.mergedTiles[x] = [];
for (var y = 0; y < grid.height; y++) {
grid.tiles[x][y] = undefined;
grid.mergedTiles[x][y] = undefined;
}
}
// Randomly generate and place two tiles to start
var firstTile = randomTile(grid);
grid.tiles[firstTile.x][firstTile.y] = firstTile;
var secondTile = randomTile(grid);