-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2048.js
276 lines (250 loc) · 8.59 KB
/
main2048.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
var board = new Array();
var score = 0;
var top = 240;
$(document).ready(function(e){
newgame();
});
function newgame(){
//初始化棋盘格
document.getElementById("score").innerHTML=0;
init();
//在随机两个各自声称的数字
generateOneNumber();
generateOneNumber();
}
function init(){
for(var i = 0;i<4;i++){
for(var j = 0;j<4;j++){
var gridCell = $("#grid-cell-"+i+"-"+j);
gridCell.css("top",getPosTop(i,j));
gridCell.css("left",getPosLeft(i,j));
}
}
for(var i = 0; i<4;i++){
board[i] = new Array();
for(var j = 0;j<4;j++){
board[i][j] = 0;
}
}
updateBoardView();//通知前端对board二位数组进行设定。
}
function updateBoardView(){
$(".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 theNumberCell = $('#number-cell-'+i+'-'+j);
if(board[i][j] == 0){
theNumberCell.css('width','0px');
theNumberCell.css('height','0px');
theNumberCell.css('top',getPosTop(i,j));
theNumberCell.css('left',getPosLeft(i,j));
}else{
theNumberCell.css('width','100px');
theNumberCell.css('hegiht','100px');
theNumberCell.css('top',getPosTop(i,j));
theNumberCell.css('left',getPosLeft(i,j));
theNumberCell.text(board[i][j]);
}
}
}
}
function generateOneNumber(){
if (nospace(board))
return false;
//随机一个位置
var randx = parseInt(Math.floor(Math.random()*4));
var randy = parseInt(Math.floor(Math.random()*4));
while(true){
if (board[randx][randy] == 0)
break;
randx = parseInt(Math.floor(Math.random()*4));
randy = parseInt(Math.floor(Math.random()*4));
}
//随机一个数字
var randNumber = Math.random()<0.5 ?2 : 4;
//在随机位置显示随机数字
board[randx][randy] = randNumber;
showNumberWithAnimation(randx,randy,randNumber);
return true;
}
//事件响应循环
$(document).keydown(function(event){
switch (event.keyCode) {
case 37://left
if(moveLeft()){
//setTimeout("generateOneNumber()",210);
generateOneNumber();//每次新增一个数字就可能出现游戏结束
isgameover();//300毫秒
}
break;
case 38://up
if(moveUp()){
generateOneNumber();//每次新增一个数字就可能出现游戏结束
isgameover();
}
break;
case 39://right
if(moveRight()){
generateOneNumber();//每次新增一个数字就可能出现游戏结束
isgameover();
}
break;
case 40://down
if(moveDown()){
generateOneNumber();//每次新增一个数字就可能出现游戏结束
isgameover();
}
break;
}
});
function isgameover(){
if(nospace(board)&&nomove(board))
gameover();
}
function gameover(){
alert("gameover");
}
function moveLeft(){//更多地细节信息
//判断格子是否能够向左移动
if( !canMoveLeft(board))
return false;
for(var i = 0;i<4;i++)
for(var j = 1;j<4;j++){//第一列的数字不可能向左移动
if(board[i][j] !=0){
//(i,j)左侧的元素
for(var k = 0;k<j;k++){
//落脚位置的是否为空 && 中间没有障碍物
if(board[i][k] == 0 && noBlockHorizontal(i , k, j, board)){
//move
showMoveAnimation(i, j,i,k);
board[i][k] = board[i][j];
board[i][j] = 0;
continue;
}
//落脚位置的数字和本来的数字相等 && 中间没有障碍物
else if(board[i][k] == board[i][j] && noBlockHorizontal(i , k, j, board)){
//move
showMoveAnimation(i, j,i,k);
//add
board[i][k] += board[i][j];score+=board[i][k];document.getElementById("score").innerHTML=score;
board[i][j] = 0;
continue;
}
}
}
}
setTimeout("updateBoardView()",200);
return true;
}
function moveRight(){//更多地细节信息
//判断格子是否能够向右移动
if( !canMoveRight(board))
return false;
for(var i = 0;i<4;i++)
for(var j = 2;j>=0;j--){//第一列的数字不可能向右移动
if(board[i][j] !=0){
//(i,j)右侧的元素
for(var k = 3;k>j;k--){
//落脚位置的是否为空 && 中间没有障碍物
if(board[i][k] == 0 && noBlockHorizontal(i , j, k, board)){
//move
showMoveAnimation(i, j,i,k);
board[i][k] = board[i][j];
board[i][j] = 0;
continue;
}
//落脚位置的数字和本来的数字相等 && 中间没有障碍物
else if(board[i][k] == board[i][j] && noBlockHorizontal(i , j, k, board)){
//move
showMoveAnimation(i, j,i,k);
//add
board[i][k] += board[i][j];score+=board[i][k];document.getElementById("score").innerHTML=score;
board[i][j] = 0;
continue;
}
}
}
}
setTimeout("updateBoardView()",200);
return true;
}
function moveUp(){//更多地细节信息
if( !canMoveUp(board))
return false;
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 && noBlockHorizontaL(j , k, i, board)){
//move
showMoveAnimation(i, j,k,j);
board[k][j] = board[i][j];
board[i][j] = 0;
continue;
}
//落脚位置的数字和本来的数字相等 && 中间没有障碍物
else if(board[k][j] == board[i][j]&& noBlockHorizontaL(j , k, i, board) ){
//move
showMoveAnimation(i, j,k,j);
//add
board[k][j] += board[i][j];score+=board[k][j];document.getElementById("score").innerHTML=score;
board[i][j] = 0;
continue;
}
}
}
}
setTimeout("updateBoardView()",200);
return true;
}
function moveDown(){//更多地细节信息
if( !canMoveDown(board))
return false;
for(var j = 0;j<4;j++)
for(var i = 2;i>=0;i--){
if(board[i][j] !=0){
//(i,j)上侧的元素
for(var k = 3;k>i;k--){
//落脚位置的是否为空 && 中间没有障碍物
if(board[k][j] == 0 && noBlockHorizontaL(j , i, k, board)){
//move
showMoveAnimation(i, j,k,j);
board[k][j] = board[i][j];
board[i][j] = 0;
continue;
}
//落脚位置的数字和本来的数字相等 && 中间没有障碍物
else if(board[k][j] == board[i][j]&& noBlockHorizontaL(j , i, k, board) ){
//move
showMoveAnimation(i, j,k,j);
//add
board[k][j] += board[i][j];score+=board[k][j];document.getElementById("score").innerHTML=score;
board[i][j] = 0;
continue;
}
}
}
}
setTimeout("updateBoardView()",200);
return true;
}
function showNumberWithAnimation(i, j, randNumber) {
var numberCell = $('#number-cell-' + i + '-' + j);
numberCell.text(randNumber);
numberCell.animate({
width : "100px",
height : "100px",
top : getPosTop(i, j),
left : getPosLeft(i, j)
}, 50);
}
function showMoveAnimation(fromx, fromy, tox, toy){
var numberCell = $('#number-cell-'+fromx +'-'+fromy);
numberCell.animate({top:getPosTop(tox,toy),
left:getPosLeft(tox,toy)},200);
}
$(document).keydown(function (event) {
event.preventDefault();
});