-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
200 lines (178 loc) · 4.72 KB
/
script.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
var board;
var score = 0;
var rows = 4;
var columns = 4;
window.onload = function() {
setGame();
}
function setGame() {
board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
let tile = document.createElement("div");
tile.id = r.toString() + "-" + c.toString();
let num = board[r][c];
updateTile(tile, num);
document.getElementById("board").append(tile);
}
}
setTwo();
setTwo();
}
function updateTile(tile, num) {
tile.innerText = "";
tile.classList.value = "";
tile.classList.add("tile");
if (num > 0) {
tile.innerText = num.toString();
if (num <= 4096) {
tile.classList.add("b"+num.toString());
} else {
tile.classList.add("b8192");
}
}
}
document.addEventListener('keyup', (e) => {
if (e.code == "ArrowLeft") {
slideLeft();
setTwo();
}
else if (e.code == "ArrowRight") {
slideRight();
setTwo();
}
else if (e.code == "ArrowUp") {
slideUp();
setTwo();
}
else if (e.code == "ArrowDown") {
slideDown();
setTwo();
}
document.getElementById("score").innerText = "Score: " + score;
})
// For Mobile Devices
document.getElementById("keyup").addEventListener("click", (e) => {
slideUp();
setTwo();
});
document.getElementById("keydown").addEventListener("click", (e) => {
slideDown();
setTwo();
});
document.getElementById("keyright").addEventListener("click", (e) => {
slideRight();
setTwo();
});
document.getElementById("keyleft").addEventListener("click", (e) => {
slideLeft();
setTwo();
});
window.addEventListener("keydown", function(e) {
if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
e.preventDefault();
}
}, false);
function filterZero(row){
return row.filter(num => num != 0);
}
function slide(row) {
//[0, 2, 2, 2]
row = filterZero(row); //[2, 2, 2]
for (let i = 0; i < row.length-1; i++){
if (row[i] == row[i+1]) {
row[i] *= 2;
row[i+1] = 0;
score += row[i];
}
}
row = filterZero(row);
while (row.length < columns) {
row.push(0);
}
return row;
}
function slideLeft() {
for (let r = 0; r < rows; r++) {
let row = board[r];
row = slide(row);
board[r] = row;
for (let c = 0; c < columns; c++){
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
updateTile(tile, num);
}
}
}
function slideRight() {
for (let r = 0; r < rows; r++) {
let row = board[r];
row.reverse();
row = slide(row)
board[r] = row.reverse();
for (let c = 0; c < columns; c++){
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
updateTile(tile, num);
}
}
}
function slideUp() {
for (let c = 0; c < columns; c++) {
let row = [board[0][c], board[1][c], board[2][c], board[3][c]];
row = slide(row);
for (let r = 0; r < rows; r++){
board[r][c] = row[r];
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
updateTile(tile, num);
}
}
}
function slideDown() {
for (let c = 0; c < columns; c++) {
let row = [board[0][c], board[1][c], board[2][c], board[3][c]];
row.reverse();
row = slide(row);
row.reverse();
for (let r = 0; r < rows; r++){
board[r][c] = row[r];
let tile = document.getElementById(r.toString() + "-" + c.toString());
let num = board[r][c];
updateTile(tile, num);
}
}
}
function setTwo() {
if (!hasEmptyTile()) {
return;
}
let found = false;
while (!found) {
let r = Math.floor(Math.random() * rows);
let c = Math.floor(Math.random() * columns);
if (board[r][c] == 0) {
board[r][c] = 2;
let tile = document.getElementById(r.toString() + "-" + c.toString());
tile.innerText = "2";
tile.classList.add("b2");
found = true;
}
}
}
function hasEmptyTile() {
let count = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
if (board[r][c] == 0) {
return true;
}
}
}
return false;
}