-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
175 lines (141 loc) · 5.07 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
from cell import Cell
import time
import random
class Maze:
def __init__(
self,
x1,
y1,
rows,
columns,
cell_size_x,
cell_size_y,
win=None,
seed=None
):
self._cells = []
self._x1 = x1
self._y1 = y1
self._rows = rows
self._columns = columns
self._cell_size_x = cell_size_x
self._cell_size_y = cell_size_y
self._win = win
if seed != None:
random.seed(seed)
else:
random.seed(0)
self._create_cells()
self._break_entrance_and_exit()
self._break_walls_r(0, 0)
self._reset_cells_visited()
def solve(self):
self._solve_r(0, 0)
def _solve_r(self, col, row):
self._animate()
current_cell = self._cells[col][row]
current_cell.visited = True
if col == self._columns - 1 and row == self._rows - 1:
return True
# up
if row > 0 and not current_cell.has_top_wall:
up = self._visit_cell(current_cell, col, row - 1)
if up == True:
return True
# right
if col < self._columns - 1 and not current_cell.has_right_wall:
right = self._visit_cell(current_cell, col + 1, row)
if right == True:
return True
# down
if row < self._rows - 1 and not current_cell.has_bottom_wall:
down = self._visit_cell(current_cell, col, row + 1)
if down == True:
return True
# left
if col > 0 and not current_cell.has_left_wall:
left = self._visit_cell(current_cell, col - 1, row)
if left == True:
return True
return False
def _visit_cell(self, current, next_col, next_row):
cell = self._cells[next_col][next_row]
if not cell.visited:
current.draw_move(cell)
next_result = self._solve_r(next_col, next_row)
if next_result == True:
return True
else:
current.draw_move(cell, undo=True)
return False
def _create_cells(self):
for c in range(self._columns):
cols = []
for r in range(self._rows):
cols.append(Cell(self._win))
self._cells.append(cols)
for c in range(self._columns):
for r in range(self._rows):
self._draw_cell(c, r)
def _draw_cell(self, col, row):
if self._win == None:
return
x1 = self._x1 + col * self._cell_size_x
y1 = self._y1 + row * self._cell_size_y
x2 = x1 + self._cell_size_x
y2 = y1 + self._cell_size_y
self._cells[col][row].draw(x1, y1, x2, y2)
self._animate()
def _animate(self):
self._win.redraw()
time.sleep(0.05)
def _break_entrance_and_exit(self):
self._cells[0][0].has_top_wall = False
self._cells[self._columns - 1][self._rows - 1].has_bottom_wall = False
self._draw_cell(0, 0)
self._draw_cell(self._columns - 1, self._rows - 1)
def _reset_cells_visited(self):
for c in range(self._columns):
for r in range(self._rows):
self._cells[c][r].visited = False
def _break_walls_r(self, col, row):
current_cell = self._cells[col][row]
current_cell.visited = True
while True:
to_visit = []
if row > 0:
up = self._cells[col][row - 1]
if not up.visited:
to_visit.append((col, row - 1))
if col < self._columns - 1:
right = self._cells[col + 1][row]
if not right.visited:
to_visit.append((col + 1, row))
if row < self._rows - 1:
down = self._cells[col][row + 1]
if not down.visited:
to_visit.append((col, row + 1))
if col > 0:
left = self._cells[col - 1][row]
if not left.visited:
to_visit.append((col - 1, row))
if len(to_visit) == 0:
self._draw_cell(col, row)
return
dir = random.randrange(len(to_visit))
next_col = to_visit[dir][0]
next_row = to_visit[dir][1]
next_cell = self._cells[next_col][next_row]
if next_col == col and next_row < row:
current_cell.has_top_wall = False
next_cell.has_bottom_wall = False
if next_col > col and next_row == row:
current_cell.has_right_wall = False
next_cell.has_left_wall = False
if next_col == col and next_row > row:
current_cell.has_bottom_wall = False
next_cell.has_top_wall = False
if next_col < col and next_row == row:
current_cell.has_left_wall = False
next_cell.has_right_wall = False
self._break_walls_r(next_col, next_row)