-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
253 lines (215 loc) · 5.08 KB
/
script.js
File metadata and controls
253 lines (215 loc) · 5.08 KB
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
'use strict';
const CELL_START = 2;
const CELL_EMPTY = '';
const CONTAINER_ID = 'game';
const DEFAULT_SIZE = 4;
/**
* Clear element.
*
* @param {HTMLElement} element object.
*/
function clearElement(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
}
/**
* Cell value.
*
* @param {HTMLTableDataCellElement} cell cell element.
* @param {number|string} value cell value.
*/
function cellValue(cell, value) {
cell.innerHTML = value;
if (value === CELL_EMPTY) {
cell.className = 'empty';
} else if (value < 1024) {
cell.className = 'value' + value;
} else {
cell.className = 'value1024';
}
}
/**
* Inits the game table.
*
* @param {HTMLDivElement} game game div.
* @param {number} size table size.
* @return {HTMLTableDataCellElement[]} cell elements.
*/
function initTable(game, size) {
const cells = [];
clearElement(game);
const table = document.createElement('table');
const tbody = document.createElement('tbody');
table.appendChild(tbody);
for (let i = 0; i < size; i++) {
const tr = document.createElement('tr');
for (let j = 0; j < size; j++) {
const td = document.createElement('td');
cellValue(td, CELL_EMPTY);
tr.appendChild(td);
cells.push(td);
}
tbody.appendChild(tr);
}
game.appendChild(table);
return cells;
}
/**
* Step by adding the next value.
*
* @param {HTMLTableDataCellElement[]} cells cell elements.
*/
function step(cells) {
const empty = cells.filter((cell) => cell.innerHTML === CELL_EMPTY);
if (empty.length > 0) {
const index = Math.floor(Math.random() * empty.length);
cellValue(empty[index], CELL_START);
} else {
console.log('Game over');
}
}
/**
* Merge cells.
*
* @param {HTMLTableDataCellElement[]} cells cell elements.
* @param {number} size table size.
* @param {number} topIterate top iterate.
* @param {number} bottomIterate bottom iterate.
* @param {boolean} backward backward flow.
*/
function merge(cells, size, topIterate, bottomIterate, backward) {
for (let i = 0; i < size; i++) {
const begin = i * topIterate;
let values = [];
for (let j = 0; j < size; j++) {
values.push(cells[begin + (j * bottomIterate)]);
}
values = values.map((cell) => cell.innerHTML);
if (backward) {
values = values.reverse();
}
values = values.reduce((filled, value) => {
if (value !== CELL_EMPTY) {
if ((filled.length > 0) && (value === filled[filled.length - 1])) {
filled[filled.length - 1] *= 2;
} else {
filled.push(value);
}
}
return filled;
}, []);
const pad = size - values.length;
if (pad > 0) {
values = Array(pad).fill(CELL_EMPTY).concat(values);
}
if (backward) {
values = values.reverse();
}
for (let j = 0; j < size; j++) {
cellValue(cells[begin + (j * bottomIterate)], values[j]);
}
}
}
/**
* Game 2048.
*
* @author Onur Cinar
*/
class Game2048 {
/**
* Constructor.
*
* @param {number} size table size default 4.
*/
constructor(size = DEFAULT_SIZE) {
this.game = document.getElementById(CONTAINER_ID);
this.reset(size);
}
/**
* Reset game with size.
*
* @param {number} size table size default 4.
*/
reset(size = DEFAULT_SIZE) {
this.size = size;
this.cells = initTable(this.game, size);
step(this.cells);
}
/**
* Up combine.
*/
up() {
merge(this.cells, this.size, 1, this.size, true);
step(this.cells);
}
/**
* Down combine.
*/
down() {
merge(this.cells, this.size, 1, this.size, false);
step(this.cells);
}
/**
* Left combine.
*/
left() {
merge(this.cells, this.size, this.size, 1, true);
step(this.cells);
}
/**
* Right combine.
*/
right() {
merge(this.cells, this.size, this.size, 1, false);
step(this.cells);
}
}
window.addEventListener('load', () => {
const game2048 = new Game2048();
let touchX = 0;
let touchY = 0;
document.getElementById('button4').addEventListener('click', () => {
game2048.reset(4);
});
document.getElementById('button5').addEventListener('click', () => {
game2048.reset(5);
});
document.addEventListener('keydown', (event) => {
switch (event.key) {
case 'ArrowUp':
game2048.up();
break;
case 'ArrowDown':
game2048.down();
break;
case 'ArrowLeft':
game2048.left();
break;
case 'ArrowRight':
game2048.right();
break;
}
});
document.addEventListener('touchstart', (event) => {
touchX = event.changedTouches[0].clientX;
touchY = event.changedTouches[0].clientY;
});
document.addEventListener('touchend', (event) => {
const deltaX = event.changedTouches[0].clientX - touchX;
const deltaY = event.changedTouches[0].clientY - touchY;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
if (deltaX < 0) {
game2048.left();
} else {
game2048.right();
}
} else {
if (deltaY < 0) {
game2048.up();
} else {
game2048.down();
}
}
});
});