-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
449 lines (405 loc) · 16.1 KB
/
index.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>grids</title>
<!-- Meta Viewport Tag -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
/* CSS Styles */
body, html {
margin: 0;
padding: 0;
background-color: #080808;
color: white;
font-family: monospace;
text-align: center;
overflow: hidden;
height: 100%;
-webkit-touch-callout: none; /* Disable callouts */
-webkit-user-select: none; /* Disable text selection */
user-select: none;
touch-action: manipulation; /* Prevent double-tap-to-zoom */
}
:root {
--square-size: 50px;
--gap-size: 6px;
}
#main-menu {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#main-menu h1 {
font-size: 5em;
margin: 0;
font-weight: 900;
line-height: 0;
}
#main-menu p {
margin-top: 20px;
}
#play-button {
width: 100%;
padding: 8px 16px;
font-size: 24px;
font-weight: 900;
background-color: blue;
color: white;
border: none;
font-family: inherit;
border-radius: 5px;
touch-action: manipulation; /* Prevent double-tap-to-zoom on the button */
}
#game-container {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
touch-action: none; /* Prevent default touch actions in the game area */
}
#grid {
display: grid;
grid-template-columns: repeat(auto-fill, var(--square-size));
grid-template-rows: repeat(auto-fill, var(--square-size));
gap: var(--gap-size);
}
.square {
width: var(--square-size);
height: var(--square-size);
border: 1px solid rgba(255,255,255,0.1);
box-sizing: border-box;
border-radius: 5px;
-webkit-tap-highlight-color: transparent; /* Remove tap highlight */
-webkit-touch-callout: none;
user-select: none;
touch-action: manipulation; /* Prevent double-tap-to-zoom on squares */
}
.active {
background-color: blue;
border-color: blue;
}
.trail {
background-color: red;
border-color: red;
}
.clickable {
cursor: pointer;
}
</style>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-N5QSZT5GKE"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-N5QSZT5GKE');
</script>
</head>
<body>
<!-- HTML Content -->
<div id="main-menu">
<h1>grids</h1>
<p>High Score: <span id="high-score">0</span></p>
<button id="play-button">Play</button>
</div>
<div id="game-container" style="display: none;">
<div id="grid"></div>
</div>
<script>
// JavaScript Code
let gridSizeX, gridSizeY;
let activeX, activeY;
let timer;
let timeLimit = 1000; // Starts at 1000ms
let score = 0;
let clickCount = 0; // Counts clicks on both blue square and trail squares
let highScore = localStorage.getItem('highScore') || 0;
let trailSquares = []; // Array to store trail positions
const gridElement = document.getElementById('grid');
const highScoreElement = document.getElementById('high-score');
const playButton = document.getElementById('play-button');
const mainMenu = document.getElementById('main-menu');
const gameContainer = document.getElementById('game-container');
highScoreElement.textContent = highScore;
playButton.addEventListener('click', startGame);
window.addEventListener('resize', onResize);
function isMobileDevice() {
return /Mobi|Android|iPhone|iPad|iPod|Opera Mini|IEMobile|Mobile/i.test(navigator.userAgent);
}
function startGame() {
mainMenu.style.display = 'none';
gameContainer.style.display = 'flex';
timeLimit = 1000;
score = 0;
clickCount = 0;
trailSquares = [];
activeX = undefined;
activeY = undefined;
setSquareSize();
setGameContainerHeight();
initializeGrid();
placeActiveSquare();
startTimer();
}
function setSquareSize() {
let root = document.documentElement;
if (isMobileDevice()) {
root.style.setProperty('--square-size', '500px');
root.style.setProperty('--gap-size', '10px');
} else {
root.style.setProperty('--square-size', '50px');
root.style.setProperty('--gap-size', '6px');
}
}
function initializeGrid() {
gridElement.innerHTML = '';
calculateGridSize();
for (let y = 0; y < gridSizeY; y++) {
for (let x = 0; x < gridSizeX; x++) {
const square = document.createElement('div');
square.classList.add('square');
square.dataset.x = x;
square.dataset.y = y;
gridElement.appendChild(square);
}
}
}
function calculateGridSize() {
let squareSize = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--square-size'));
let gapSize = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--gap-size'));
gridSizeX = Math.floor(window.innerWidth / (squareSize + gapSize));
gridSizeY = Math.floor(window.innerHeight / (squareSize + gapSize));
gridElement.style.gridTemplateColumns = `repeat(${gridSizeX}, ${squareSize}px)`;
gridElement.style.gridTemplateRows = `repeat(${gridSizeY}, ${squareSize}px)`;
}
function placeActiveSquare() {
// Remove event listener from the old active square
if (activeX !== undefined && activeY !== undefined) {
const oldSquare = getSquareElement(activeX, activeY);
if (oldSquare) {
oldSquare.classList.remove('active', 'clickable');
oldSquare.removeEventListener('click', onSquareClick);
}
}
let newPosition;
if (activeX === undefined || activeY === undefined) {
// Start at a random empty position
newPosition = getRandomEmptyPosition();
if (!newPosition) {
endGame();
return;
}
activeX = newPosition.x;
activeY = newPosition.y;
} else {
// Move the blue square to a neighboring empty square (not a trail square)
const neighbors = getNeighbors(activeX, activeY);
// Exclude trail positions
const availablePositions = neighbors.filter(pos => !isTrailSquare(pos.x, pos.y));
if (availablePositions.length === 0) {
// No available positions, end game
endGame();
return;
}
const randomNeighbor = availablePositions[Math.floor(Math.random() * availablePositions.length)];
activeX = randomNeighbor.x;
activeY = randomNeighbor.y;
}
const newSquare = getSquareElement(activeX, activeY);
if (!newSquare) {
endGame();
return;
}
newSquare.classList.add('active');
if (trailSquares.length === 0) {
// Only add event listener if there is no trail
newSquare.classList.add('clickable');
newSquare.addEventListener('click', onSquareClick);
}
}
function getRandomEmptyPosition() {
let emptyPositions = [];
for (let x = 0; x < gridSizeX; x++) {
for (let y = 0; y < gridSizeY; y++) {
if (!isTrailSquare(x, y) && !(x === activeX && y === activeY)) {
emptyPositions.push({ x: x, y: y });
}
}
}
if (emptyPositions.length === 0) {
// No empty positions, end game
return null;
}
return emptyPositions[Math.floor(Math.random() * emptyPositions.length)];
}
function isTrailSquare(x, y) {
return trailSquares.some(pos => pos.x === x && pos.y === y);
}
function getSquareElement(x, y) {
return document.querySelector(`.square[data-x="${x}"][data-y="${y}"]`);
}
function getNeighbors(x, y) {
const positions = [];
if (x !== undefined && y !== undefined) {
if (x > 0) positions.push({ x: x - 1, y });
if (x < gridSizeX - 1) positions.push({ x: x + 1, y });
if (y > 0) positions.push({ x, y: y - 1 });
if (y < gridSizeY - 1) positions.push({ x, y: y + 1 });
}
return positions;
}
function onSquareClick() {
if (trailSquares.length > 0) {
// Cannot click blue square if there is a trail
return;
}
this.removeEventListener('click', onSquareClick);
this.classList.remove('clickable');
score++;
clickCount++;
timeLimit = Math.max(100, timeLimit - 20); // Decrease time limit, minimum 100ms
clearTimeout(timer);
if (clickCount % 20 === 0) {
// Every 20 clicks, clear the trail and move blue square to random position
clearTrail();
const activeSquare = getSquareElement(activeX, activeY);
if (activeSquare) {
activeSquare.classList.remove('active', 'clickable');
activeSquare.removeEventListener('click', onSquareClick);
}
activeX = undefined;
activeY = undefined;
placeActiveSquare();
} else {
placeActiveSquare();
}
startTimer();
}
function onTrailSquareClick() {
const x = parseInt(this.dataset.x);
const y = parseInt(this.dataset.y);
const firstTrailSquare = trailSquares[0];
if (x === firstTrailSquare.x && y === firstTrailSquare.y) {
// Correct trail square clicked
this.classList.remove('trail', 'clickable');
this.removeEventListener('click', onTrailSquareClick);
trailSquares.shift(); // Remove the first element
clickCount++;
score++;
clearTimeout(timer);
startTimer();
if (trailSquares.length === 0) {
// Trail cleared, make blue square clickable again
const activeSquare = getSquareElement(activeX, activeY);
if (activeSquare) {
activeSquare.classList.add('clickable');
activeSquare.addEventListener('click', onSquareClick);
}
} else {
// Update trail squares to make the new first one clickable
updateTrailSquares();
}
if (clickCount % 20 === 0) {
// Every 20 clicks, clear the trail and move blue square to random position
clearTrail();
const activeSquare = getSquareElement(activeX, activeY);
if (activeSquare) {
activeSquare.classList.remove('active', 'clickable');
activeSquare.removeEventListener('click', onSquareClick);
}
activeX = undefined;
activeY = undefined;
placeActiveSquare();
}
}
}
function startTimer() {
timer = setTimeout(() => {
if (trailSquares.length >= 5) {
endGame();
} else {
if (trailSquares.length === 0) {
// Can't click blue square until trail is cleared
const activeSquare = getSquareElement(activeX, activeY);
if (activeSquare) {
activeSquare.classList.remove('clickable');
activeSquare.removeEventListener('click', onSquareClick);
}
}
addTrailSquare(); // Add trail at current position before moving
placeActiveSquare(); // Move blue square to neighbor
startTimer();
}
}, timeLimit);
}
function addTrailSquare() {
// Add a trail square at the current position of the blue square
const trailSquare = { x: activeX, y: activeY };
trailSquares.push(trailSquare);
updateTrailSquares();
}
function updateTrailSquares() {
// Update all trail squares
trailSquares.forEach((pos, index) => {
const square = getSquareElement(pos.x, pos.y);
if (square) {
square.classList.add('trail');
square.classList.remove('clickable');
square.removeEventListener('click', onTrailSquareClick);
// Make only the first trail square clickable
if (index === 0) {
square.classList.add('clickable');
square.addEventListener('click', onTrailSquareClick);
}
}
});
}
function clearTrail() {
trailSquares.forEach(pos => {
const square = getSquareElement(pos.x, pos.y);
if (square) {
square.classList.remove('trail', 'clickable');
square.removeEventListener('click', onTrailSquareClick);
}
});
trailSquares = [];
}
function endGame() {
clearTimeout(timer);
alert(`Game Over! Your score: ${score}`);
if (score > highScore) {
highScore = score;
localStorage.setItem('highScore', highScore);
highScoreElement.textContent = highScore;
}
activeX = undefined;
activeY = undefined;
trailSquares = [];
gameContainer.style.display = 'none';
mainMenu.style.display = 'block';
}
function onResize() {
if (gameContainer.style.display === 'flex') {
setGameContainerHeight();
initializeGrid();
if (activeX >= gridSizeX || activeY >= gridSizeY) {
endGame();
} else {
placeActiveSquare();
updateTrailSquares();
}
}
}
function setGameContainerHeight() {
const vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
gameContainer.style.height = `calc(var(--vh, 1vh) * 100)`;
}
// Initial setup for game container height
setGameContainerHeight();
window.addEventListener('resize', setGameContainerHeight);
</script>
</body>
</html>