-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathscript.js
More file actions
197 lines (163 loc) · 5.48 KB
/
script.js
File metadata and controls
197 lines (163 loc) · 5.48 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
function generateMazeGrid(width, height) {
// Maze generator class inside function
class MazeGrid {
constructor(w, h) {
this.width = w;
this.height = h;
this.grid = [];
for (let y = 0; y < h; y++) {
this.grid[y] = [];
for (let x = 0; x < w; x++) {
this.grid[y][x] = { visited: false, walls: [true, true, true, true] };
}
}
}
getUnvisitedNeighbors(x, y) {
const neighbors = [];
if (y > 0 && !this.grid[y - 1][x].visited) neighbors.push({ x, y: y - 1, dir: 0 });
if (x < this.width - 1 && !this.grid[y][x + 1].visited) neighbors.push({ x: x + 1, y, dir: 1 });
if (y < this.height - 1 && !this.grid[y + 1][x].visited) neighbors.push({ x, y: y + 1, dir: 2 });
if (x > 0 && !this.grid[y][x - 1].visited) neighbors.push({ x: x - 1, y, dir: 3 });
return neighbors;
}
removeWalls(x, y, nx, ny, dir) {
this.grid[y][x].walls[dir] = false;
this.grid[ny][nx].walls[(dir + 2) % 4] = false;
}
generateMaze(x = 0, y = 0) {
this.grid[y][x].visited = true;
let neighbors = this.getUnvisitedNeighbors(x, y);
while (neighbors.length > 0) {
const rand = Math.floor(Math.random() * neighbors.length);
const { x: nx, y: ny, dir } = neighbors[rand];
if (!this.grid[ny][nx].visited) {
this.removeWalls(x, y, nx, ny, dir);
this.generateMaze(nx, ny);
}
neighbors.splice(rand, 1);
}
}
toOutputGrid() {
const outWidth = this.width * 2 + 1;
const outHeight = this.height * 2 + 1;
const output = Array.from({ length: outHeight }, () => Array(outWidth).fill(1));
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const outY = y * 2 + 1;
const outX = x * 2 + 1;
output[outY][outX] = 0;
const cell = this.grid[y][x];
if (!cell.walls[0]) output[outY - 1][outX] = 0;
if (!cell.walls[1]) output[outY][outX + 1] = 0;
if (!cell.walls[2]) output[outY + 1][outX] = 0;
if (!cell.walls[3]) output[outY][outX - 1] = 0;
}
}
// Mark goal at bottom-right open cell
output[outHeight - 2][outWidth - 2] = 'G';
return output;
}
}
const maze = new MazeGrid(width, height);
maze.generateMaze();
return maze.toOutputGrid();
}
// Example:
const mazeArray = generateMazeGrid(7, 10); // This is your 2D array
console.log(mazeArray);
const mazeData = mazeArray;
const grid = document.getElementById('grid');
const popup = document.getElementById('popup');
const popupClose = document.getElementById('popup-close');
const resetBtn = document.getElementById('resetBtn');
const timerDisplay = document.getElementById('timer');
const ROWS = mazeData.length;
const COLS = mazeData[0].length;
let dragonPos = { row: 1, col: 1 };
let timer = 60;
let timerInterval;
function drawMaze() {
grid.innerHTML = '';
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
const tile = document.createElement('div');
tile.classList.add('tile');
if (mazeData[r][c] === 1) tile.classList.add('wall');
if (mazeData[r][c] === 'G') tile.classList.add('goal');
if (r === dragonPos.row && c === dragonPos.col) {
tile.classList.add('dragon');
tile.innerHTML = '🐉';
}
grid.appendChild(tile);
}
}
}
function canMove(row, col) {
return (
row >= 0 &&
col >= 0 &&
row < ROWS &&
col < COLS &&
mazeData[row][col] !== 1
);
}
function moveDragon(dr, dc) {
const newRow = dragonPos.row + dr;
const newCol = dragonPos.col + dc;
if (!canMove(newRow, newCol)) return;
dragonPos = { row: newRow, col: newCol };
drawMaze();
if (mazeData[newRow][newCol] === 'G') {
clearInterval(timerInterval);
let score=timer*10;
showPopup(`🎉 You reached the mountaintop!<br>Score: ${score}`);
window.removeEventListener('keydown', handleKey);
}
}
function handleKey(e) {
if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key)) {
e.preventDefault();
switch (e.key) {
case 'ArrowUp': moveDragon(-1, 0); break;
case 'ArrowDown': moveDragon(1, 0); break;
case 'ArrowLeft': moveDragon(0, -1); break;
case 'ArrowRight': moveDragon(0, 1); break;
}
}
}
function showPopup(message) {
console.log("Popup triggered with message:", message);
document.getElementById('popup-message').innerHTML = message;
popup.classList.remove('hidden');
}
function updateTimerDisplay() {
timerDisplay.textContent = `⏱ Time Left: ${timer}s`;
}
function startTimer() {
clearInterval(timerInterval);
timer = 30;
updateTimerDisplay();
timerInterval = setInterval(() => {
timer--;
updateTimerDisplay();
if (timer <= 0) {
clearInterval(timerInterval);
showPopup("⏰ Time's up! The dragon got lost in the clouds!");
window.removeEventListener('keydown', handleKey);
}
}, 1000);
}
resetBtn.addEventListener('click', () => {
dragonPos = { row: 1, col: 1 };
drawMaze();
popup.classList.add('hidden');
window.addEventListener('keydown', handleKey);
startTimer();
});
popupClose.addEventListener('click', () => {
popup.classList.add('hidden');
});
window.addEventListener('keydown', handleKey);
// Start game
drawMaze();
startTimer();