Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
233 lines (211 loc) · 8.13 KB
/
Copy pathscript.js
File metadata and controls
233 lines (211 loc) · 8.13 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
const boardSize = 4;
let board = Array.from({ length: boardSize }, () => Array(boardSize).fill(null));
let gameOver = false;
let score = 0;
let highScore = localStorage.getItem('highScore') ? parseInt(localStorage.getItem('highScore')) : 0; // Initialize from local storage
document.getElementById('high-score').innerText = highScore
function setHighScore(newScore) {
if (newScore > highScore) {
highScore = newScore;
localStorage.setItem('highScore', highScore);
}
document.getElementById('high-score').innerText = highScore
}
function updateScore(points) {
score += points;
document.getElementById('score').innerText = `Score: ${score}`;
// Update high score whenever the score increases
setHighScore(score);
}
function initGame() {
resetBoard();
addRandomTile();
addRandomTile();
renderBoard();
}
function resetBoard() {
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
board[row][col] = null;
}
}
gameOver = false;
score = 0;
document.getElementById('score').innerText = `Score: ${score}`;
document.getElementById('gameOver').style.display = 'none';
}
function addRandomTile() {
const emptyCells = [];
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (!board[row][col]) {
emptyCells.push({ row, col });
}
}
}
if (emptyCells.length > 0) {
const { row, col } = emptyCells[Math.floor(Math.random() * emptyCells.length)];
board[row][col] = Math.random() < 0.9 ? 2 : 4;
}
}
function renderBoard() {
const gameBoard = document.getElementById('gameBoard');
gameBoard.innerHTML = '';
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
const tile = document.createElement('div');
tile.classList.add('tile');
if (board[row][col]) {
tile.textContent = board[row][col];
tile.style.backgroundColor = getTileColor(board[row][col]);
}
gameBoard.appendChild(tile);
}
}
}
function getTileColor(value) {
switch (value) {
case 2: return '#eee4da';
case 4: return '#ede0c8';
case 8: return '#f2b179';
case 16: return '#f59563';
case 32: return '#f67c5f';
case 64: return '#f65e3b';
case 128: return '#edcf72';
case 256: return '#edcc61';
case 512: return '#edc950';
case 1024: return '#edc53f';
case 2048: return '#edc22e';
default: return '#eee4da';
}
}
function checkGameOver() {
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (!board[row][col]) {
return false;
}
if ((row > 0 && board[row][col] === board[row - 1][col]) ||
(row < boardSize - 1 && board[row][col] === board[row + 1][col]) ||
(col > 0 && board[row][col] === board[row][col - 1]) ||
(col < boardSize - 1 && board[row][col] === board[row][col + 1])) {
return false;
}
}
}
return true;
}
function newGame() {
resetBoard();
addRandomTile();
addRandomTile();
renderBoard();
gameOver = false;
document.getElementById('gameOver').style.display = 'none';
}
document.addEventListener('keydown', (event) => {
if (gameOver) return;
const key = event.key;
let moved = false;
switch (key) {
case 'ArrowUp':
for (let col = 0; col < boardSize; col++) {
let newRow = 0;
let points = 0;
for (let row = 1; row < boardSize; row++) {
if (board[row][col]) {
if (!board[newRow][col] || board[newRow][col] === board[row][col]) {
board[newRow][col] = board[newRow][col] ? board[newRow][col] * 2 : board[row][col];
points += board[newRow][col];
board[row][col] = null;
newRow++;
} else {
newRow++;
}
}
}
if (points > 0) {
updateScore(points);
}
}
moved = true;
break;
case 'ArrowDown':
for (let col = 0; col < boardSize; col++) {
let newRow = boardSize - 1;
let points = 0;
for (let row = boardSize - 2; row >= 0; row--) {
if (board[row][col]) {
if (!board[newRow][col] || board[newRow][col] === board[row][col]) {
board[newRow][col] = board[newRow][col] ? board[newRow][col] * 2 : board[row][col];
points += board[newRow][col];
board[row][col] = null;
newRow--;
} else {
newRow--;
}
}
}
if (points > 0) {
updateScore(points);
}
}
moved = true;
break;
case 'ArrowLeft':
for (let row = 0; row < boardSize; row++) {
let newCol = 0;
let points = 0;
for (let col = 1; col < boardSize; col++) {
if (board[row][col]) {
if (!board[row][newCol] || board[row][newCol] === board[row][col]) {
board[row][newCol] = board[row][newCol] ? board[row][newCol] * 2 : board[row][col];
points += board[row][newCol];
board[row][col] = null;
newCol++;
} else {
newCol++;
}
}
}
if (points > 0) {
updateScore(points);
}
}
moved = true;
break;
case 'ArrowRight':
for (let row = 0; row < boardSize; row++) {
let newCol = boardSize - 1;
let points = 0;
for (let col = boardSize - 2; col >= 0; col--) {
if (board[row][col]) {
if (!board[row][newCol] || board[row][newCol] === board[row][col]) {
board[row][newCol] = board[row][newCol] ? board[row][newCol] * 2 : board[row][col];
points += board[row][newCol];
board[row][col] = null;
newCol--;
} else {
newCol--;
}
}
}
if (points > 0) {
updateScore(points);
}
}
moved = true;
break;
}
if (moved) {
addRandomTile();
renderBoard();
gameOver = checkGameOver();
if (gameOver) {
document.getElementById('gameOver').style.display = 'block';
document.getElementById('final-score').innerText = score;
setHighScore(score);//don't need to check if score is bigger, since done in the func
}
}
});
initGame();