-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
327 lines (304 loc) · 8.75 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
<!DOCTYPE html>
<html>
<head>
<title>2048</title>
<style>
td.tile {
border: 1px solid black;
width: 70px;
height: 70px;
}
i {
border: solid black;
border-width: 0 3px 3px 0;
display: inline-block;
padding: 3px;
}
.right {
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
}
.left {
transform: rotate(135deg);
-webkit-transform: rotate(135deg);
}
.up {
transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
}
.down {
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
</style>
</head>
<body onload="init()">
<h1 style="text-align: center">2048</h1>
<table style="margin-left: auto; margin-right: auto; text-align: center">
<tr>
<td></td>
<td>
<button type="button" onclick="upClicked()">
<i class="up"></i>
</button>
</td>
<td></td>
</tr>
<tr>
<td>
<button type="button" onclick="leftClicked()">
<i class="left"></i>
</button>
</td>
<td>
<table>
<tr>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
</tr>
<tr>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
</tr>
<tr>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
</tr>
<tr>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
<td class="tile"></td>
</tr>
</table>
</td>
<td>
<button type="button" onclick="rightClicked()">
<i class="right"></i>
</button>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="button" onclick="downClicked()">
<i class="down"></i>
</button>
</td>
<td></td>
</tr>
</table>
<script>
var currentTileValues = [];
var previousTileValues = [];
var rows = 4;
var columns = 4;
function init() {
let initialInsertionMatrix = [];
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
rowValues = [];
rowValuesInitialInsertionMatrix = [];
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
rowValues.push(0);
rowValuesInitialInsertionMatrix.push(2);
}
currentTileValues.push(rowValues);
initialInsertionMatrix.push(rowValuesInitialInsertionMatrix);
}
insertTile(initialInsertionMatrix);
insertTile(initialInsertionMatrix);
updateDisplay();
}
var updateDisplay=function updateDisplayedTiles() {
let tileElements = Array.from(document.getElementsByClassName("tile"));
tileElements.forEach(function (tileElement, index) {
let rowIndex = Math.floor(index / columns);
let columnIndex = index % columns;
let value = currentTileValues[rowIndex][columnIndex];
if (value != 0) {
tileElement.innerHTML = value;
} else {
tileElement.innerHTML = "";
}
});
}
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function shuffle(arrayValues) {
values = [];
while (arrayValues.length > 0) {
removeIndex = getRandomInteger(1, arrayValues.length) - 1;
values.push(arrayValues.splice(removeIndex, 1)[0]);
}
return values;
}
function generateInsertionMatrix() {
/*
Below two parameters are used to generate one value which is doubled than the least value at random times
so as to increase the complexity of game
*/
doubledTileMinIndex = 30;
doubledTileMaxIndex = 50;
minInsertionValue = 2;
doubledTileIndex = getRandomInteger(doubledTileMinIndex, doubledTileMaxIndex);
let values = [];
for (let index = 0; index < doubledTileIndex; index++) {
values.push(minInsertionValue);
}
values.push(minInsertionValue * 2);
values = shuffle(values);
let insertionValues = values.slice(0, 16);
values = [];
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
rowValues = [];
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
rowValues.push(insertionValues[(rowIndex * columns) + columnIndex]);
}
values.push(rowValues);
}
return values;
}
function insertTile(insertionMatrix) {
let blankIndices = [];
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
if (currentTileValues[rowIndex][columnIndex] == 0) {
blankIndex = [rowIndex, columnIndex];
blankIndices.push(blankIndex);
}
}
}
insertionIndex = blankIndices[getRandomInteger(1, blankIndices.length) - 1];
currentTileValues[insertionIndex[0]][insertionIndex[1]] = insertionMatrix[insertionIndex[0]][insertionIndex[1]];
}
function buttonClicked(callback) {
if (valuesUnchanged(previousTileValues, currentTileValues)) {
currentTileValues = previousTileValues;
}
else {
insertTile(generateInsertionMatrix());
}
callback();
}
function pullValues(inputArray) {
pulledArray = [];
inputArray.forEach(function (value) {
if (value != 0) {
pulledArray.push(value);
}
});
for (let index = pulledArray.length; index < inputArray.length; index++) {
pulledArray.push(0);
}
return pulledArray;
}
function mergeValues(inputArray) {
inputArray = pullValues(inputArray);
for (let index = 0; index < inputArray.length - 1; index++) {
if (inputArray[index] == inputArray[index + 1]) {
inputArray[index] = inputArray[index] * 2;
inputArray[index + 1] = 0;
index = index + 1;
}
}
inputArray = pullValues(inputArray);
return inputArray;
}
function copyCurrentTileValues() {
values = [];
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
rowValues = [];
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
rowValues.push(currentTileValues[rowIndex][columnIndex]);
}
values.push(rowValues);
}
return values;
}
function valuesUnchanged(previousValues, currentValues) {
previousValues=previousValues.flat();
currentValues=currentValues.flat();
let match=true;
for (let index = 0; index < previousValues.length; index++) {
if(match) {
if (previousValues[index] != currentValues[index]) {
match=false;
}
}
}
return match;
}
function upClicked() {
previousTileValues = copyCurrentTileValues();
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
columnValues = [];
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
columnValues.push(currentTileValues[rowIndex][columnIndex]);
}
columnValues = mergeValues(columnValues);
let index = 0;
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
currentTileValues[rowIndex][columnIndex] = columnValues[index];
index = index + 1;
}
}
buttonClicked(updateDisplay);
}
function downClicked() {
previousTileValues = copyCurrentTileValues();
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
columnValues = [];
for (let rowIndex = rows - 1; rowIndex >= 0; rowIndex--) {
columnValues.push(currentTileValues[rowIndex][columnIndex]);
}
columnValues = mergeValues(columnValues);
let index = 0;
for (let rowIndex = rows - 1; rowIndex >= 0; rowIndex--) {
currentTileValues[rowIndex][columnIndex] = columnValues[index];
index = index + 1;
}
}
buttonClicked(updateDisplay);
}
function leftClicked() {
previousTileValues = copyCurrentTileValues();
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
rowValues = [];
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
rowValues.push(currentTileValues[rowIndex][columnIndex]);
}
rowValues = mergeValues(rowValues);
let index = 0;
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
currentTileValues[rowIndex][columnIndex] = rowValues[index];
index = index + 1;
}
}
buttonClicked(updateDisplay);
}
function rightClicked() {
previousTileValues = copyCurrentTileValues();
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
rowValues = [];
for (let columnIndex = columns - 1; columnIndex >= 0; columnIndex--) {
rowValues.push(currentTileValues[rowIndex][columnIndex]);
}
rowValues = mergeValues(rowValues);
let index = 0;
for (let columnIndex = columns - 1; columnIndex >= 0; columnIndex--) {
currentTileValues[rowIndex][columnIndex] = rowValues[index];
index = index + 1;
}
}
buttonClicked(updateDisplay);
}
</script>
</body>
</html>