-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
157 lines (144 loc) · 3.06 KB
/
render.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
//得到top,left值
function getTop(i, j) {
return 20 + i * 120;
}
function getLeft(i, j) {
return 20 + j * 120;
}
function updateScore(score) {
$('#score').text(score);
}
//判断水平和垂直方向中间是否有阻隔
function noHBlock(row, col1, col2, grid) { //col1>col2
for (var s = col2 + 1; s < col1; s++) {
if (grid[row][s] !== 0) return false;
}
return true;
}
function noVBlock(row1, row2, col, grid) { //row1>row2
for (var s = row2 + 1; s < row1; s++) {
if (grid[s][col] !== 0) return false;
}
return true;
}
//判断是否可以移动
function canMoveLeft(board) {
for (var i = 0; i < 4; i++) {
for (var j = 1; j < 4; j++) {
if (board[i][j] !== 0) {
if (board[i][j - 1] === 0 || board[i][j - 1] === board[i][j]) {
return true;
}
}
}
}
return false;
}
function canMoveRight(board) {
for (var i = 0; i < 4; i++) {
for (var j = 2; j >= 0; j--) {
if (board[i][j] !== 0) {
if (board[i][j + 1] === 0 || board[i][j + 1] === board[i][j]) {
return true;
}
}
}
}
return false;
}
function canMoveUp(board) {
for (var i = 1; i < 4; i++) {
for (var j = 0; j < 4; j++) {
if (board[i][j] !== 0) {
if (board[i - 1][j] === 0 || board[i - 1][j] === board[i][j]) {
return true;
}
}
}
}
return false;
}
function canMoveDown(board) {
for (var i = 2; i >= 0; i--) {
for (var j = 0; j < 4; j++) {
if (board[i][j] !== 0) {
if (board[i + 1][j] === 0 || board[i + 1][j] === board[i][j]) {
return true;
}
}
}
}
return false;
}
//判断游戏是否结束
function nospace(board) {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
if (board[i][j] === 0) {
return false;
}
}
}
return true;
}
function nomove(board) {
if (canMoveLeft(board) || canMoveRight(board) || canMoveUp(board) || canMoveDown(board)) {
return false;
}
return true;
}
function isGameOver(board) {
if (nospace(board) && nomove(board)) {
alert('Game over');
}
}
//动画
function showNumberWithAnimation(x, y, num) {
var numberCell = $("#number-cell-" + x + "-" + y);
numberCell.css({
'background-color': numberColor(num),
}).text(num);
numberCell.animate({
width: 100,
height: 100,
top: getTop(x, y),
left: getLeft(x, y)
}, 50);
}
function moveWithAnimation(i, j, s, k) {
var numberCell = $("#number-cell-" + i + "-" + j);
numberCell.animate({
top: getTop(s, k),
left: getLeft(s, k)
}, 200);
}
function numberColor(num) {
switch (num) {
case 2:
return "#eee4da";
case 4:
return "#ede0c8";
case 8:
return "#f2b179";
case 16:
return "#f59563";
case 32:
return "#f67c5f";
case 64:
return "#f65e3b";
case 128:
return "#edcf72";
case 256:
return "#edcc61";
case 512:
return "#9c0";
case 1024:
return "#33b5e5";
case 2048:
return "#09c";
case 4096:
return "#a6c";
case 8192:
return "#93c";
}
}