-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
289 lines (231 loc) · 8.78 KB
/
classes.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from tkinter import Tk, BOTH, Canvas
from colors import *
import random
import time
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Line:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def draw(self, canvas, fill_color):
canvas.create_line(
self.p1.x, self.p1.y, self.p2.x, self.p2.y, fill=fill_color, width=5
)
class Window:
def __init__(self, width, height):
self.__root = Tk()
self.__root.title("aMazing Solver")
self.canvas = Canvas(self.__root, bg="PaleTurquoise1", width=width, height=height)
self.canvas.pack(fill=BOTH, expand=True)
self.__running = False
self.__root.protocol("WM_DELETE_WINDOW", self.close)
def redraw(self):
self.__root.update_idletasks()
self.__root.update()
def wait_for_close(self):
self.__running = True
while self.__running:
self.redraw()
def close(self):
self.__running = False
self.__root.destroy()
self.__root = None
def draw_line(self, line, fill_color=colorme()):
line.draw(self.canvas, fill_color)
def closewin(self):
return self.close()
class Cell:
def __init__(self, win=None):
self.has_left_wall = True
self.has_right_wall = True
self.has_top_wall = True
self.has_bottom_wall = True
self._x1 = None
self._y1 = None
self._x2 = None
self._y2 = None
self._win = win
self.visited = False
def draw(self, x1, y1, x2, y2):
self._x1 = x1
self._x2 = x2
self._y1 = y1
self._y2 = y2
if self.has_left_wall:
line = Line(Point(x1, y1), Point(x1, y2))
self._win.draw_line(line, colorme())
else:
line = Line(Point(x1, y1), Point(x1, y2))
self._win.draw_line(line, "PaleTurquoise1")
if self.has_top_wall:
line = Line(Point(x1, y1), Point(x2, y1))
self._win.draw_line(line, colorme())
else:
line = Line(Point(x1, y1), Point(x2, y1))
self._win.draw_line(line, "PaleTurquoise1")
if self.has_right_wall:
line = Line(Point(x2, y1), Point(x2, y2))
self._win.draw_line(line, colorme())
else:
line = Line(Point(x2, y1), Point(x2, y2))
self._win.draw_line(line, "PaleTurquoise1")
if self.has_bottom_wall:
line = Line(Point(x1, y2), Point(x2, y2))
self._win.draw_line(line, colorme())
else:
line = Line(Point(x1, y2), Point(x2, y2))
self._win.draw_line(line, "PaleTurquoise1")
def draw_move(self, to_cell, undo=False):
line_color = colorme() if not undo else "cyan4"
self._win.canvas.create_line(
(self._x1 + self._x2)//2, (self._y1 + self._y2)//2,
(to_cell._x1 + to_cell._x2)//2,(to_cell._y1 + to_cell._y2)//2,
fill=line_color, width = 24
)
def mark_visited(self):
offset = int(min(abs(self._x2 - self._x1),abs(self._y2 - self._y1))*.33)
if self.visited is True:
self._win.canvas.create_rectangle(
self._x1+offset , self._y1+offset, self._x2-offset, self._y2-offset,
fill="goldenrod1", outline="black",
),
else:
self._win.canvas.create_rectangle(
self._x1+offset , self._y1+offset, self._x2-offset, self._y2-offset,
fill="PaleTurquoise1", outline="PaleTurquoise1",
),
self._win.redraw()
class Maze:
def __init__(
self,
mazex,
mazey,
num_rows,
num_cols,
cell_size_x,
cell_size_y,
win=None, seed=None
):
self._mazex = mazex
self._mazey = mazey
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 is not None: random.seed(seed)
self._create_cells()
self._break_entrance_and_exit()
self._wallbreaker()
self._reset_cells_visited()
def _create_cells(self):
self._cells = []
for cols in range(self._num_cols):
rowlist = []
for row in range(self._num_rows):
rowlist.append(Cell(self._win))
self._cells.append(rowlist)
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, offset=7):
if self._win is None:
return
x1 = -offset+self._mazex + i * self._cell_size_x
y1 = -offset+self._mazey + j * self._cell_size_y
x2 = offset+x1 + self._cell_size_x
y2 = offset+y1 + self._cell_size_y
self._cells[i][j].draw(x1, y1, x2, y2)
self._animate()
def _animate(self):
self._win.redraw()
time.sleep(0.001)
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 _wallbreaker(self, i=0, j=0):
self._cells[i][j].visited = True
while True:
visitable = []
if i > 0 and self._cells[i-1][j].visited is False:
visitable.append([i-1, j])
if i < self._num_cols-1 and self._cells[i+1][j].visited is False:
visitable.append([i+1, j])
if j > 0 and self._cells[i][j-1].visited is False:
visitable.append([i, j-1])
if j < self._num_rows-1 and self._cells[i][j+1].visited is False:
visitable.append([i, j+1])
if len(visitable) == 0:
return
visiting = random.choice(visitable)
if visiting[0] == i + 1:
self._cells[i][j].has_right_wall = False
self._draw_cell(i, j)
self._cells[i + 1][j].has_left_wall = False
self._draw_cell(i+1, j)
if visiting[0] == i - 1:
self._cells[i][j].has_left_wall = False
self._draw_cell(i, j)
self._cells[i - 1][j].has_right_wall = False
self._draw_cell(i-1, j)
if visiting[1] == j + 1:
self._cells[i][j].has_bottom_wall = False
self._draw_cell(i, j)
self._cells[i][j + 1].has_top_wall = False
self._draw_cell(i, j+1)
if visiting[1] == j - 1:
self._cells[i][j].has_top_wall = False
self._draw_cell(i, j)
self._cells[i][j - 1].has_bottom_wall = False
self._draw_cell(i, j-1)
self._wallbreaker(visiting[0],visiting[1])
def _reset_cells_visited(self):
for cols in self._cells:
for cell in cols:
cell.visited = False
def solve(self):
return self._solve_r(0,0)
def _solve_r(self, i, j):
self._animate()
self._cells[i][j].visited = True
if self._cells[self._num_cols-1][self._num_rows-1].visited == True:
return self._celebration()
if not self._cells[i][j].has_left_wall:
if 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)
if not self._cells[i][j].has_bottom_wall:
if 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)
if not self._cells[i][j].has_top_wall:
if 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)
if not self._cells[i][j].has_right_wall:
if 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)
return False
def _celebration(self):
# celebrate....more color, shapez if updated in future.
print('Found the Exit')
time.sleep(2)
exit()