-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
295 lines (274 loc) · 8.43 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
window.onload = (event) => {
/**
* 50%概率获取2、4
*/
function getRandomNum() {
let blockNum;
let num = Math.random();
if (num < 0.5) {
blockNum = 2;
}else {
blockNum = 4;
}
return blockNum;
}
/**
* 获取1-4随机数
*/
function getOneToThree() {
return parseInt(Math.random()*4);
}
// 获取x,y坐标
function getXY() {
let x = getOneToThree();
let y = getOneToThree();
let judge = 0;
for (let i=0;i<4;i++) {
for (let j=0;j<4;j++) {
if (blockData[i][j] === 0) {
judge = 1;
break;
}
}
if (judge) {
break;
}
}
if (judge) {
while (blockData[x][y] !== 0) {
x = getOneToThree();
y = getOneToThree();
}
} else {
alert('游戏失败!!!')
return ;
}
return [x, y];
}
// 在一个随机一个空格子中出现2或4
function appearNumberInGrid() {
// 获取随机数
let blockNum = getRandomNum();
// 得到坐标
let [x, y] = getXY();
// 将坐标定位到数组具体的位置
let positionClass = 'cell-'.concat(x+1).concat('-').concat(y+1);
// 把随机数显示到页面对应16个格子的具体位置
let span = document.createElement('span');
let innerSpan = document.createElement('span');
let recClass = [];
// 给外部的span加类
recClass.push(positionClass);
recClass.push('cell');
span.setAttribute('class', recClass.join(' '));
innerSpan.innerText = blockNum;
innerSpan.setAttribute('class', 'block')
innerSpan.style.backgroundColor = 'rgb(246, 150, 100)';
document.getElementById('wrap').append(span);
span.appendChild(innerSpan);
blockData[x][y] = blockNum;
}
function initData() {
for (let i=0;i<4;i++) {
blockData[i] = [];
}
// 假定'0'值代表方块为空, 初始时所有方块没有数字为空
for (let i=0;i<4;i++) {
for (let j=0;j<4;j++) {
blockData[i][j] = 0;
}
}
}
// 创建出4*4的二维数组对应页面中的24个格子,全局变量
let blockData = [];
// 获取24个方块的位置
let tds = document.getElementsByTagName('span');
initData()
appearNumberInGrid();
// 将数字向左移动
function leftMove(row, col) {
let positionClass = 'cell-'.concat(row+1).concat('-').concat(col+1);
let span = document.getElementsByClassName(positionClass)[0];
let nowPosition;
let recSpan;
for (let i=col-1;i>-1;i--) {
nowPosition = 'cell-'.concat(row+1).concat('-').concat(i+1);
if (blockData[row][i] === 0) {
blockData[row][i] = blockData[row][i+1];
blockData[row][i+1] = 0;
span.classList.remove(positionClass);
span.classList.add(nowPosition);
positionClass = nowPosition;
} else {
if (blockData[row][i] === blockData[row][i+1]) {
blockData[row][i] *= 2;
blockData[row][i+1] = 0;
recSpan = document.getElementsByClassName(nowPosition)[0];
span.classList.remove(positionClass);
span.classList.add(nowPosition);
span.remove();
recSpan.firstChild.innerText = blockData[row][i];
positionClass = nowPosition;
span = recSpan;
}
}
}
}
// 按下左按键,数字往左移动,并且格子中随机出现以一个数字
function moveLeft() {
for (let col=1;col<4;col++) {
for (let row=0;row<4;row++) {
if (blockData[row][col] !== 0) {
// 一次移动一个数字
leftMove(row, col);
}
}
}
appearNumberInGrid();
}
// 将数字往右移动
function rightMove(row, col) {
let positionClass = 'cell-'.concat(row+1).concat('-').concat(col+1);
let span = document.getElementsByClassName(positionClass)[0];
let nowPosition;
let recSpan;
for (let i=col+1;i<4;i++) {
nowPosition = 'cell-'.concat(row+1).concat('-').concat(i+1);
if (blockData[row][i] === 0) {
blockData[row][i] = blockData[row][i-1];
blockData[row][i-1] = 0;
span.classList.remove(positionClass);
span.classList.add(nowPosition);
positionClass = nowPosition;
} else {
if (blockData[row][i] === blockData[row][i-1]) {
blockData[row][i] *= 2;
blockData[row][i-1] = 0;
recSpan = document.getElementsByClassName(nowPosition)[0];
span.classList.remove(positionClass);
span.classList.add(nowPosition);
span.remove();
recSpan.firstChild.innerText = blockData[row][i];
recSpan.firstChild.classList.add('block-merged');
positionClass = nowPosition;
span = recSpan;
}
}
}
}
function moveRight() {
for (let col=3;col>-1;col--) {
for (let row=0;row<4;row++) {
if (blockData[row][col] !== 0) {
// 一次移动一个数字
rightMove(row, col);
}
}
}
appearNumberInGrid();
}
function upMove (row, col) {
let positionClass = 'cell-'.concat(row+1).concat('-').concat(col+1);
let span = document.getElementsByClassName(positionClass)[0];
let nowPosition;
let recSpan;
for (let i=row-1;i>-1;i--) {
nowPosition = 'cell-'.concat(i+1).concat('-').concat(col+1);
if (blockData[i][col] === 0) {
blockData[i][col] = blockData[i+1][col];
blockData[i+1][col] = 0;
span.classList.remove(positionClass);
span.classList.add(nowPosition);
positionClass = nowPosition;
} else {
if (blockData[i][col] === blockData[i+1][col]) {
blockData[i][col] *= 2;
blockData[i+1][col] = 0;
recSpan = document.getElementsByClassName(nowPosition)[0];
span.classList.remove(positionClass);
span.classList.add(nowPosition);
span.remove();
recSpan.firstChild.innerText = blockData[i][col];
positionClass = nowPosition;
span = recSpan;
}
}
}
}
function moveUp() {
for (let row=1;row<4;row++) {
for (let col=0;col<4;col++) {
if (blockData[row][col] !== 0) {
// 一次移动一个数字
upMove(row, col);
}
}
}
appearNumberInGrid();
}
function downMove(row, col) {
let positionClass = 'cell-'.concat(row+1).concat('-').concat(col+1);
let span = document.getElementsByClassName(positionClass)[0];
let nowPosition;
let recSpan;
for (let i=row+1;i<4;i++) {
nowPosition = 'cell-'.concat(i+1).concat('-').concat(col+1);
if (blockData[i][col] === 0) {
blockData[i][col] = blockData[i-1][col];
blockData[i-1][col] = 0;
span.classList.remove(positionClass);
span.classList.add(nowPosition);
positionClass = nowPosition;
} else {
if (blockData[i][col] === blockData[i-1][col]) {
blockData[i][col] *= 2;
blockData[i-1][col] = 0;
recSpan = document.getElementsByClassName(nowPosition)[0];
span.classList.remove(positionClass);
span.classList.add(nowPosition);
span.remove();
recSpan.firstChild.innerText = blockData[i][col];
positionClass = nowPosition;
span = recSpan;
}
}
}
}
function moveDown() {
for (let row=3;row>-1;row--) {
for (let col=0;col<4;col++) {
if (blockData[row][col] !== 0) {
// 一次移动一个数字
downMove(row, col);
}
}
}
appearNumberInGrid();
}
/**
* 按键按下'上下左右'键可以执行相应的操作
*/
document.onkeydown = function(e) {
switch(e.keyCode) {
case 37:
moveLeft();
break;
case 38:
moveUp();
break;
case 39:
moveRight();
break;
case 40:
moveDown();
break;
default:
break;
}
}
/**
* main: 小游戏的入口函数
*/
let main = function() {
}
}