-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
185 lines (156 loc) · 6.25 KB
/
maze.py
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
"""
Implemention of the Maze ADT.
"""
import turtle
import pygame
from dataclasses import dataclass
from lliststack import Stack
class Maze:
"""
Class for representing Maze objects.
Attributes:
maze: The list of lists representing the maze.
_start: The starting position of the maze; marked as -1 on the maze.
_exit: The exit position of the maze; marked as 2 on the maze.
"""
FREE = 0
MAZE_WALL = 1
EXIT = 2
PATH_TOKEN = 3
TRIED_TOKEN = 4
def __init__(self, maze_file: str, visualization=None):
"""
Creates a maze object using _build_maze func.
"""
self._start = None
self._exit = None
self.maze = self._build_maze(maze_file)
self.visualization = visualization
def _build_maze(self, filename: str):
"""
Builds a maze based on a text format in the given file.
The initial cell and the final cell must be present.
Otherwise an error with the corresponding message will be raised.
"""
maze = []
with open(filename, 'r') as f:
lines = f.readlines()
for i in range(len(lines)):
line = list(map(int, lines[i].split()))
if -1 in line:
self._start = _CellPosition(i, line.index(-1))
if 2 in line:
self._exit = _CellPosition(i, line.index(2))
maze.append(line)
assert self._start is not None, "No starting position found."
assert self._exit is not None, "No final position found."
return maze
def find_path(self):
"""
Attempts to solve the maze by finding a path from the starting cell
to the exit. Returns True if a path is found and False otherwise.
Change the self.maze where:
0 - not tried cells;
1 - walls;
2 - exit;
3 - found path;
4 - tried cells.
"""
stack = Stack()
current_cell = _CellPosition(
self._start.row, self._start.col)
self._mark_path(current_cell.row, current_cell.col)
while not self._exit_found(current_cell.row, current_cell.col):
# step up
if self._valid_move(current_cell.row - 1, current_cell.col):
stack.push(current_cell)
self._mark_path(current_cell.row - 1, current_cell.col)
current_cell = _CellPosition(
current_cell.row - 1, current_cell.col)
# step right
elif self._valid_move(current_cell.row, current_cell.col + 1):
stack.push(current_cell)
self._mark_path(current_cell.row, current_cell.col + 1)
current_cell = _CellPosition(
current_cell.row, current_cell.col + 1)
# step down
elif self._valid_move(current_cell.row + 1, current_cell.col):
stack.push(current_cell)
self._mark_path(current_cell.row + 1, current_cell.col)
current_cell = _CellPosition(
current_cell.row + 1, current_cell.col)
# step left
elif self._valid_move(current_cell.row, current_cell.col - 1):
stack.push(current_cell)
self._mark_path(current_cell.row, current_cell.col - 1)
current_cell = _CellPosition(
current_cell.row, current_cell.col - 1)
# step back if no other options are valid.
else:
# HERE SHOULD BE THE GRAPHICS
if self.visualization:
self.visualization.all_sprites.update()
self.visualization.draw()
self.visualization.events()
pygame.time.wait(self.visualization.TIMESTEP)
self._mark_tried(current_cell.row, current_cell.col)
# if current cell is start cell and there are no other valid moves.
if (current_cell == self._start and not
(self._valid_move(current_cell.row - 1, current_cell.col) or
self._valid_move(current_cell.row, current_cell.col + 1) or
self._valid_move(current_cell.row + 1, current_cell.col) or
self._valid_move(current_cell.row, current_cell.col - 1))):
self._mark_tried(self._start.row,
self._start.col)
return False
current_cell = stack.pop()
# HERE SHOULD BE THE GRAPHICS
if self.visualization:
self.visualization.all_sprites.update()
self.visualization.draw()
self.visualization.events()
pygame.time.wait(self.visualization.TIMESTEP)
if self._exit_found(current_cell.row, current_cell.col):
self._mark_path(current_cell.row, current_cell.col)
return True
def __str__(self):
"""
Returns a text-based representation of the maze.
"""
grid = ''
for row in range(len(self.maze)):
for col in range(len(self.maze[0])):
grid += str(self.maze[row][col]) + ' '
if row != len(self.maze) - 1:
grid += '\n'
return grid
def _valid_move(self, row: int, col: int):
"""
Returns True if the given cell position is a valid move.
"""
return (row >= 0 and row < len(self.maze)
and col >= 0 and col < len(self.maze[0])
and (self.maze[row][col] == self.FREE or
self.maze[row][col] == self.EXIT))
def _exit_found(self, row: int, col: int):
"""
Helper method to determine if the exit was found.
"""
return row == self._exit.row and col == self._exit.col
def _mark_tried(self, row: int, col: int):
"""
Drops a "tried" token at the given cell.
"""
self.maze[row][col] = self.TRIED_TOKEN
def _mark_path(self, row: int, col: int):
"""
Drops a "path" token at the given cell.
"""
self.maze[row][col] = self.PATH_TOKEN
@dataclass
class _CellPosition():
"""
Private storage class for holding a cell position.
"""
row: int
col: int