-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSolver.html
More file actions
195 lines (173 loc) · 5.21 KB
/
Copy pathMazeSolver.html
File metadata and controls
195 lines (173 loc) · 5.21 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
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8" />
<title>迷宮產生器</title>
<style>
body {
text-align: center;
font-family: Arial;
}
canvas {
border: 1px solid black;
background-color: white;
}
button {
margin: 10px;
padding: 10px 20px;
}
</style>
</head>
<body>
<h1>迷宮產生器(DFS+BFS)</h1>
<canvas id="mazeCanvas" width="420" height="420"></canvas><br />
<button id="generateButton" onclick="generateMaze()">產生迷宮</button>
<button id="solveMaze" onclick="solveMaze()">解迷宮</button>
<script>
const canvas = document.getElementById("mazeCanvas");
const ctx = canvas.getContext("2d");
const cellSize = 20;
const cols = 21;
const rows = 21;
let maze = [];
const start = { x: 1, y: 1 };
const end = { x: cols - 2, y: rows - 2 };
function initMaze() {
maze = Array.from({ length: rows }, () => Array(cols).fill("#"));
}
function drawMaze() {
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
if (maze[y][x] === "#") ctx.fillStyle = "black";
else if (maze[y][x] === ".") ctx.fillStyle = "orange";
else if (maze[y][x] === "S") ctx.fillStyle = "green";
else if (maze[y][x] === "E") ctx.fillStyle = "red";
else ctx.fillStyle = "white";
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
async function generateMazeDFS(x, y) {
maze[y][x] = " ";
drawMaze();
await sleep(100);
const directions = [
{ dx: 0, dy: -2 },
{ dx: 2, dy: 0 },
{ dx: 0, dy: 2 },
{ dx: -2, dy: 0 },
];
shuffle(directions);
for (const { dx, dy } of directions) {
const nx = x + dx;
const ny = y + dy;
if (
nx > 0 &&
nx < cols - 1 &&
ny > 0 &&
ny < rows - 1 &&
maze[ny][nx] === "#"
) {
maze[y + dy / 2][x + dx / 2] = " ";
await generateMazeDFS(nx, ny);
}
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function toggleButtons(disabled) {
const buttons = document.querySelectorAll("button");
buttons.forEach((button) => {
button.disabled = disabled;
});
}
async function generateMaze() {
toggleButtons(true); // 禁用所有按鈕
initMaze();
await generateMazeDFS(1, 1);
toggleButtons(false); // 啟用所有按鈕
}
function solveMaze() {
const queue = [];
const visited = Array.from({ length: rows }, () =>
Array(cols).fill(false)
);
const parent = Array.from({ length: rows }, () =>
Array(cols).fill(null)
);
const directions = [
{ dx: 0, dy: -1 },
{ dx: 1, dy: 0 },
{ dx: 0, dy: 1 },
{ dx: -1, dy: 0 },
];
queue.push(start);
visited[start.y][start.x] = true;
while (queue.length > 0) {
const { x, y } = queue.shift();
if (x === end.x && y === end.y) break;
for (const { dx, dy } of directions) {
const nx = x + dx;
const ny = y + dy;
if (
nx >= 0 &&
ny >= 0 &&
nx < cols &&
ny < rows &&
maze[ny][nx] === " " &&
!visited[ny][nx]
) {
visited[ny][nx] = true;
parent[ny][nx] = { x, y };
queue.push({ x: nx, y: ny });
}
}
}
let path = [];
// 從終點回推到起點
let cur = end;
while (cur.x !== start.x || cur.y !== start.y) {
maze[cur.y][cur.x] = ".";
path.push(cur);
cur = parent[cur.y][cur.x];
}
maze[start.y][start.x] = "S";
maze[end.y][end.x] = "E";
path.shift(); // 移除起點
path.reverse(); // 反轉路徑以便從起點到終點繪製
drawparentMaze(path);
// drawMaze();
}
function drawparentMaze(path) {
ctx.fillStyle = "green";
ctx.fillRect(
start.x * cellSize,
start.y * cellSize,
cellSize,
cellSize
);
ctx.fillStyle = "red";
ctx.fillRect(end.x * cellSize, end.y * cellSize, cellSize, cellSize);
path.forEach((element, index) => {
setTimeout(() => {
ctx.fillStyle = "orange";
ctx.fillRect(
element.x * cellSize,
element.y * cellSize,
cellSize,
cellSize
);
}, index * 50); // 每個格子的繪製時間間隔為 200ms
});
}
generateMaze(); // 預設先產生一個迷宮
</script>
</body>
</html>