-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
358 lines (341 loc) · 9 KB
/
main.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
var board = new Array();
var score = 0;
var has_conflicted = new Array();
var startx = 0;
var starty = 0;
var endx = 0;
var endy = 0;
var success_string = 'Success';
var gameover_string = 'GameOver';
$(document).ready(function() {
prepare_for_mobile();
new_game();
});
function new_game() {
//初始化棋盘
init();
//在随机两个格子生成数字
generate_one_number();
generate_one_number();
}
function init() {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
var grid_cell = $('#grid_cell_' + i + '_' + j);
grid_cell.css('top', get_pos_top(i, j));
grid_cell.css('left', get_pos_left(i, j));
}
}
for (var i = 0; i < 4; i++) {
board[i] = new Array();
has_conflicted[i] = new Array();
for (var j =0; j < 4; j++) {
board[i][j] = 0;
has_conflicted[i][j] = false;
}
}
update_board_view();
score = 0;
update_score(score);
}
function update_board_view() {
$('.number_cell').remove();
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
$('#grid_container').append('<div class="number_cell" id="number_cell_' + i + '_' + j + '"></div>');
var number_cell = $('#number_cell_' + i + '_' + j);
if (board[i][j] == 0) {
number_cell.css('width', '0px');
number_cell.css('height', '0px');
number_cell.css('top', get_pos_top(i, j) + cell_side_length / 2);
number_cell.css('left', get_pos_left(i, j) + cell_side_length / 2);
} else {
number_cell.css('width', cell_side_length);
number_cell.css('height', cell_side_length);
number_cell.css('top', get_pos_top(i, j));
number_cell.css('left', get_pos_left(i, j));
number_cell.css('background-color', get_number_background_color(board[i][j]));
number_cell.css('color', get_number_color(board[i][j]));
number_cell.text(board[i][j]);
}
has_conflicted[i][j] = false;
}
}
$('.number_cell').css('line-height', cell_side_length + 'px');
$('.number_cell').css('font-size', 0.6 * cell_side_length + 'px');
}
function generate_one_number() {
if (nospace(board)) {
return false;
}
//随机一个位置
var randx = parseInt(Math.floor(Math.random() * 4));
var randy = parseInt(Math.floor(Math.random() * 4));
var time = 0;
while (time < 50) {
if (board[randx][randy] == 0) {
break;
}
randx = parseInt(Math.floor(Math.random() * 4));
randy = parseInt(Math.floor(Math.random() * 4));
time++;
}
if (time == 50) {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
if (board[i][j] == 0) {
randx = i;
randy = j;
}
}
}
}
//随机一个数字
var rand_number = Math.random() < 0.5 ? 2 : 4;
//在随机位置显示随机数字
board[randx][randy] = rand_number;
show_number_with_animation(randx, randy, rand_number);
return true;
}
$(document).keydown(function(event) {
if ($('#score').text() == success_string) {
new_game();
return;
}
switch (event.keyCode) {
case 37: //left
event.preventDefault();
if (move_left()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
break;
case 38: //up
event.preventDefault();
if (move_up()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
break;
case 39: //right
event.preventDefault();
if (move_right()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
break;
case 40: //down
event.preventDefault();
if (move_down()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
break;
default:
break;
}
});
document.addEventListener('touchstart', function(event) {
startx = event.touches[0].pageX;
starty = event.touches[0].pageY;
});
document.addEventListener('touchmove', function(event) {
event.preventDefault();
});
document.addEventListener('touchend', function(event) {
endx = event.changedTouches[0].pageX;
endy = event.changedTouches[0].pageY;
var deltax = endx - startx;
var deltay = endy - starty;
if (Math.abs(deltax) < 0.3 * document_width && Math.abs(deltay) < 0.3 * document_width) {
return;
}
if ($('#score').text() == success_string) {
new_game();
return;
}
//x
if (Math.abs(deltax) >= Math.abs(deltay)) {
if (deltax > 0) {
//move right
if (move_right()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
} else {
//move left
if (move_left()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
}
} else { //y
if (deltay > 0) {
//move down
if (move_down()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
} else {
//move up
if (move_up()) {
setTimeout('generate_one_number()', 210);
setTimeout('is_gameover()', 300);
}
}
}
});
function move_left() {
if (!can_move_left(board)) {
return false;
}
//move left
for (var i = 0; i < 4; i++) {
for (var j = 1; j < 4; j++) {
if (board[i][j] != 0) {
for (var k = 0; k < j; k++) {
if (board[i][k] == 0 && no_block_horizontal(i, k, j, board)) {
show_move_animation(i, j, i, k);
board[i][k] = board[i][j];
board[i][j] = 0;
break;
} else if (board[i][k] == board[i][j] && no_block_horizontal(i, k, j, board) && !has_conflicted[i][k]) {
show_move_animation(i, j, i, k);
board[i][k] += board[i][j]
board[i][j] = 0;
//add score
score += board[i][k];
update_score(score);
has_conflicted[i][k] = true;
break;
}
}
}
}
}
setTimeout('update_board_view()', 200);
return true;
}
function move_right() {
if (!can_move_right(board)) {
return false;
}
//move right
for (var i = 0; i < 4; i++) {
for (var j = 2; j >= 0; j--) {
if (board[i][j] != 0) {
for (var k = 3; k > j; k--) {
if (board[i][k] == 0 && no_block_horizontal(i, j, k, board)) {
show_move_animation(i, j, i, k);
board[i][k] = board[i][j];
board[i][j] = 0;
break;
} else if (board[i][k] == board[i][j] && no_block_horizontal(i, j, k, board) && !has_conflicted[i][k]) {
show_move_animation(i, j, i, k);
board[i][k] += board[i][j];
board[i][j] = 0;
//add score
score += board[i][k];
update_score(score);
has_conflicted[i][k] = true;
break;
}
}
}
}
}
setTimeout('update_board_view()', 200);
return true;
}
function move_up() {
if (!can_move_up(board)) {
return false;
}
//move up
for (var j = 0; j < 4; j++) {
for (var i = 1; i < 4; i++) {
if (board[i][j] != 0) {
for (var k = 0; k < i; k++) {
if (board[k][j] == 0 && no_block_vertical(j, k, i, board)) {
show_move_animation(i, j, k, j);
board[k][j] = board[i][j];
board[i][j] = 0;
break;
} else if (board[k][j] == board[i][j] && no_block_vertical(j, k, i, board) && !has_conflicted[k][j]) {
show_move_animation(i, j, k, j);
board[k][j] += board[i][j];
board[i][j] = 0;
//add score
score += board[k][j];
update_score(score);
has_conflicted[k][j] = true;
break;
}
}
}
}
}
setTimeout('update_board_view()', 200);
return true;
}
function move_down() {
if (!can_move_down(board)) {
return false;
}
//move down
for (var j = 0; j < 4; j++) {
for (var i = 2; i >= 0; i--) {
if (board[i][j] != 0) {
for (var k = 3; k > i; k--) {
if (board[k][j] == 0 && no_block_vertical(j, i, k, board)) {
show_move_animation(i, j, k, j);
board[k][j] = board[i][j];
board[i][j] = 0;
break;
} else if (board[k][j] == board[i][j] && no_block_vertical(j, i, k, board) && !has_conflicted[k][j]) {
show_move_animation(i, j, k, j);
board[k][j] += board[i][j];
board[i][j] = 0;
//add score
score += board[k][j];
update_score(score);
has_conflicted[k][j] = true;
break;
}
}
}
}
}
setTimeout('update_board_view()', 200);
return true;
}
function is_gameover() {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
if (board[i][j] == 2048) {
update_score(success_string);
return;
}
}
}
if (nospace(board) && nomove(board)) {
gameover();
}
}
function gameover() {
update_score(gameover_string);
}
function prepare_for_mobile() {
if (document_width > 500) {
grid_container_width = 500;
cell_side_length = 100;
cell_space = 20;
}
$('#grid_container').css('width', grid_container_width - 2 * cell_space);
$('#grid_container').css('height', grid_container_width - 2 * cell_space);
$('#grid_container').css('padding', cell_space);
$('#grid_container').css('border-radius', 0.02 * grid_container_width);
$('.grid_cell').css('width', cell_side_length);
$('.grid_cell').css('height', cell_side_length);
$('.grid_cell').css('border-radius', 0.02 * grid_container_width);
}