-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
359 lines (325 loc) · 9.56 KB
/
index.html
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
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>2048 Game Online</title>
<style>
body {
background-color: lightblue;
display: flex;
justify-content: center;
font-family: "Clear Sans", "Helvetica Neue";
}
h1 {
font-size: 80px;
line-height: 0.7;
color: darkblue;
margin: 0px;
}
.container {
width: 468px;
margin-top: 10px;
}
.info {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.grid {
display: flex;
flex-wrap: wrap;
width: 456px;
height: 456px;
background-color: darkblue;
border: 7px solid black;
border-radius: 6px;
margin-top: 20px;
}
.grid div {
width: 100px;
height: 100px;
margin: 7px;
border-radius: 3px;
background-color: lightblue;
color: darkblue;
font-weight: bold;
text-align: center;
font-size: 60px;
line-height: 1.6;
}
.score-container,
button {
text-align: center;
height: 60px;
border-radius: 3px;
background-color:darkblue;
color: #ffffff;
}
.score-container {
width: 70px;
}
#score {
font-size: 30px;
}
.score-title,
#restart-button {
font-size: 16px;
}
</style>
</head>
<body>
<div class="container">
<div class="info">
<h1>2048</h1>
<div class="score-container">
<div class="score-title">score</div>
<span id="score">0</span>
</div>
<button id="restart-button">Restart Game</button>
</div>
<span id="result">Join the numbers and get to the <b>2048</b> tile!</span>
<div class="grid"></div>
</div>
<script>
function createBoard() {
let grid = document.querySelector(".grid");
if (grid.textContent == 0) {
grid.textContent.display = 'none';
}
for (let i = 0; i < 16; i++) {
let div = document.createElement("div");
div.setAttribute("id", `id_${i}`);
div.textContent = 0;
grid.appendChild(div);
}
}
createBoard();
function generate() {
let optionArray = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4];
let allBlocks = document.querySelectorAll(".grid > div");
let emptyBlocks = Array.from(allBlocks).filter(
(a) => a.textContent == 0
);
if (emptyBlocks.length == 0) {
return;
}
let randomNumber = optionArray[Math.floor(Math.random() * 10)];
let randomBlock =
emptyBlocks[Math.floor(Math.random() * emptyBlocks.length)];
randomBlock.textContent = randomNumber;
}
let totalScore = 0;
function shiftArrayLeft(valueArray) {
let finalArray = valueArray.filter((a) => a != 0);
let index = finalArray.length;
while (index < 4) {
finalArray.push(0);
index++;
}
return finalArray;
}
function combineArrayLeft(valueArray) {
let index = 1;
while (index < 4) {
if (valueArray[index] == valueArray[index - 1]) {
valueArray[index - 1] = valueArray[index] * 2;
totalScore += valueArray[index] * 2;
document.querySelector("#score").textContent = totalScore;
valueArray[index] = 0;
}
index++;
}
return valueArray;
}
function shiftArrayRight(valueArray) {
let finalArray = valueArray.filter((a) => a != 0);
let index = finalArray.length;
while (index < 4) {
finalArray.unshift(0);
index++;
}
return finalArray;
}
function combineArrayRight(valueArray) {
let index = valueArray.length - 1;
while (index > 0) {
if (valueArray[index] == valueArray[index - 1]) {
totalScore += valueArray[index] * 2;
document.querySelector("#score").textContent = totalScore;
valueArray[index] = valueArray[index] * 2;
valueArray[index - 1] = 0;
}
index--;
}
return valueArray;
}
function getRowValues(rowNumber) {
let rowValues = [];
for (let i = 4 * (rowNumber - 1); i < 4 * rowNumber; i++) {
rowValues.push(
Number(document.querySelector(`#id_${i}`).textContent)
);
}
return rowValues;
}
function moveRow(rowNumber, direction) {
let rowValues = getRowValues(rowNumber);
if (direction == "L") {
rowValues = shiftArrayLeft(rowValues);
rowValues = combineArrayLeft(rowValues);
rowValues = shiftArrayLeft(rowValues);
}
if (direction == "R") {
rowValues = shiftArrayRight(rowValues);
rowValues = combineArrayRight(rowValues);
rowValues = shiftArrayRight(rowValues);
}
for (let i = 4 * (rowNumber - 1); i < 4 * rowNumber; i++) {
document.querySelector(`#id_${i}`).textContent = rowValues[i % 4];
}
}
function moveLeft() {
for (let i of [1, 2, 3, 4]) {
moveRow(i, "L");
}
}
function moveRight() {
for (let i of [1, 2, 3, 4]) {
moveRow(i, "R");
}
}
function getColumnValues(colNumber) {
let colValues = [];
for (let i = colNumber - 1; i < 16; i += 4) {
colValues.push(
Number(document.querySelector(`#id_${i}`).textContent)
);
}
return colValues;
}
function moveColumn(colNumber, direction) {
let colValues = getColumnValues(colNumber);
if (direction == "U") {
colValues = shiftArrayLeft(colValues);
colValues = combineArrayLeft(colValues);
colValues = shiftArrayLeft(colValues);
}
if (direction == "D") {
colValues = shiftArrayRight(colValues);
colValues = combineArrayRight(colValues);
colValues = shiftArrayRight(colValues);
}
let counter = 0;
for (let i = colNumber - 1; i < 16; i += 4) {
document.querySelector(`#id_${i}`).textContent = colValues[counter];
counter++;
}
}
function moveUp() {
for (let i of [1, 2, 3, 4]) {
moveColumn(i, "U");
}
}
function moveDown() {
for (let i of [1, 2, 3, 4]) {
moveColumn(i, "D");
}
}
function keyupHandler(event) {
switch (event.keyCode) {
case 37:
moveLeft();
break;
case 38:
moveUp();
break;
case 39:
moveRight();
break;
case 40:
moveDown();
break;
}
generate();
if (checkForWin()) {
document.body.removeEventListener("keyup", keyupHandler);
document.querySelector("#result").textContent = "YOU WIN!";
} else if (isGameOver()) {
document.body.removeEventListener("keyup", keyupHandler);
document.querySelector("#result").innerText = "Game Over";
}
}
document.body.addEventListener("keyup", keyupHandler);
function checkAdjacent(values) {
let index = values.length - 1;
while (index > 0) {
if (values[index] === values[index - 1]) {
return true;
}
index--;
}
return false;
}
function isGameOver() {
let allBlocks = document.querySelectorAll(".grid > div");
let emptyBlocks = Array.from(allBlocks).filter(
(a) => a.textContent == 0
);
if (emptyBlocks.length != 0) {
return false;
}
for (let rowNumber of [1, 2, 3, 4]) {
let rowValues = [];
for (let i = 4 * (rowNumber - 1); i < 4 * rowNumber; i++) {
rowValues.push(
Number(document.querySelector(`#id_${i}`).textContent)
);
if (
rowValues.length > 1 &&
rowValues[i % 4] == rowValues[(i % 4) - 1]
) {
return false;
}
}
}
for (let colNumber of [1, 2, 3, 4]) {
let colValues = [];
let count = 0;
for (let i = colNumber - 1; i < 16; i += 4) {
colValues.push(
Number(document.querySelector(`#id_${i}`).textContent)
);
if (
colValues.length > 1 &&
colValues[count] == colValues[count - 1]
) {
return false;
}
count++;
}
}
return true;
}
function checkForWin() {
let allBlocks = document.querySelectorAll(".grid > div");
let winningBlocks = Array.from(allBlocks).filter(
(a) => a.textContent == 2048
);
return winningBlocks.length != 0;
}
document
.querySelector("#restart-button")
.addEventListener("click", () => {
document.querySelector(".grid").innerHTML = "";
totalScore = 0;
document.querySelector("#score").textContent = totalScore;
document.querySelector("#result").innerText =
"Join the numbers and get to the 2048 tile!";
createBoard();
generate();
generate();
document.body.addEventListener("keyup", keyupHandler);
});
</script>
</body>
</html>