-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.js
161 lines (137 loc) · 4.87 KB
/
maze.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
class MazeSolver {
constructor(ctx, maze, startX, startY, exitX, exitY) {
this.ctx = ctx;
this.maze = maze;
this.currentX = startX;
this.currentY = startY;
this.exitX = exitX;
this.exitY = exitY;
this.cellSize = 30;
this.directions = {
up: { x: 0, y: -1 },
right: { x: 1, y: 0 },
down: { x: 0, y: 1 },
left: { x: -1, y: 0 }
};
this.direction = 'right';
}
turnRight() {
switch (this.direction) {
case 'up': this.direction = 'right'; break;
case 'right': this.direction = 'down'; break;
case 'down': this.direction = 'left'; break;
case 'left': this.direction = 'up'; break;
}
}
turnLeft() {
switch (this.direction) {
case 'up': this.direction = 'left'; break;
case 'right': this.direction = 'up'; break;
case 'down': this.direction = 'right'; break;
case 'left': this.direction = 'down'; break;
}
}
moveForward() {
this.currentX += this.directions[this.direction].x;
this.currentY += this.directions[this.direction].y;
}
isWallAhead() {
const nextX = this.currentX + this.directions[this.direction].x;
const nextY = this.currentY + this.directions[this.direction].y;
return this.maze[nextY][nextX] === 1;
}
isWallOnRight() {
let rightDirection;
switch (this.direction) {
case 'up': rightDirection = 'right'; break;
case 'right': rightDirection = 'down'; break;
case 'down': rightDirection = 'left'; break;
case 'left': rightDirection = 'up'; break;
}
const nextX = this.currentX + this.directions[rightDirection].x;
const nextY = this.currentY + this.directions[rightDirection].y;
return this.maze[nextY][nextX] === 1;
}
drawCurrentPosition() {
this.ctx.fillStyle = 'red';
this.ctx.fillRect(this.currentX * this.cellSize, this.currentY * this.cellSize, this.cellSize, this.cellSize);
}
async solve() {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
while (this.currentX !== this.exitX || this.currentY !== this.exitY) {
if (!this.isWallOnRight()) {
this.turnRight();
this.moveForward();
} else if (!this.isWallAhead()) {
this.moveForward();
} else {
this.turnLeft();
}
drawMaze(this.ctx, this.maze, this.cellSize);
this.drawCurrentPosition();
await delay(100);
}
alert('Maze solved! 🎉');
}
}
function generateRandomMaze(width, height) {
const maze = Array.from({ length: height }, () => Array(width).fill(1));
const stack = [];
const directions = [
{ x: 0, y: -2 },
{ x: 2, y: 0 },
{ x: 0, y: 2 },
{ x: -2, y: 0 }
];
const startX = Math.floor(Math.random() * (width / 2)) * 2 + 1;
const startY = Math.floor(Math.random() * (height / 2)) * 2 + 1;
maze[startY][startX] = 0;
stack.push({ x: startX, y: startY });
while (stack.length > 0) {
const { x, y } = stack.pop();
const validDirections = directions.filter(dir => {
const nx = x + dir.x;
const ny = y + dir.y;
return (
nx >= 1 && ny >= 1 &&
nx < width - 1 && ny < height - 1 &&
maze[ny][nx] === 1 &&
maze[ny + dir.y / 2][nx + dir.x / 2] === 1
);
});
if (validDirections.length > 0) {
stack.push({ x, y });
const { x: dx, y: dy } = validDirections[Math.floor(Math.random() * validDirections.length)];
maze[y + dy / 2][x + dx / 2] = 0;
maze[y + dy][x + dx] = 0;
stack.push({ x: x + dx, y: y + dy });
}
}
return maze;
}
function drawMaze(ctx, maze, cellSize) {
const width = maze[0].length;
const height = maze.length;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
ctx.fillStyle = maze[y][x] === 1 ? 'black' : 'white';
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
function initMaze() {
const canvas = document.getElementById('mazeCanvas');
const ctx = canvas.getContext('2d');
const cellSize = 30;
const mazeWidth = Math.floor(canvas.width / cellSize);
const mazeHeight = Math.floor(canvas.height / cellSize);
const maze = generateRandomMaze(mazeWidth, mazeHeight);
const startX = 1;
const startY = 1;
const exitX = mazeWidth - 2;
const exitY = mazeHeight - 2;
drawMaze(ctx, maze, cellSize);
const solver = new MazeSolver(ctx, maze, startX, startY, exitX, exitY);
solver.solve();
}
initMaze();