-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
161 lines (140 loc) · 5.16 KB
/
maze.py
File metadata and controls
161 lines (140 loc) · 5.16 KB
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
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()
time.sleep(3)
self.__break_entrance_and_exit()
self.__break_walls_r(0, 0)
self.__reset_cells_visited()
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 = []
if i > 0 and not self.__cells[i - 1][j].visited:
next_index_list.append((i - 1, j))
if i < self.__num_cols - 1 and not self.__cells[i + 1][j].visited:
next_index_list.append((i + 1, j))
if j > 0 and not self.__cells[i][j - 1].visited:
next_index_list.append((i, j - 1))
if j < self.__num_rows - 1 and not self.__cells[i][j + 1].visited:
next_index_list.append((i, j + 1))
if len(next_index_list) == 0:
self.__draw_cell(i, j)
return
direction_index = random.randrange(len(next_index_list))
next_index = next_index_list[direction_index]
if next_index[0] == i + 1:
self.__cells[i][j].has_right_wall = False
self.__cells[i + 1][j].has_left_wall = False
if next_index[0] == i - 1:
self.__cells[i][j].has_left_wall = False
self.__cells[i - 1][j].has_right_wall = False
if next_index[1] == j + 1:
self.__cells[i][j].has_bottom_wall = False
self.__cells[i][j + 1].has_top_wall = False
if next_index[1] == j - 1:
self.__cells[i][j].has_top_wall = False
self.__cells[i][j - 1].has_bottom_wall = False
self.__break_walls_r(next_index[0], next_index[1])
def __reset_cells_visited(self):
for col in self.__cells:
for cell in col:
cell.visited = False
def _solve_r(self, i, j):
self.__animate()
self.__cells[i][j].visited = True
if i == self.__num_cols - 1 and j == self.__num_rows - 1:
return True
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)
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)
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)
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)
return False
def solve(self):
return self._solve_r(0, 0)