-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.html
More file actions
231 lines (227 loc) · 7.9 KB
/
2048.html
File metadata and controls
231 lines (227 loc) · 7.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2048</title>
<style>
#table { border-collapse: collapse; user-select: none; }
#table td {
border: 10px solid #bbada0; width: 116px; height: 128px;
font-size: 50px; font-weight: bold; text-align: center;
}
#score { user-select: none; }
.color-2 { background-color: #eee4da; color: #776e65;}
.color-4 { background-color: #eee1c9; color: #776e65;}
.color-8 { background-color: #f3b27a; color: 'white';}
.color-16 { background-color: #f69664; color: 'white';}
.color-32 { background-color: #f77c5f; color: 'white';}
.color-64 { background-color: #f75f3b; color: 'white';}
.color-128 { background-color: #edd073; color: #776e65;}
.color-256 { background-color: #edcc62; color: #776e65;}
.color-512 { background-color: #edc950; color: #776e65;}
.color-1024 { background-color: #edc53f; color: #776e65;}
.color-2048 { background-color: #edc22e; color: #776e65;}
</style>
</head>
<body>
<table id="table"></table>
<div id="score">0</div>
<script>
const $table = document.querySelector('#table');
const $score = document.querySelector('#score');
let data = [];
// fragment는 직접 그려지는 것이 아니라, 메모리에만 저장됨
// 결과적으로 딱 1번만 그려서 성능 향상
function startGame() {
const $fragment = document.createDocumentFragment();
[1,2,3,4].forEach(function () {
const rowData = [];
data.push(rowData);
const $tr = document.createElement('tr');
[1,2,3,4].forEach(() => {
rowData.push(0);
const $td = document.createElement('td');
$tr.appendChild($td);
});
$fragment.appendChild($tr);
});
$table.appendChild($fragment);
put2ToRandomCell();
draw();
}
function put2ToRandomCell() {
const emptyCells = [];
data.forEach(function (rowData, i) {
rowData.forEach(function (cellData,j) {
if (!cellData) {
emptyCells.push([i,j]);
}
});
});
const randomCell = emptyCells[Math.floor(Math.random()*emptyCells.length)];
data[randomCell[0]][randomCell[1]] = 2;
}
function draw() {
data.forEach(function (rowData, i) {
rowData.forEach(function (cellData, j) {
const $target = $table.children[i].children[j];
if (cellData > 0) {
$target.textContent = cellData;
$target.className = 'color-'+cellData;
} else {
$target.textContent = '';
$target.className = '';
}
});
});
}
function moveCells(direction) {
switch (direction) {
case 'left': {
const newData = [[], [], [], []];
data.forEach((rowData, i) => {
rowData.forEach((cellData, j) => {
if (cellData) {
const currentRow = newData[i];
const prevData = currentRow.at(-1);
console.log(prevData, cellData);
if (prevData === cellData) { // 이전 값과 지금 값이 같으면
const score = parseInt($score.textContent);
$score.textContent = score + currentRow.at(-1) * 2;
currentRow[currentRow.length - 1] *= 2;
} else {
newData[i].push(cellData);
}
}
});
});
console.log(newData);
[1, 2, 3, 4].forEach((rowData, i) => {
[1, 2, 3, 4].forEach((cellData, j) => {
data[i][j] = Math.abs(newData[i][j]) || 0;
});
});
break;
}
case 'right': {
const newData = [[], [], [], []];
data.forEach((rowData, i) => {
rowData.forEach((cellData, j) => {
if (rowData[3 - j]) {
const currentRow = newData[i];
const prevData = currentRow.at(-1);
if (prevData === rowData[3 - j]) {
const score = parseInt($score.textContent);
$score.textContent = score + currentRow.at(-1) * 2;
currentRow[currentRow.length - 1] *= -2;
} else {
newData[i].push(rowData[3 - j]);
}
}
});
});
console.log(newData);
[1, 2, 3, 4].forEach((rowData, i) => {
[1, 2, 3, 4].forEach((cellData, j) => {
data[i][3 - j] = Math.abs(newData[i][j]) || 0;
});
});
break;
}
case 'up': {
const newData = [[], [], [], []];
data.forEach((rowData, i) => {
rowData.forEach((cellData, j) => {
if (cellData) {
const currentRow = newData[j];
const prevData = currentRow.at(-1);
if (prevData === cellData) {
const score = parseInt($score.textContent);
$score.textContent = score + currentRow.at(-1) * 2;
currentRow[currentRow.length - 1] *= -2;
} else {
newData[j].push(cellData);
}
}
});
});
console.log(newData);
[1, 2, 3, 4].forEach((cellData, i) => {
[1, 2, 3, 4].forEach((rowData, j) => {
data[j][i] = Math.abs(newData[i][j]) || 0;
});
});
break;
}
case 'down': {
const newData = [[], [], [], []];
data.forEach((rowData, i) => {
rowData.forEach((cellData, j) => {
if (data[3 - i][j]) {
const currentRow = newData[j];
const prevData = currentRow.at(-1);
if (prevData === data[3 - i][j]) {
const score = parseInt($score.textContent);
$score.textContent = score + currentRow.at(-1) * 2;
currentRow[currentRow.length - 1] *= -2;
} else {
newData[j].push(data[3 - i][j]);
}
}
});
});
console.log(newData);
[1, 2, 3, 4].forEach((cellData, i) => {
[1, 2, 3, 4].forEach((rowData, j) => {
data[3 - j][i] = Math.abs(newData[i][j]) || 0;
});
});
break;
}
}
if (data.flat().includes(2048)) { // 승리
draw();
setTimeout(() => {
alert('축하합니다. 2048을 만들었습니다!');
}, 0);
} else if (!data.flat().includes(0)) { // 빈칸이 없으면 패배
alert(`패배했습니다... ${$score.textContent}점`);
} else {
put2ToRandomCell();
draw();
}
}
window.addEventListener('keyup', (event) => {
if (event.key === 'ArrowUp') {
moveCells('up');
} else if (event.key === 'ArrowDown') {
moveCells('down');
} else if (event.key === 'ArrowLeft') {
moveCells('left');
} else if (event.key === 'ArrowRight') {
moveCells('right');
}
});
let startCoord;
window.addEventListener('mousedown', (event) => {
startCoord = [event.clientX, event.clientY];
});
window.addEventListener('mouseup', (event) => {
const endCoord = [event.clientX, event.clientY];
const diffX = endCoord[0] - startCoord[0];
const diffY = endCoord[1] - startCoord[1];
if (diffX < 0 && Math.abs(diffX) > Math.abs(diffY)) {
moveCells('left');
} else if (diffX > 0 && Math.abs(diffX) > Math.abs(diffY)) {
moveCells('right');
} else if (diffY > 0 && Math.abs(diffX) <= Math.abs(diffY)) {
moveCells('down');
} else if (diffY < 0 && Math.abs(diffX) <= Math.abs(diffY)) {
moveCells('up');
}
});
startGame();
</script>
</body>
</html>