-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
328 lines (301 loc) · 6.92 KB
/
app.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
var grid = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]];
var score = 0;
var highestscore = localStorage.getItem('highestscore') || 0;
document.getElementsByClassName('highestscore-board')[0].getElementsByClassName('highest-score')[0].innerHTML = highestscore;
var leftSaves = 0;
var leftChances = leftSaves;
function gridEqual(grid1, grid2){
for(var i = 0; i < 4; i++){
for(var j = 0; j < 4; j++){
if(grid1[i][j] !== grid2[i][j]){
return false
}
}
}
return true
}
function drawCells(){
var rows = document.getElementsByClassName('grid-row');
var scoreContainer = document.getElementsByClassName('score-container')[0];
for(var i = 0; i < 4; i++){
var cells = rows[i].getElementsByClassName('grid-cell');
for(var j = 0; j < 4; j++){
if(grid[i][j] != 0){
cells[j].innerHTML = grid[i][j];
cells[j].style.textAlign = "center";;
}
else{
cells[j].innerHTML = "";
}
}
}
for(var i = 0; i < 4; i++){
cells = rows[i].getElementsByClassName('grid-cell');
for(var j = 0; j < 4; j++){
if(grid[i][j] == 0){
cells[j].style.background = "#febe7e";
}
else if(grid[i][j] == 2){
cells[j].style.background = "#e4f68f";
}
else if(grid[i][j] == 4){
cells[j].style.background = "#a9d7f6";
}
else if(grid[i][j] == 8){
cells[j].style.background = "#f19cbb";
}
else if(grid[i][j] == 16){
cells[j].style.background = "#ccccff";
}
else if(grid[i][j] == 32){
cells[j].style.background = "#faebd7";
}
else if(grid[i][j] == 64){
cells[j].style.background = "#b2beb5";
}
else if(grid[i][j] == 128){
cells[j].style.background = "#c4d8e2";
}
else if(grid[i][j] == 256){
cells[j].style.background = "#fb607f";
}
else if(grid[i][j] == 512){
cells[j].style.background = "#ffc1cc";
}
else if(grid[i][j] == 1024){
cells[j].style.background = "#a3c1ad";
}
else if(grid[i][j] == 2048){
cells[j].style.background = "#00bfff";
}
else if(grid[i][j] == 4096){
cells[j].style.background = "#ffb7c5";
}
}
}
scoreContainer.getElementsByClassName('current-score')[0].innerHTML = score;
if(score > highestscore){
var tempScore = parseInt(score);
localStorage.setItem('highestscore', tempScore);
document.getElementsByClassName('highestscore-board')[0].getElementsByClassName('highest-score')[0].innerHTML = score;
}
document.getElementsByClassName('bonus-board')[0].innerHTML = "<p>you can destory </p><p>" + parseInt(leftChances) + " cells</p>";
if(isGameOver(grid)){
document.getElementsByClassName("game-control")[0].innerHTML = "Game Over <br>Try again?";
}
else{
document.getElementsByClassName("game-control")[0].innerHTML = "New Game";
}
}
function copyGrid(grid){
var newGrid = [];
for(var i = 0; i < 4; i++){
newGrid.push([]);
for(var j = 0; j < 4; j++){
newGrid[i].push(grid[i][j]);
}
}
return newGrid
}
function addNumber(grid){
var options = [];
for(var i = 0; i < 4; i++){
for(var j = 0; j < 4; j++){
if(grid[i][j] === 0){
options.push([i,j]);
}
}
}
if(options.length > 0){
var spot = options[Math.floor(Math.random() * options.length)];
var twoOrFour = Math.round(Math.random() - 0.3);
grid[spot[0]][spot[1]] = twoOrFour === 0? 2 : 4;
}
}
function slideLeft(row){
var newRow = [];
var j = 0
for(var i = 0; i < 4; i++){
if(row[i] !== 0){
newRow[j] = row[i];
j += 1
}
}
var missing = 4 - newRow.length;
while(missing > 0){
newRow.push(0);
missing -= 1;
}
return newRow
}
function combineLeft(row){
var newRow = [];
var i = 0;
while(i < 4){
j = i + 1;
if(j < 4){
if(row[j] == row[i]){
newRow.push(2 * row[i]);
score += 2 * row[i];
i += 2;
if(Math.floor(score / 1000) > leftSaves){
leftSaves += 1;
leftChances += 1;
}
}
else{
newRow.push(row[i]);
i += 1;
}
}
else{
newRow.push(row[i]);
i += 1;
}
}
var missing = 4 - newRow.length;
while(missing > 0){
newRow.push(0);
missing -= 1;
}
return newRow
}
function leftOperation(grid){
var newGrid = [];
for(var i = 0; i < 4; i++){
newGrid.push(combineLeft(slideLeft(grid[i])));
}
return newGrid
}
function slideAndCombineRight(row){
var newRow = [];
newRow = combineLeft(slideLeft(row.reverse())).reverse();
return newRow
}
function rightOperation(grid){
var newGrid = [];
for(var i = 0; i < 4; i++){
newGrid.push(slideAndCombineRight(grid[i]));
}
return newGrid
}
function slideAndCombineUp(grid, j){
var column = [];
for(var i = 0; i < 4; i++){
column.push(grid[i][j]);
}
column = combineLeft(slideLeft(column));
for(var i = 0; i < 4; i++){
grid[i][j] = column[i];
}
return grid
}
function upOperation(grid){
var newGrid = [];
for(var j = 0; j < 4; j++){
newGrid = slideAndCombineUp(grid, j)
}
return newGrid;
}
function slideAndCombineDown(grid, j){
var column = [];
for(var i = 0; i < 4; i++){
column.push(grid[i][j]);
}
column = slideAndCombineRight(column);
for(var i = 0; i < 4; i++){
grid[i][j] = column[i];
}
return grid
}
function downOperation(grid){
var newGrid = [];
for(var j = 0; j < 4; j++){
newGrid = slideAndCombineDown(grid, j)
}
return newGrid;
}
function isGameOver(grid){
if(leftChances > 0){
return false
}
for(var i = 0; i < 4; i++){
for(var j = 0; j < 4; j++){
if(grid[i][j] == 0){
return false
}
else if(i != 3 && grid[i][j] == grid[i+1][j]){
return false
}
else if(j != 3 && grid[i][j] == grid[i][j+1]){
return false
}
}
}
return true
}
$(document).ready(function(){
addNumber(grid);
addNumber(grid);
drawCells();
});
$(document).keydown(function(e) {
var previousGrid = copyGrid(grid);
switch(e.which) {
case 37:
grid = leftOperation(grid);
drawCells();
break;
case 38:
grid = upOperation(grid);
drawCells();
break;
case 39:
grid = rightOperation(grid);
drawCells();
break;
case 40:
grid = downOperation(grid);
drawCells();
break;
default: return;
}
if(gridEqual(previousGrid, grid) === false){
addNumber(grid);
drawCells();
}
e.preventDefault();
});
$(".grid-cell").dblclick(function(event) {
if(leftChances > 0){
leftChances -= 1;
var cellID = $(this).attr('id');
var i = Number(cellID[4]);
var j = Number(cellID[5]);
score -= grid[i][j];
grid[i][j] = 0;
drawCells();
$(this).effect("shake");
}
});
$(".game-control").click(function(){
startOver(grid);
});
function startOver(grid){
score = 0;
leftSaves = 0;
leftChances = leftSaves;
for(var i = 0; i < 4; i++){
for(var j = 0; j < 4; j++){
grid[i][j] = 0;
}
}
drawCells();
addNumber(grid);
addNumber(grid);
drawCells();
}