-
Notifications
You must be signed in to change notification settings - Fork 3
/
HTML 2048.html
212 lines (194 loc) · 6.02 KB
/
HTML 2048.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>2048</title>
<style>
#field {
border: 1px solid black;
}
.cell {
border: 1px solid black;
text-align: center;
width: 80px;
height: 80px;
}
</style>
</head>
<body>
<table id="field"></table>
<script>
var size = 4;
var htmlElements;
var cell;
function createField() {
if(htmlElements) {
return;
}
htmlElements = [];
var table = document.getElementById('field');
for (var y = 0; y < size; y++) {
var tr = document.createElement('tr');
var trElements = [];
for (var x = 0; x < size; x++) {
var td = document.createElement('td');
td.setAttribute('class', 'cell');
tr.appendChild(td);
trElements.push(td);
}
htmlElements.push(trElements);
table.appendChild(tr);
}
}
function createCells() {
cells = [];
for(var y = 0; y < size; y++) {
cells.push(new Array(size).fill(0));
}
}
function generateInEmptyCell() {
var x, y;
do {
x = Math.floor(Math.random() * size);
y = Math.floor(Math.random() * size);
if(cells[y][x] === 0) {
cells[y][x] = Math.random() >= 0.9 ? 4 : 2;
break;
}
} while(true);
}
function draw() {
for(var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
var td = htmlElements[y][x];
var v = cells[y][x];
td.innerHTML = v === 0 ? '' : String(v);
if (v === 0) {
td.setAttribute('style', 'background-color: white');
} else {
var h = 20 + 24 * Math.log2(2048 / v);
td.setAttribute('style', 'background-color: hsl(' + h + ', 100%, 50%)');
}
}
}
}
function slide(array, size) {
function filterEmpty(a) {
return a.filter(x => x !== 0);
}
array = filterEmpty(array);
if(array.length > 0) {
for(var i = 0; i < array.length - 1; i++) {
if(array[i] === array[i + 1]) {
array[i] *= 2;
array[i + 1] = 0;
}
}
}
array = filterEmpty(array);
while (array.length < size) {
array.push(0);
}
return array;
}
function slideLeft() {
var changed = false;
for(var y = 0; y < size; y++) {
var old = Array.from(cells[y]);
cells[y] = slide(cells[y], size);
changed = changed || (cells[y].join(',') !== old.join(','));
}
return changed;
}
function swap(x1, y1, x2, y2) {
var tmp = cells[y1][x1];
cells[y1][x1] = cells[y2][x2];
cells[y2][x2] = tmp;
}
function mirror() {
for (var y = 0; y < size; y++) {
for (var xLeft = 0, xRight = size - 1; xLeft < xRight; xLeft++, xRight--) {
swap(xLeft, y, xRight, y);
}
}
}
function transpose() {
for(var y = 0; y < size; y++){
for (var x = 0; x < y; x++) {
swap(x, y, y, x);
}
}
}
function moveLeft() {
return slideLeft();
}
function moveRight() {
mirror();
var changed = moveLeft();
mirror();
return changed;
}
function moveUp() {
transpose();
var changed = moveLeft();
transpose();
return changed;
}
function moveDown() {
transpose();
var changed = moveRight();
transpose();
return changed;
}
function isGameOver() {
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
if (cells[y][x] === 0) {
return false;
}
}
}
for (var y = 0; y < size - 1; y++) {
for (var x = 0; x < size - 1; x++) {
var c = cells[y][x]
if (c !== 0 && (c === cells[y + 1][x] || c === cells[y][x + 1])) {
return false;
}
}
}
return true;
}
document.addEventListener('keydown', function (e) {
var code = e.keyCode;
var ok;
switch (code) {
case 40: ok = moveDown(); break;
case 38: ok = moveUp(); break;
case 37: ok = moveLeft(); break;
case 39: ok = moveRight(); break;
default: return;
}
if(ok) {
generateInEmptyCell();
draw();
}
if(isGameOver()) {
setTimeout(function () {
alert('Game Over');
init();
}, 1000);
}
});
function init() {
createField();
createCells();
new Array(3).fill(0).forEach(generateInEmptyCell);
draw();
}
init();
</script>
</body>
</html>