-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
234 lines (203 loc) · 6.16 KB
/
game.js
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
const gridContainer = document.querySelector('.grid-container');
const scoreDisplay = document.getElementById('score');
let grid = [];
let score = 0;
function setupGame() {
grid = createEmptyGrid();
addRandomTile();
addRandomTile();
renderGrid();
updateScoreDisplay();
}
function createEmptyGrid() {
return Array(4).fill(null).map(() => Array(4).fill(0));
}
function addRandomTile() {
let emptyCells = [];
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (grid[i][j] === 0) {
emptyCells.push({ row: i, col: j });
}
}
}
if (emptyCells.length > 0) {
let randomIndex = Math.floor(Math.random() * emptyCells.length);
let { row, col } = emptyCells[randomIndex];
grid[row][col] = Math.random() < 0.9 ? 2 : 4;
}
}
function renderGrid() {
gridContainer.innerHTML = '';
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
let tile = document.createElement('div');
tile.classList.add('grid-cell');
if (grid[i][j] !== 0) {
tile.textContent = grid[i][j];
tile.classList.add(`tile-${grid[i][j]}`);
}
gridContainer.appendChild(tile);
}
}
}
function handleInput(direction) {
let moved = false;
switch (direction) {
case 'ArrowUp':
moved = moveTilesUp();
break;
case 'ArrowDown':
moved = moveTilesDown();
break;
case 'ArrowLeft':
moved = moveTilesLeft();
break;
case 'ArrowRight':
moved = moveTilesRight();
break;
}
if (moved) {
addRandomTile();
renderGrid();
checkGameOver();
}
}
function mergeTiles(tiles) {
for (let i = 0; i < tiles.length - 1; i++) {
if (tiles[i] === tiles[i + 1]) {
tiles[i] *= 2;
score += tiles[i]; // Update the score
tiles.splice(i + 1, 1);
}
}
return tiles;
}
function moveAndMerge(rowOrCol) {
const filteredRowOrCol = rowOrCol.filter(num => num !== 0);
const mergedRowOrCol = mergeTiles(filteredRowOrCol);
const finalRowOrCol = mergedRowOrCol.concat(Array(4 - mergedRowOrCol.length).fill(0));
return finalRowOrCol;
}
function moveTilesUp() {
let moved = false;
for (let col = 0; col < 4; col++) {
const colData = [grid[0][col], grid[1][col], grid[2][col], grid[3][col]];
const newColData = moveAndMerge(colData);
for (let row = 0; row < 4; row++) {
if (grid[row][col] !== newColData[row]) moved = true;
grid[row][col] = newColData[row];
}
}
return moved;
}
function moveTilesDown() {
let moved = false;
for (let col = 0; col < 4; col++) {
const colData = [grid[3][col], grid[2][col], grid[1][col], grid[0][col]];
const newColData = moveAndMerge(colData);
for (let row = 3; row >= 0; row--) {
if (grid[row][col] !== newColData[3 - row]) moved = true;
grid[row][col] = newColData[3 - row];
}
}
return moved;
}
function moveTilesLeft() {
let moved = false;
for (let row = 0; row < 4; row++) {
const rowData = [grid[row][0], grid[row][1], grid[row][2], grid[row][3]];
const newRowData = moveAndMerge(rowData);
for (let col = 0; col < 4; col++) {
if (grid[row][col] !== newRowData[col]) moved = true;
grid[row][col] = newRowData[col];
}
}
return moved;
}
function moveTilesRight() {
let moved = false;
for (let row = 0; row < 4; row++) {
const rowData = [grid[row][3], grid[row][2], grid[row][1], grid[row][0]];
const newRowData = moveAndMerge(rowData);
for (let col = 3; col >= 0; col--) {
if (grid[row][col] !== newRowData[3 - col]) moved = true;
grid[row][col] = newRowData[3 - col];
}
}
return moved;
}
function checkGameOver() {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (grid[i][j] === 0) {
return;
}
}
}
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 3; j++) {
if (grid[i][j] === grid[i][j + 1]) {
return;
}
}
}
for (let j = 0; j < 4; j++) {
for (let i = 0; i < 3; i++) {
if (grid[i][j] === grid[i + 1][j]) {
return;
}
}
}
alert('Game Over!');
}
function updateScoreDisplay() {
scoreDisplay.textContent = score;
}
document.addEventListener('keyup', event => {
const key = event.key;
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(key)) {
handleInput(key);
updateScoreDisplay(); // Update score after each move
}
});
const arrows = document.querySelectorAll('.arrow');
function handleArrowInput(direction) {
const keyMap = {
'left': 'ArrowLeft',
'right': 'ArrowRight',
'up': 'ArrowUp',
'down': 'ArrowDown',
};
const arrowKey = keyMap[direction];
handleInput(arrowKey);
updateScoreDisplay(); // Update score after each move
}
arrows.forEach(arrow => {
arrow.addEventListener('touchstart', (event) => {
handleArrowInput(arrow.classList[1]);
}, { passive: true });
arrow.addEventListener('click', (event) => {
handleArrowInput(arrow.classList[1]);
});
});
const themeButton = document.getElementById('theme-button');
function toggleDarkTheme() {
document.body.classList.toggle('dark-theme');
document.body.classList.toggle('light-theme');
storeCurrentTheme();
}
function storeCurrentTheme() {
const isDarkTheme = document.body.classList.contains('dark-theme');
localStorage.setItem('theme', isDarkTheme ? 'dark' : 'light');
}
function loadTheme() {
const storedTheme = localStorage.getItem('theme');
if (storedTheme === 'dark') {
document.body.classList.add('dark-theme');
document.body.classList.remove('light-theme'); // Ensure light theme is removed
}
}
themeButton.addEventListener('click', toggleDarkTheme);
loadTheme();
setupGame();