-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathgame.js
215 lines (198 loc) · 6.99 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
document.addEventListener("DOMContentLoaded", () => {
const gridDisplay = document.querySelector('.grid-container');
const scoreDisplay = document.querySelector('.score-container');
const bestDisplay = document.querySelector('.best-container');
const newGameButton = document.querySelector('.new-game-button');
const tileContainer = document.querySelector('.tile-container');
let tiles = [];
let score = 0;
let best = 0;
// Create the playing board
function createBoard() {
gridDisplay.innerHTML = ''; // Clear previous grid cells
tiles = []; // Reset tiles array
for (let i = 0; i < 16; i++) {
let tile = document.createElement('div');
tile.classList.add('grid-cell');
gridDisplay.appendChild(tile);
tiles.push(0);
}
addNumber();
addNumber();
}
// Add a number to the board
function addNumber() {
let emptyTiles = [];
tiles.forEach((tile, index) => {
if (tile === 0) emptyTiles.push(index);
});
if (emptyTiles.length > 0) {
let randomNumber = emptyTiles[Math.floor(Math.random() * emptyTiles.length)];
tiles[randomNumber] = 2;
displayTiles();
}
}
// Display the tiles on the board
function displayTiles() {
tileContainer.innerHTML = '';
if(window.innerWidth > 530){
tiles.forEach((tile, index) => {
if (tile !== 0) {
let tileElement = document.createElement('div');
tileElement.classList.add('tile');
tileElement.classList.add(`tile-${tile}`);
tileElement.innerText = tile;
tileElement.style.top = `${Math.floor(index / 4) * 110}px`;
tileElement.style.left = `${(index % 4) * 110}px`;
tileContainer.appendChild(tileElement);
}
});
} else {
tiles.forEach((tile, index) => {
if (tile !== 0) {
let tileElement = document.createElement('div');
tileElement.classList.add('tile');
tileElement.classList.add(`tile-${tile}`);
tileElement.innerText = tile;
tileElement.style.top = `${Math.floor(index / 4) * 80}px`;
tileElement.style.left = `${(index % 4) * 80}px`;
tileContainer.appendChild(tileElement);
}
});
}
checkForGameOver();
checkForWin();
}
// Check for game over condition
function checkForGameOver() {
if (!tiles.includes(0)) {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
let currentIndex = i * 4 + j;
if (j < 3 && tiles[currentIndex] === tiles[currentIndex + 1]) return; // Check horizontal
if (i < 3 && tiles[currentIndex] === tiles[currentIndex + 4]) return; // Check vertical
}
}
alert("Game Over! No more moves available.");
}
}
// Check for 2048 tile
function checkForWin() {
if (tiles.includes(2048)) {
alert("Congratulations! You've reached the 2048 tile!");
}
}
// Handle swipe events
function moveTiles(direction) {
let newTiles = [...tiles];
for (let i = 0; i < 4; i++) {
let row = [];
for (let j = 0; j < 4; j++) {
if (direction === 'right' || direction === 'left') {
row.push(newTiles[i * 4 + j]);
} else {
row.push(newTiles[j * 4 + i]);
}
}
if (direction === 'right' || direction === 'down') {
row = row.reverse();
}
row = slide(row);
if (direction === 'right' || direction === 'down') {
row = row.reverse();
}
for (let j = 0; j < 4; j++) {
if (direction === 'right' || direction === 'left') {
newTiles[i * 4 + j] = row[j];
} else {
newTiles[j * 4 + i] = row[j];
}
}
}
if (JSON.stringify(newTiles) !== JSON.stringify(tiles)) {
tiles = newTiles;
addNumber();
displayTiles();
}
}
function slide(row) {
let newRow = row.filter(tile => tile);
for (let i = 0; i < newRow.length - 1; i++) {
if (newRow[i] === newRow[i + 1]) {
newRow[i] *= 2;
score += newRow[i];
newRow.splice(i + 1, 1);
newRow.push(0);
}
}
while (newRow.length < 4) {
newRow.push(0);
}
return newRow;
}
// Listen for key presses
document.addEventListener('keydown', e => {
if (e.key === 'ArrowRight') {
moveTiles('right');
} else if (e.key === 'ArrowLeft') {
moveTiles('left');
} else if (e.key === 'ArrowUp') {
moveTiles('up');
} else if (e.key === 'ArrowDown') {
moveTiles('down');
}
scoreDisplay.textContent = score;
if (score > best) {
best = score;
bestDisplay.textContent = best;
}
});
// Variables to store touch positions
let touchStartX = 0;
let touchStartY = 0;
let touchEndX = 0;
let touchEndY = 0;
// Function to handle swipe detection
function handleSwipe() {
const diffX = touchEndX - touchStartX;
const diffY = touchEndY - touchStartY;
if (Math.abs(diffX) > Math.abs(diffY)) {
// Horizontal swipe
if (diffX > 0) {
moveTiles('right');
} else {
moveTiles('left');
}
} else {
// Vertical swipe
if (diffY > 0) {
moveTiles('down');
} else {
moveTiles('up');
}
}
scoreDisplay.textContent = score;
if (score > best) {
best = score;
bestDisplay.textContent = best;
}
}
// Event listeners for touch events
document.addEventListener('touchstart', e => {
touchStartX = e.changedTouches[0].screenX;
touchStartY = e.changedTouches[0].screenY;
});
document.addEventListener('touchend', e => {
touchEndX = e.changedTouches[0].screenX;
touchEndY = e.changedTouches[0].screenY;
handleSwipe();
});
// Start a new game
newGameButton.addEventListener('click', () => {
score = 0;
scoreDisplay.textContent = "Current score = "+score;
bestDisplay.textContent = "Best score = "+best;
createBoard();
});
createBoard();
});