-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
216 lines (171 loc) · 6.5 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from cell import Cell
from time import sleep
from enum import Enum
import random
class Direction(Enum):
LEFT = 1
RIGHT = 2
UP = 3
DOWN = 4
class Maze:
def __init__(
self,
x1,
y1,
num_rows,
num_cols,
cell_size_x,
cell_size_y,
window,
seed=None
):
self._x1 = x1
self._y1 = y1
self._num_rows = num_rows
self._num_cols = num_cols
self._cell_size_x = cell_size_x
self._cell_size_y = cell_size_y
self._win = window
self._cells = []
self.create_cells()
self.seed = seed
if self.seed is not None:
random.seed(seed)
def create_cells(self):
for col in range(self._num_cols):
self._cells.append(
[Cell(0, 0, 0, 0, self._win) for i in range(self._num_rows)]
)
x_draw_possition = self._x1
y_draw_possition = self._y1
for col in self._cells:
for cell in col:
# Set the top left point of the cell
cell._x1 = x_draw_possition
cell._y1 = y_draw_possition
# Set the bottom right possition of the cell
cell._x2 = x_draw_possition + self._cell_size_x
cell._y2 = y_draw_possition + self._cell_size_y
cell.draw()
self.animate()
y_draw_possition += self._cell_size_y
# Reset the Y possition of the draw to start at the
# top of the next col
y_draw_possition = self._y1
x_draw_possition += self._cell_size_x
self.break_entrance_and_exit()
self.break_walls(0, 0)
self.reset_cells()
self.solve()
def animate(self):
if not self._win:
return
self._win.redraw()
sleep(0.005)
def break_entrance_and_exit(self):
maze_entrance = self._cells[0][0]
maze_exit = self._cells[self._num_cols - 1][self._num_rows - 1]
maze_entrance.has_left_wall = False
maze_exit.has_right_wall = False
maze_entrance.draw()
maze_exit.draw()
def break_walls(self, col, row):
self._cells[col][row].visited = True
while True:
# format [[col, row, direction]], [col, row, direction]]
can_visit = []
if col >= 1:
if not self._cells[col - 1][row].visited:
can_visit.append([col - 1, row, Direction.LEFT])
if col < self._num_cols - 1:
if not self._cells[col + 1][row].visited:
can_visit.append([col + 1, row, Direction.RIGHT])
if row >= 1:
if not self._cells[col][row - 1].visited:
can_visit.append([col, row - 1, Direction.UP])
if row < self._num_rows - 1:
if not self._cells[col][row + 1].visited:
can_visit.append([col, row + 1, Direction.DOWN])
if len(can_visit) == 0:
self._cells[col][row].draw()
break
next_cell_move = random.randint(0, len(can_visit) - 1)
next_cell_values = can_visit[next_cell_move]
next_cell = self._cells[next_cell_values[0]][next_cell_values[1]]
match next_cell_values[2]:
# TODO: Base case from Enum
case Direction.RIGHT:
self._cells[col][row].has_right_wall = False
next_cell.has_left_wall = False
case Direction.LEFT:
self._cells[col][row].has_left_wall = False
next_cell.has_right_wall = False
case Direction.UP:
self._cells[col][row].has_top_wall = False
next_cell.has_bottom_wall = False
case Direction.DOWN:
self._cells[col][row].has_bottom_wall = False
next_cell.has_top_wall = False
self._cells[col][row].draw()
next_cell.draw()
self.break_walls(next_cell_values[0], next_cell_values[1])
def reset_cells(self):
for col in self._cells:
for cell in col:
cell.visited = False
def solve(self):
solved = self.solve_r(0, 0)
return solved
def solve_r(self, col, row):
self.animate()
current_cell = self._cells[col][row]
current_cell.visited = True
# Check if we are at the end of the maze if we are return
if current_cell == self._cells[self._num_cols - 1][self._num_rows - 1]:
return True
# Draw our move to each direction
if col >= 1 and not current_cell.has_left_wall:
if not self._cells[col - 1][row].visited:
current_cell.draw_move(self._cells[col - 1][row])
solved = self.solve_r(col - 1, row)
if solved:
return True
else:
current_cell.draw_move(
self._cells[col - 1][row],
undo=True
)
if col < self._num_cols - 1 and not current_cell.has_right_wall:
if not self._cells[col + 1][row].visited:
current_cell.draw_move(self._cells[col + 1][row])
solved = self.solve_r(col + 1, row)
if solved:
return True
else:
current_cell.draw_move(
self._cells[col + 1][row],
undo=True
)
if row >= 1 and not current_cell.has_top_wall:
if not self._cells[col][row - 1].visited:
current_cell.draw_move(self._cells[col][row - 1])
solved = self.solve_r(col, row - 1)
if solved:
return True
else:
current_cell.draw_move(
self._cells[col][row - 1],
undo=True
)
if row < self._num_rows - 1 and not current_cell.has_bottom_wall:
if not self._cells[col][row + 1].visited:
current_cell.draw_move(self._cells[col][row + 1])
solved = self.solve_r(col, row + 1)
if solved:
return True
else:
current_cell.draw_move(
self._cells[col][row + 1],
undo=True
)
return False