-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
192 lines (168 loc) · 6.33 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
from cell import Cell
import random
import time
class Maze:
def __init__(
self,
x1,
y1,
num_rows,
num_cols,
cell_size_x,
cell_size_y,
win=None,
seed=None,
):
self._cells = []
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 = win
if seed:
random.seed(seed)
self._create_cells()
self._break_entrance_and_exit()
self._break_walls_r(0, 0)
self._reset_cells_visted()
def _create_cells(self):
for i in range(self._num_cols):
col_cells = []
for j in range(self._num_rows):
col_cells.append(Cell(self._win))
self._cells.append(col_cells)
for i in range(self._num_cols):
for j in range(self._num_rows):
self._draw_cell(i, j)
def _draw_cell(self, i, j):
if self._win is None:
return
x1 = self._x1 + i * self._cell_size_x
y1 = self._y1 + j * self._cell_size_y
x2 = x1 + self._cell_size_x
y2 = y1 + self._cell_size_y
self._cells[i][j].draw(x1, y1, x2, y2)
self._animate()
def _animate(self):
if self._win is None:
return
self._win.redraw()
time.sleep(0.05)
def _break_entrance_and_exit(self):
self._cells[0][0].has_top_wall = False
self._draw_cell(0, 0)
self._cells[self._num_cols - 1][self._num_rows - 1].has_bottom_wall = False
self._draw_cell(self._num_cols - 1, self._num_rows - 1)
def _break_walls_r(self, i, j):
self._cells[i][j].visited = True
while True:
next_index_list = []
possible_direction_indexes = 0
# determine which cell(s) to visit next
# left
if i > 0 and not self._cells[i - 1][j].visited:
next_index_list.append((i - 1, j))
possible_direction_indexes += 1
# right
if i < self._num_cols - 1 and not self._cells[i + 1][j].visited:
next_index_list.append((i + 1, j))
possible_direction_indexes += 1
# up
if j > 0 and not self._cells[i][j - 1].visited:
next_index_list.append((i, j - 1))
possible_direction_indexes += 1
# down
if j < self._num_rows - 1 and not self._cells[i][j + 1].visited:
next_index_list.append((i, j + 1))
possible_direction_indexes += 1
# if there is nowhere to go from here
# just break out
if possible_direction_indexes == 0:
self._draw_cell(i, j)
return
# randomly choose the next direction to go
direction_index = random.randrange(possible_direction_indexes)
next_index = next_index_list[direction_index]
# knock out walls between this cell and the next cell(s)
# right
if next_index[0] == i + 1:
self._cells[i][j].has_right_wall = False
self._cells[i + 1][j].has_left_wall = False
# left
if next_index[0] == i - 1:
self._cells[i][j].has_left_wall = False
self._cells[i - 1][j].has_right_wall = False
# down
if next_index[1] == j + 1:
self._cells[i][j].has_bottom_wall = False
self._cells[i][j + 1].has_top_wall = False
# up
if next_index[1] == j - 1:
self._cells[i][j].has_top_wall = False
self._cells[i][j - 1].has_bottom_wall = False
# recursively visit the next cell
self._break_walls_r(next_index[0], next_index[1])
def _reset_cells_visted(self):
for col in self._cells:
for cell in col:
cell.visited = False
# returns True if this is the end cell, OR if it leads to the end cell.
# returns False if this is a loser cell.
def _solve_r(self, i, j):
self._animate()
# vist the current cell
self._cells[i][j].visited = True
# if we are at the end cell, we are done!
if i == self._num_cols - 1 and j == self._num_rows - 1:
return True
# move left if there is no wall and it hasn't been visited
if (
i > 0
and not self._cells[i][j].has_left_wall
and not self._cells[i - 1][j].visited
):
self._cells[i][j].draw_move(self._cells[i - 1][j])
if self._solve_r(i - 1, j):
return True
else:
self._cells[i][j].draw_move(self._cells[i - 1][j], True)
# move right if there is no wall and it hasn't been visited
if (
i < self._num_cols - 1
and not self._cells[i][j].has_right_wall
and not self._cells[i + 1][j].visited
):
self._cells[i][j].draw_move(self._cells[i + 1][j])
if self._solve_r(i + 1, j):
return True
else:
self._cells[i][j].draw_move(self._cells[i + 1][j], True)
# move up if there is no wall and it hasn't been visited
if (
j > 0
and not self._cells[i][j].has_top_wall
and not self._cells[i][j - 1].visited
):
self._cells[i][j].draw_move(self._cells[i][j - 1])
if self._solve_r(i, j - 1):
return True
else:
self._cells[i][j].draw_move(self._cells[i][j - 1], True)
# move down if there is no wall and it hasn't been visited
if (
j < self._num_rows - 1
and not self._cells[i][j].has_bottom_wall
and not self._cells[i][j + 1].visited
):
self._cells[i][j].draw_move(self._cells[i][j + 1])
if self._solve_r(i, j + 1):
return True
else:
self._cells[i][j].draw_move(self._cells[i][j + 1], True)
# we went the wrong way let the previous cell know by returning False
return False
# create the moves for the solution using a depth first search
def solve(self):
return self._solve_r(0, 0)