-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
342 lines (300 loc) · 10.7 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
/*
Author : Suresh Pokharel
Email : [email protected]
GitHub : github.com/suresh021
URL : psuresh.com.np
*/
window.onload = function() {
$("#high_score").html(localStorage.getItem("hscore")); //write high score
$( ".block" ).css( "background-color", "#ddd" );
// continue if data exists in backup_array local storage
var result = JSON.parse(localStorage.getItem("backup_array"));
if (result) {
restoreForContinue();
}else{ //start new game if no previous data is stored
startNewGame();
}
}
function startNewGame(){
$(".number").html("");
$("#total_moves").html('0');
$("#total_score").html('0');
initializeTwoBlocks();
changeBlockColor();
}
/* to set Highscore*/
function setHighScore(new_score) {
// Check browser support
if (typeof(Storage) !== "undefined") {
if (new_score > localStorage.getItem("hscore")) {
localStorage.setItem("hscore", new_score);
}
} else {
console.log("Web storage not supported");
}
}
/* To store latest itemset for continue option */
function backupData() {
var allBlocks = document.getElementsByClassName("block");
var latest_array = [];
if (typeof(Storage) !== "undefined") {
for (var i = 0; i < allBlocks.length; i++) {
latest_array.push($( "#blk-"+i).html());
}
localStorage.setItem("backup_array", JSON.stringify(latest_array));
localStorage.setItem("backup_moves", $("#total_moves").html());
localStorage.setItem("backup_score", $("#total_score").html());
console.log(JSON.parse(localStorage.getItem("backup_array")));
}
}
function restoreForContinue(){
var allBlocks = document.getElementsByClassName("block");
var result = JSON.parse(localStorage.getItem("backup_array"));
console.log(result);
if (typeof(Storage) !== "undefined") { // to check if brower supports local storage
if (result) {
for (var i = 0; i < allBlocks.length; i++) {
$( "#blk-"+i).html(result[i]);
}
$("#total_moves").html(localStorage.getItem("backup_moves"));
$("#total_score").html(localStorage.getItem("backup_score"));
changeBlockColor();
}
}
}
$(document).keydown(function(e){
switch(e.which) {
case 37: // left
handleLeft();
break;
case 38: // up
handleUp();
break;
case 39: // right
handleRight();
break;
case 40: // down
handleDown();
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
function initializeTwoBlocks(){
var first_block = Math.floor(Math.random() * 15);
var second_block = Math.floor(Math.random() * 15);
// if both number came same
if (first_block==second_block) {
var second_block = (first_block + 1)%15;
}
// populate two blocks with 2
$("#block-"+first_block).html("<p class='number' id='blk-"+first_block+"'>2</p>");
$("#block-"+second_block).html("<p class='number' id='blk-"+second_block+"'>2</p>");
}
function changeBlockColor(){
var allBlocks = document.getElementsByClassName("block");
for (var i = allBlocks.length - 1; i >= 0; i--) { $( "#blk-0").html() == ""
//if(allBlocks[i].innerHTML=="<p class=\"number\" id='blk-'+i>2</p>"){
if($( "#blk-"+i).html()=="2"){
allBlocks[i].style.background='#ff99ff';
}else if($( "#blk-"+i).html()=="4"){
allBlocks[i].style.background='#6699cc';
}
else if($( "#blk-"+i).html()=="8"){
allBlocks[i].style.background=' #5c8a8a';
}
else if($( "#blk-"+i).html()=="16"){
allBlocks[i].style.background='#008ae6';
}
else if($( "#blk-"+i).html()=="32"){
allBlocks[i].style.background='#661aff';
}else if($( "#blk-"+i).html()=="64"){
allBlocks[i].style.background=' #ff8533';
}
else if($( "#blk-"+i).html()=="128"){
allBlocks[i].style.background='#88ac00';
}
else if($( "#blk-"+i).html()=="256"){
allBlocks[i].style.background='#99994d';
}
else if($( "#blk-"+i).html()=="512"){
allBlocks[i].style.background='#ffd11a';
}
else if($( "#blk-"+i).html()=="1024"){
allBlocks[i].style.background='#ff0055';
}
else if($( "#blk-"+i).html()=="2048"){
allBlocks[i].style.background='#4d0019';
}
else if($( "#blk-"+i).html()=="4096"){
allBlocks[i].style.background='#1a0000';
}else{
allBlocks[i].style.background='#ddd';
}
}
}
function handleUp(){
var changed_something = 0;
// when up key is pressed (0,4,8,12),(1,5,9,13),... should be merged if possible
for (var j = 0; j < 4; j++) {
for (var i = 0; i <16; i++) {
if ( ($( "#blk-"+i).html() != "") && i<12 && ($( "#blk-"+i).html() == $( "#blk-"+(i+4)).html())) { //merge if same numbers came
var temp= eval(2 * ($( "#blk-"+i).html()));
$( "#blk-"+i).html(temp);
$( "#blk-"+(i+4)).html("");
var changed_something = 1;
updateScore($( "#blk-"+i ).html()); //send the number to update score
}
if ($( "#blk-"+i).html() == "" && i<12) { //upshift the blank spaces
var temp= $( "#blk-"+(i+4)).html()
/* to check if some position changes as it doesnot make sense if both are empty space*/
if (i<12 && (($("#blk-"+i).html() != "") || ($("#blk-"+(i+4)).html() != ""))) {
var changed_something = 1;
}
$( "#blk-"+i ).html(temp);
$( "#blk-"+(i+4)).html("");
}
}
}
// if something is changed, we need to generate new item
if (changed_something) {
generateNewItem();
}
//change block color after change in numbers
changeBlockColor();
}
function handleLeft(){
var changed_something = 0;
// when Left key is pressed (0,1,2,3),(4,5,6,7),... should be merged if possible
for (var j = 0; j < 4; j++) {
for (var i = 0; i <16; i++) {
if ( ($( "#blk-"+i).html() != "") && i!=3 && i!=7 && i!=11 && i!=15 && ($( "#blk-"+i).html() == $( "#blk-"+(i+1)).html())) { //merge if same numbers came
var temp= eval(2 * ($( "#blk-"+i).html()));
$( "#blk-"+i).html(temp);
$( "#blk-"+(i+1)).html("");
var changed_something = 1;
updateScore($( "#blk-"+i ).html()); //send the number to update score
}
if ($( "#blk-"+i).html() == "" && i!=3 && i!=7 && i!=11 && i!=15) {
var temp= $( "#blk-"+(i+1)).html()
/*to check if some position changes as it doesnot make sense if both are empty space*/
if (i!=3 && i!=7 && i!=11 && i!=15 && (($("#blk-"+i).html() != "") || ($("#blk-"+(i+1)).html() != ""))) {
var changed_something = 1;
}
$( "#blk-"+i ).html(temp);
$( "#blk-"+(i+1)).html("");
}
}
}
// if something is changed, we need to generate new item
if (changed_something) {
generateNewItem();
}
changeBlockColor();
}
function handleRight(){
var changed_something = 0;
// when Right key is pressed (0,1,2,3),(4,5,6,7),... should be merged if possible
for (var j = 0; j < 4; j++) {
for (var i = 0; i <16; i++) {
if ( ($( "#blk-"+i).html() != "") && i!=0 && i!=4 && i!=8 && i!=12 && ($( "#blk-"+i).html() == $( "#blk-"+(i-1)).html())) { //merge if same numbers came
var temp= eval(2 * ($( "#blk-"+i).html()));
$( "#blk-"+i).html(temp);
$( "#blk-"+(i-1)).html("");
var changed_something = 1;
updateScore($( "#blk-"+i ).html()); //send the number to update score
}
if ($( "#blk-"+i).html() == "" && i!=0 && i!=4 && i!=8 && i!=12) {
var temp= $( "#blk-"+(i-1)).html()
/*to check if some position changes as it doesnot make sense if both are empty space*/
if (i!=0 && i!=4 && i!=8 && i!=12 && (($("#blk-"+i).html() != "") || ($("#blk-"+(i-1)).html() != ""))) {
var changed_something = 1;
}
$( "#blk-"+i ).html(temp);
$( "#blk-"+(i-1)).html("");
}
}
}
// if something is changed, we need to generate new item
if (changed_something) {
generateNewItem();
}
changeBlockColor();
}
function handleDown(){
var changed_something = 0;
// when Down key is pressed (0,4,8,12),(1,5,9,13),... should be merged if possible
for (var j = 0; j < 4; j++) {
for (var i = 0; i <16; i++) {
if ( ($( "#blk-"+i).html() != "") && i>3 && ($( "#blk-"+i).html() == $( "#blk-"+(i-4)).html())) { //merge if same numbers came
var temp= eval(2 * ($( "#blk-"+i).html()));
$( "#blk-"+i).html(temp);
$( "#blk-"+(i-4)).html("");
var changed_something = 1;
updateScore($( "#blk-"+i ).html()); //send the number to update score
}
if ($( "#blk-"+i).html() == "" && i>3) {
var temp= $( "#blk-"+(i-4)).html();
/*to check if some position changes as it doesnot make sense if both are empty space*/
if (i>3 && (($("#blk-"+i).html() != "") || ($("#blk-"+(i-4)).html() != ""))) {
var changed_something = 1;
}
$( "#blk-"+i ).html(temp);
$( "#blk-"+(i-4)).html("");
}
}
}
// if something is changed, we need to generate new item
if (changed_something) {
generateNewItem();
}
changeBlockColor();
}
function generateNewItem(){ // to generate and place new item on every move
var empty_fields = []; // array to store empty fields
for (var i = 0; i <16; i++) {
if ($( "#blk-"+i).html() == "") {
empty_fields.push(i);
}
}
//to update move count
var count= eval($( "#total_moves").html());
count++;
$( "#total_moves").html(count);
//get an empty field id randomly
var new_item = empty_fields[Math.floor(Math.random()*empty_fields.length)];
//place new number 2 in field
$( "#blk-"+new_item).html("2");
changeBlockColor();
checkGameOver();
backupData(); // for continue option
}
function updateScore(new_value){
var old_score = eval($("#total_score").html());
var new_score = old_score+ 3 * eval(new_value);
$("#total_score").html(new_score);
setHighScore(new_score);
}
function checkGameOver(){
var empty_fields = []; // array to store empty fields
for (var i = 0; i <16; i++) {
if ($( "#blk-"+i).html() == "") {
empty_fields.push(i);
}
}
//console.log(empty_fields.length);
//if no empty field send game over message
if(empty_fields.length==0){
var game_over = true;
for (var i = 0; i < 16; i++) {
/*this condition is used to determine if any move is possible
in any direction___made by just combining up,down,left,write conditions above*/
if ((($( "#blk-"+i).html() != "") && i<12 && ($( "#blk-"+i).html() == $( "#blk-"+(i+4)).html())) || ( ($( "#blk-"+i).html() != "") && i!=3 && i!=7 && i!=11 && i!=15 && ($( "#blk-"+i).html() == $( "#blk-"+(i+1)).html())) || ( ($( "#blk-"+i).html() != "") && i!=0 && i!=4 && i!=8 && i!=12 && ($( "#blk-"+i).html() == $( "#blk-"+(i-1)).html())) || ( ($( "#blk-"+i).html() != "") && i>3 && ($( "#blk-"+i).html() == $( "#blk-"+(i-4)).html()))){
var game_over = false;
}
}
if(game_over){
$( "#status" ).html("<hr>GAME<br>OVER");
}
}
}