hns-maze 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hns_maze-0.0.1/.gitignore +4 -0
- hns_maze-0.0.1/LICENSE +21 -0
- hns_maze-0.0.1/PKG-INFO +17 -0
- hns_maze-0.0.1/README.md +2 -0
- hns_maze-0.0.1/hns_maze/__init__.py +4 -0
- hns_maze-0.0.1/hns_maze/algorithm/__init__.py +2 -0
- hns_maze-0.0.1/hns_maze/algorithm/a_star.py +88 -0
- hns_maze-0.0.1/hns_maze/algorithm/bfs.py +87 -0
- hns_maze-0.0.1/hns_maze/algorithm/bidirectional_bfs.py +112 -0
- hns_maze-0.0.1/hns_maze/algorithm/dfs.py +68 -0
- hns_maze-0.0.1/hns_maze/algorithm/gbfs.py +89 -0
- hns_maze-0.0.1/hns_maze/algorithm/heuristics/__init__.py +2 -0
- hns_maze-0.0.1/hns_maze/algorithm/heuristics/euclidean.py +8 -0
- hns_maze-0.0.1/hns_maze/algorithm/heuristics/manhattan.py +8 -0
- hns_maze-0.0.1/hns_maze/game/__init__.py +5 -0
- hns_maze-0.0.1/hns_maze/game/entity_controller/__init__.py +2 -0
- hns_maze-0.0.1/hns_maze/game/entity_controller/agent_control.py +230 -0
- hns_maze-0.0.1/hns_maze/game/entity_controller/player_control.py +111 -0
- hns_maze-0.0.1/hns_maze/game/entity_controller/shared_control.py +62 -0
- hns_maze-0.0.1/hns_maze/game/entity_spawning/__init__.py +1 -0
- hns_maze-0.0.1/hns_maze/game/entity_spawning/spawn_handler.py +57 -0
- hns_maze-0.0.1/hns_maze/game/entity_state/__init__.py +2 -0
- hns_maze-0.0.1/hns_maze/game/entity_state/agent.py +280 -0
- hns_maze-0.0.1/hns_maze/game/entity_state/player.py +201 -0
- hns_maze-0.0.1/hns_maze/game/entity_state/shared.py +59 -0
- hns_maze-0.0.1/hns_maze/game/interface/__init__.py +0 -0
- hns_maze-0.0.1/hns_maze/game/interface/backbone.py +323 -0
- hns_maze-0.0.1/hns_maze/game/interface/main_menu.py +74 -0
- hns_maze-0.0.1/hns_maze/game/interface/maze_gameplay.py +611 -0
- hns_maze-0.0.1/hns_maze/game/interface/play_menu.py +233 -0
- hns_maze-0.0.1/hns_maze/game/interface/settings_menu.py +340 -0
- hns_maze-0.0.1/hns_maze/game/interface/visual.py +344 -0
- hns_maze-0.0.1/hns_maze/game/managers/__init__.py +11 -0
- hns_maze-0.0.1/hns_maze/game/managers/game_manager.py +134 -0
- hns_maze-0.0.1/hns_maze/game/managers/loop_manager.py +242 -0
- hns_maze-0.0.1/hns_maze/game/managers/ui_manager.py +39 -0
- hns_maze-0.0.1/hns_maze/generation/__init__.py +1 -0
- hns_maze-0.0.1/hns_maze/generation/flood_fill.py +181 -0
- hns_maze-0.0.1/hns_maze/generation/generate_all.py +16 -0
- hns_maze-0.0.1/hns_maze/generation/generate_grid.py +116 -0
- hns_maze-0.0.1/hns_maze/play.py +7 -0
- hns_maze-0.0.1/pyproject.toml +28 -0
hns_maze-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zico Diego
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
hns_maze-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hns-maze
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A maze-based hide-and-seek game with a model-based reflex agent as a seeker and a player as the hider.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ZicoDiegoRR/AI-Concept-Course-Final-Project
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/ZicoDiegoRR/AI-Concept-Course-Final-Project/issues
|
|
7
|
+
Author-email: Zico Diego <itzb4pakku1@gmail.com>
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Requires-Dist: pygame>=2.6
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# HnS: Don't Get Caught By It!
|
|
17
|
+
A maze-based hide-and-seek game with a model-based reflex agent as a seeker and a player as the hider.
|
hns_maze-0.0.1/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
from typing import Callable
|
|
3
|
+
|
|
4
|
+
def solve(
|
|
5
|
+
grid: list[list[dict[str, int]]],
|
|
6
|
+
start: tuple[int, int],
|
|
7
|
+
goal: tuple[int, int],
|
|
8
|
+
h_func: Callable,
|
|
9
|
+
) -> list[tuple[int, int]]:
|
|
10
|
+
if not grid or not grid[0]:
|
|
11
|
+
return []
|
|
12
|
+
|
|
13
|
+
rows = len(grid)
|
|
14
|
+
cols = len(grid[0])
|
|
15
|
+
|
|
16
|
+
if not (0 <= start[0] < rows and 0 <= start[1] < cols):
|
|
17
|
+
return []
|
|
18
|
+
if not (0 <= goal[0] < rows and 0 <= goal[1] < cols):
|
|
19
|
+
return []
|
|
20
|
+
if start == goal:
|
|
21
|
+
return [start]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
q = [[0, 0, start, [start]]]
|
|
25
|
+
visited = set()
|
|
26
|
+
moves = (
|
|
27
|
+
((-1, 0), "up", "down"),
|
|
28
|
+
((1, 0), "down", "up"),
|
|
29
|
+
((0, -1), "left", "right"),
|
|
30
|
+
((0, 1), "right", "left")
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
while q:
|
|
34
|
+
_, curr_g, curr_pos, curr_path = heapq.heappop(q)
|
|
35
|
+
|
|
36
|
+
if curr_pos == goal:
|
|
37
|
+
return curr_path
|
|
38
|
+
|
|
39
|
+
if curr_pos in visited:
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
visited.add(curr_pos)
|
|
43
|
+
curr_x, curr_y = curr_pos
|
|
44
|
+
for (dx, dy), wall, opposite_wall in moves:
|
|
45
|
+
nxt_x, nxt_y = curr_x + dx, curr_y + dy
|
|
46
|
+
|
|
47
|
+
if not (0 <= nxt_x < rows and 0 <= nxt_y < cols):
|
|
48
|
+
continue
|
|
49
|
+
if (nxt_x, nxt_y) in visited:
|
|
50
|
+
continue
|
|
51
|
+
if grid[curr_x][curr_y].get(wall, 1) == 1:
|
|
52
|
+
continue
|
|
53
|
+
if grid[nxt_x][nxt_y].get(opposite_wall, 1) == 1:
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
new_path = curr_path + [(nxt_x, nxt_y)]
|
|
57
|
+
new_g = curr_g + 1
|
|
58
|
+
new_f = new_g + h_func(curr_pos, goal)
|
|
59
|
+
|
|
60
|
+
heapq.heappush(q, [new_f, new_g, (nxt_x, nxt_y), new_path])
|
|
61
|
+
|
|
62
|
+
return curr_path
|
|
63
|
+
|
|
64
|
+
if __name__ == "__main__":
|
|
65
|
+
import heuristics.euclidean as euclidean
|
|
66
|
+
|
|
67
|
+
def print_maze(grid: list[list[dict[str, int]]]) -> None:
|
|
68
|
+
for row in grid:
|
|
69
|
+
for col in row:
|
|
70
|
+
print("|" if col["left"] else " ", end="")
|
|
71
|
+
if col["up"] and col["down"]:
|
|
72
|
+
print("ニ", end="")
|
|
73
|
+
elif col["up"]:
|
|
74
|
+
print("‾", end="")
|
|
75
|
+
elif col["down"]:
|
|
76
|
+
print("_", end="")
|
|
77
|
+
else:
|
|
78
|
+
print(" ", end="")
|
|
79
|
+
print("|" if col["right"] else " ", end="")
|
|
80
|
+
print()
|
|
81
|
+
|
|
82
|
+
grid = [[{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}]]
|
|
83
|
+
print_maze(grid)
|
|
84
|
+
print()
|
|
85
|
+
|
|
86
|
+
start = (4, 4)
|
|
87
|
+
goal = (3, 6)
|
|
88
|
+
print(solve(grid, start, goal, euclidean.compute))
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from collections import deque
|
|
2
|
+
import random
|
|
3
|
+
|
|
4
|
+
def solve(
|
|
5
|
+
grid: list[list[dict[str, int]]],
|
|
6
|
+
visited: list[list[tuple[int, int]]],
|
|
7
|
+
start: tuple[int, int],
|
|
8
|
+
hiding_cell_reduction: float,
|
|
9
|
+
) -> list[tuple[int, int]]:
|
|
10
|
+
if not grid or not grid[0]:
|
|
11
|
+
return []
|
|
12
|
+
|
|
13
|
+
rows = len(grid)
|
|
14
|
+
cols = len(grid[0])
|
|
15
|
+
|
|
16
|
+
if not (0 <= start[0] < rows and 0 <= start[1] < cols):
|
|
17
|
+
return []
|
|
18
|
+
|
|
19
|
+
q = deque([[start, [start]]])
|
|
20
|
+
seen = set()
|
|
21
|
+
moves = (
|
|
22
|
+
((-1, 0), "up", "down"),
|
|
23
|
+
((1, 0), "down", "up"),
|
|
24
|
+
((0, -1), "left", "right"),
|
|
25
|
+
((0, 1), "right", "left")
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
while q:
|
|
29
|
+
curr_pos, curr_path = q.popleft()
|
|
30
|
+
curr_x, curr_y = curr_pos
|
|
31
|
+
|
|
32
|
+
if (
|
|
33
|
+
grid[curr_x][curr_y]["hiding"] == 1
|
|
34
|
+
and random.random() <= hiding_cell_reduction
|
|
35
|
+
and curr_pos != start
|
|
36
|
+
):
|
|
37
|
+
if curr_pos not in visited: visited.append(curr_pos)
|
|
38
|
+
if curr_pos not in seen: seen.add(curr_pos)
|
|
39
|
+
continue
|
|
40
|
+
|
|
41
|
+
if curr_pos not in visited:
|
|
42
|
+
return curr_path
|
|
43
|
+
|
|
44
|
+
if curr_pos in seen:
|
|
45
|
+
continue
|
|
46
|
+
|
|
47
|
+
seen.add(curr_pos)
|
|
48
|
+
for (dx, dy), wall, opposite_wall in moves:
|
|
49
|
+
nxt_x, nxt_y = curr_x + dx, curr_y + dy
|
|
50
|
+
|
|
51
|
+
if not (0 <= nxt_x < rows and 0 <= nxt_y < cols):
|
|
52
|
+
continue
|
|
53
|
+
if (nxt_x, nxt_y) in seen:
|
|
54
|
+
continue
|
|
55
|
+
if grid[curr_x][curr_y].get(wall, 1) == 1:
|
|
56
|
+
continue
|
|
57
|
+
if grid[nxt_x][nxt_y].get(opposite_wall, 1) == 1:
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
new_path = curr_path + [(nxt_x, nxt_y)]
|
|
61
|
+
q.append([(nxt_x, nxt_y), new_path])
|
|
62
|
+
|
|
63
|
+
return []
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
def print_maze(grid: list[list[dict[str, int]]]) -> None:
|
|
67
|
+
for row in grid:
|
|
68
|
+
for col in row:
|
|
69
|
+
print("|" if col["left"] else " ", end="")
|
|
70
|
+
if col["up"] and col["down"]:
|
|
71
|
+
print("ニ", end="")
|
|
72
|
+
elif col["up"]:
|
|
73
|
+
print("‾", end="")
|
|
74
|
+
elif col["down"]:
|
|
75
|
+
print("_", end="")
|
|
76
|
+
else:
|
|
77
|
+
print(" ", end="")
|
|
78
|
+
print("|" if col["right"] else " ", end="")
|
|
79
|
+
print()
|
|
80
|
+
|
|
81
|
+
grid = [[{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}]]
|
|
82
|
+
print_maze(grid)
|
|
83
|
+
print()
|
|
84
|
+
|
|
85
|
+
start = (4, 4)
|
|
86
|
+
visited = [(4, 4), (3, 4), (5, 4), (4, 5), (3, 3), (3, 5), (5, 3), (5, 5)]
|
|
87
|
+
print(solve(grid, visited, start))
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
from collections import deque
|
|
2
|
+
|
|
3
|
+
def solve(
|
|
4
|
+
grid: list[list[dict[str, int]]],
|
|
5
|
+
start: tuple[int, int],
|
|
6
|
+
goal: tuple[int, int],
|
|
7
|
+
max_steps: int = 500,
|
|
8
|
+
) -> list[tuple[int, int]]: # Code modified from GeeksforGeeks and corrected by Copilot AI
|
|
9
|
+
if not grid or not grid[0]:
|
|
10
|
+
return []
|
|
11
|
+
|
|
12
|
+
rows = len(grid)
|
|
13
|
+
cols = len(grid[0])
|
|
14
|
+
|
|
15
|
+
if not (0 <= start[0] < rows and 0 <= start[1] < cols):
|
|
16
|
+
return []
|
|
17
|
+
if not (0 <= goal[0] < rows and 0 <= goal[1] < cols):
|
|
18
|
+
return []
|
|
19
|
+
if start == goal:
|
|
20
|
+
return [start]
|
|
21
|
+
|
|
22
|
+
if not max_steps:
|
|
23
|
+
max_steps = float("inf")
|
|
24
|
+
|
|
25
|
+
queue_start = deque([[start, 0]])
|
|
26
|
+
queue_goal = deque([[goal, 0]])
|
|
27
|
+
visited_start = set()
|
|
28
|
+
visited_goal = set()
|
|
29
|
+
parent_start = {start: None}
|
|
30
|
+
parent_goal = {goal: None}
|
|
31
|
+
|
|
32
|
+
moves = (
|
|
33
|
+
((-1, 0), "up", "down"),
|
|
34
|
+
((1, 0), "down", "up"),
|
|
35
|
+
((0, -1), "left", "right"),
|
|
36
|
+
((0, 1), "right", "left")
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
while queue_start and queue_goal:
|
|
40
|
+
for q, visited, parent in zip(
|
|
41
|
+
[queue_start, queue_goal],
|
|
42
|
+
[visited_start, visited_goal],
|
|
43
|
+
[parent_start, parent_goal],
|
|
44
|
+
):
|
|
45
|
+
curr_pos, curr_step = q.popleft()
|
|
46
|
+
|
|
47
|
+
if curr_pos in visited or curr_step >= max_steps:
|
|
48
|
+
continue
|
|
49
|
+
|
|
50
|
+
visited.add(curr_pos)
|
|
51
|
+
curr_x, curr_y = curr_pos
|
|
52
|
+
for (dx, dy), wall, opposite_wall in moves:
|
|
53
|
+
nxt_x, nxt_y = curr_x + dx, curr_y + dy
|
|
54
|
+
|
|
55
|
+
if not (0 <= nxt_x < rows and 0 <= nxt_y < cols):
|
|
56
|
+
continue
|
|
57
|
+
if (nxt_x, nxt_y) in visited:
|
|
58
|
+
continue
|
|
59
|
+
if grid[curr_x][curr_y].get(wall, 1) == 1:
|
|
60
|
+
continue
|
|
61
|
+
if grid[nxt_x][nxt_y].get(opposite_wall, 1) == 1:
|
|
62
|
+
continue
|
|
63
|
+
|
|
64
|
+
q.append([(nxt_x, nxt_y), curr_step + 1])
|
|
65
|
+
parent[(nxt_x, nxt_y)] = curr_pos
|
|
66
|
+
|
|
67
|
+
intersect = None
|
|
68
|
+
for node in visited_start:
|
|
69
|
+
if node in visited_goal:
|
|
70
|
+
intersect = node
|
|
71
|
+
break
|
|
72
|
+
|
|
73
|
+
if intersect is not None:
|
|
74
|
+
path = []
|
|
75
|
+
step = intersect
|
|
76
|
+
while step is not None:
|
|
77
|
+
path.append(step)
|
|
78
|
+
step = parent_start.get(step)
|
|
79
|
+
path.reverse()
|
|
80
|
+
|
|
81
|
+
step = parent_goal[intersect]
|
|
82
|
+
while step is not None:
|
|
83
|
+
path.append(step)
|
|
84
|
+
step = parent_goal.get(step)
|
|
85
|
+
|
|
86
|
+
return path
|
|
87
|
+
|
|
88
|
+
return []
|
|
89
|
+
|
|
90
|
+
if __name__ == "__main__":
|
|
91
|
+
def print_maze(grid: list[list[dict[str, int]]]) -> None:
|
|
92
|
+
for row in grid:
|
|
93
|
+
for col in row:
|
|
94
|
+
print("|" if col["left"] else " ", end="")
|
|
95
|
+
if col["up"] and col["down"]:
|
|
96
|
+
print("ニ", end="")
|
|
97
|
+
elif col["up"]:
|
|
98
|
+
print("‾", end="")
|
|
99
|
+
elif col["down"]:
|
|
100
|
+
print("_", end="")
|
|
101
|
+
else:
|
|
102
|
+
print(" ", end="")
|
|
103
|
+
print("|" if col["right"] else " ", end="")
|
|
104
|
+
print()
|
|
105
|
+
|
|
106
|
+
grid = [[{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}]]
|
|
107
|
+
print_maze(grid)
|
|
108
|
+
print()
|
|
109
|
+
|
|
110
|
+
start = (4, 4)
|
|
111
|
+
goal = (3, 6)
|
|
112
|
+
print(solve(grid, start, goal))
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
def solve(
|
|
2
|
+
grid: list[list[dict[str, int]]],
|
|
3
|
+
start: tuple[int, int],
|
|
4
|
+
goal: tuple[int, int],
|
|
5
|
+
max_steps: int = 500,
|
|
6
|
+
) -> list[tuple[int, int]]: # Code by Copilot AI
|
|
7
|
+
"""Solve the maze using recursive depth-first search.
|
|
8
|
+
|
|
9
|
+
Each cell in `grid` contains wall flags for `up`, `down`, `left`, and `right`.
|
|
10
|
+
A value of `1` indicates a wall, and `0` indicates an open passage.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
if not grid or not grid[0]:
|
|
14
|
+
return []
|
|
15
|
+
|
|
16
|
+
rows = len(grid)
|
|
17
|
+
cols = len(grid[0])
|
|
18
|
+
start_x, start_y = start
|
|
19
|
+
goal_x, goal_y = goal
|
|
20
|
+
|
|
21
|
+
if not (0 <= start_x < rows and 0 <= start_y < cols):
|
|
22
|
+
return []
|
|
23
|
+
if not (0 <= goal_x < rows and 0 <= goal_y < cols):
|
|
24
|
+
return []
|
|
25
|
+
if start == goal:
|
|
26
|
+
return [start]
|
|
27
|
+
|
|
28
|
+
if not max_steps:
|
|
29
|
+
max_steps = float("inf")
|
|
30
|
+
|
|
31
|
+
visited: set[tuple[int, int]] = set()
|
|
32
|
+
directions = [
|
|
33
|
+
(-1, 0, "up", "down"),
|
|
34
|
+
(1, 0, "down", "up"),
|
|
35
|
+
(0, -1, "left", "right"),
|
|
36
|
+
(0, 1, "right", "left"),
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
def dfs(x: int, y: int, step: int) -> list[tuple[int, int]] | None:
|
|
40
|
+
if step == max_steps:
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
visited.add((x, y))
|
|
44
|
+
if (x, y) == goal:
|
|
45
|
+
return [(x, y)]
|
|
46
|
+
|
|
47
|
+
for dx, dy, wall, opposite_wall in directions:
|
|
48
|
+
next_x = x + dx
|
|
49
|
+
next_y = y + dy
|
|
50
|
+
|
|
51
|
+
if not (0 <= next_x < rows and 0 <= next_y < cols):
|
|
52
|
+
continue
|
|
53
|
+
if (next_x, next_y) in visited:
|
|
54
|
+
continue
|
|
55
|
+
if grid[x][y].get(wall, 1) == 1:
|
|
56
|
+
continue
|
|
57
|
+
if grid[next_x][next_y].get(opposite_wall, 1) == 1:
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
result = dfs(next_x, next_y, step + 1)
|
|
61
|
+
if result is not None:
|
|
62
|
+
return [(x, y)] + result
|
|
63
|
+
|
|
64
|
+
return None
|
|
65
|
+
|
|
66
|
+
path = dfs(start_x, start_y, 0)
|
|
67
|
+
return path if path is not None else []
|
|
68
|
+
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
from typing import Callable
|
|
3
|
+
|
|
4
|
+
def solve(
|
|
5
|
+
grid: list[list[dict[str, int]]],
|
|
6
|
+
start: tuple[int, int],
|
|
7
|
+
goal: tuple[int, int],
|
|
8
|
+
h_func: Callable,
|
|
9
|
+
max_steps: int = 500,
|
|
10
|
+
) -> list[tuple[int, int]]:
|
|
11
|
+
if not grid or not grid[0]:
|
|
12
|
+
return []
|
|
13
|
+
|
|
14
|
+
rows = len(grid)
|
|
15
|
+
cols = len(grid[0])
|
|
16
|
+
|
|
17
|
+
if not (0 <= start[0] < rows and 0 <= start[1] < cols):
|
|
18
|
+
return []
|
|
19
|
+
if not (0 <= goal[0] < rows and 0 <= goal[1] < cols):
|
|
20
|
+
return []
|
|
21
|
+
if start == goal:
|
|
22
|
+
return [start]
|
|
23
|
+
|
|
24
|
+
if not max_steps:
|
|
25
|
+
max_steps = float("inf")
|
|
26
|
+
|
|
27
|
+
q = [[0, 0, start, [start]]]
|
|
28
|
+
visited = set()
|
|
29
|
+
moves = (
|
|
30
|
+
((-1, 0), "up", "down"),
|
|
31
|
+
((1, 0), "down", "up"),
|
|
32
|
+
((0, -1), "left", "right"),
|
|
33
|
+
((0, 1), "right", "left")
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
while q:
|
|
37
|
+
_, curr_step, curr_pos, curr_path = heapq.heappop(q)
|
|
38
|
+
|
|
39
|
+
if curr_pos in visited or curr_step >= max_steps:
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
if curr_pos == goal:
|
|
43
|
+
return curr_path
|
|
44
|
+
|
|
45
|
+
visited.add(curr_pos)
|
|
46
|
+
curr_x, curr_y = curr_pos
|
|
47
|
+
for (dx, dy), wall, opposite_wall in moves:
|
|
48
|
+
nxt_x, nxt_y = curr_x + dx, curr_y + dy
|
|
49
|
+
|
|
50
|
+
if not (0 <= nxt_x < rows and 0 <= nxt_y < cols):
|
|
51
|
+
continue
|
|
52
|
+
if (nxt_x, nxt_y) in visited:
|
|
53
|
+
continue
|
|
54
|
+
if grid[curr_x][curr_y].get(wall, 1) == 1:
|
|
55
|
+
continue
|
|
56
|
+
if grid[nxt_x][nxt_y].get(opposite_wall, 1) == 1:
|
|
57
|
+
continue
|
|
58
|
+
|
|
59
|
+
new_h = h_func(start, goal)
|
|
60
|
+
new_path = curr_path + [(nxt_x, nxt_y)]
|
|
61
|
+
heapq.heappush(q, [new_h, curr_step+1, (nxt_x, nxt_y), new_path])
|
|
62
|
+
|
|
63
|
+
return curr_path
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
import heuristics.euclidean as euclidean
|
|
67
|
+
|
|
68
|
+
def print_maze(grid: list[list[dict[str, int]]]) -> None:
|
|
69
|
+
for row in grid:
|
|
70
|
+
for col in row:
|
|
71
|
+
print("|" if col["left"] else " ", end="")
|
|
72
|
+
if col["up"] and col["down"]:
|
|
73
|
+
print("ニ", end="")
|
|
74
|
+
elif col["up"]:
|
|
75
|
+
print("‾", end="")
|
|
76
|
+
elif col["down"]:
|
|
77
|
+
print("_", end="")
|
|
78
|
+
else:
|
|
79
|
+
print(" ", end="")
|
|
80
|
+
print("|" if col["right"] else " ", end="")
|
|
81
|
+
print()
|
|
82
|
+
|
|
83
|
+
grid = [[{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 0, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 0}], [{'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 0, 'left': 1, 'right': 1}], [{'up': 1, 'down': 0, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 0, 'down': 1, 'left': 0, 'right': 0}, {'up': 1, 'down': 0, 'left': 0, 'right': 1}, {'up': 0, 'down': 0, 'left': 1, 'right': 1}], [{'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 0}, {'up': 1, 'down': 1, 'left': 0, 'right': 1}, {'up': 1, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}, {'up': 0, 'down': 1, 'left': 1, 'right': 1}]]
|
|
84
|
+
print_maze(grid)
|
|
85
|
+
print()
|
|
86
|
+
|
|
87
|
+
start = (4, 4)
|
|
88
|
+
goal = (3, 6)
|
|
89
|
+
print(solve(grid, start, goal, euclidean.compute))
|