-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
153 lines (131 loc) · 5.24 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
from graphics import Line, Point, Window
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
self.seed=random.seed(seed)
self.animate_speed=.001
self._create_cells()
self._break_entrance_and_exit()
self._break_walls_r(0,0)
self._reset_cells_visited()
self.solve()
def _create_cells(self):
for i in range(0, self.num_cols):
temp_cells=[]
for j in range(0,self.num_rows):
tl=Point(self.x1+(j*self.cell_size_x), self.y1+(i*self.cell_size_y))
br=Point(self.x1+((j+1)*self.cell_size_x), self.y1+((i+1)*self.cell_size_y))
my_cell=Cell(tl, br, self.win)
temp_cells.append(my_cell)
self._cells.append(temp_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
x_mod=self.x1+(i*self.cell_size_x)
y_mod=self.y1+(j*self.cell_size_y)
x2_mod=x_mod+self.cell_size_x
y2_mod=y_mod+self.cell_size_y
p1=Point(x_mod,y_mod)
p2=Point(x2_mod,y2_mod)
print(f"Drawing cell {i} {j}")
self._cells[i][j].draw(p1, p2)
self._animate()
def _animate(self):
self.win.redraw()
time.sleep(self.animate_speed)
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)
self._draw_cell(0, 0)
def _break_walls_r(self, i, j):
self._cells[i][j].visited=True
while True:
to_visit=[]
current_visitable=len(to_visit)
# left
if i > 0 and not self._cells[i - 1][j].visited:
to_visit.append((i-1, j))
# right
if i < self.num_cols - 1 and not self._cells[i + 1][j].visited:
to_visit.append((i+1, j))
# up
if j > 0 and not self._cells[i][j - 1].visited:
to_visit.append((i, j-1))
# down
if j < self.num_rows - 1 and not self._cells[i][j + 1].visited:
to_visit.append((i, j+1))
if len(to_visit)==0:
self._draw_cell(i,j)
return
random_pick=random.randrange(len(to_visit))
pick=to_visit[random_pick]
if pick[0]==i+1:
self._cells[i][j].has_right_wall=False
self._cells[i+1][j].has_left_wall=False
elif pick[0]==i-1:
self._cells[i-1][j].has_right_wall=False
self._cells[i][j].has_left_wall=False
elif pick[1]==j+1:
self._cells[i][j].has_bottom_wall=False
self._cells[i][j+1].has_top_wall=False
elif pick[1]==j-1:
self._cells[i][j].has_top_wall=False
self._cells[i][j-1].has_bottom_wall=False
self._break_walls_r(pick[0], pick[1])
def _reset_cells_visited(self):
for i in range(0,self.num_cols):
for j in range(0,self.num_rows):
self._cells[i][j].visited=False
def _solve_r(self,i,j):
self.animate_speed=.05
self._animate()
self._cells[i][j].visited=True
print(f"Solving... {i} {j}")
if i==self.num_cols-1 and j==self.num_rows-1:
print("Found the end!")
return True
# left
if self._cells[i][j].has_left_wall==False and i > 0 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], undo=True)
# right
if self._cells[i][j].has_right_wall==False and i < self.num_cols - 1 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], undo=True)
# up
if self._cells[i][j].has_top_wall==False and j > 0 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], undo=True)
# down
if self._cells[i][j].has_bottom_wall==False and j < self.num_rows - 1 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], undo=True)
def solve(self):
self._solve_r(0,0)