-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
267 lines (154 loc) · 5.18 KB
/
Copy pathmaze.py
File metadata and controls
267 lines (154 loc) · 5.18 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
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
import tkinter as tk
from constants import *
class MazeGrid:
def __init__(self, parent, rows=ROWS, cols=COLS):
self.parent = parent
self.rows = rows
self.cols = cols
self.current_tool = "Wall"
self.start = None
self.goal = None
self.cells = []
self.create_grid()
# ===========================================
def create_grid(self):
for row in range(self.rows):
current_row = []
self.parent.grid_rowconfigure(row, minsize=CELL_SIZE)
for col in range(self.cols):
self.parent.grid_columnconfigure(col, minsize=CELL_SIZE)
cell = tk.Button(
self.parent,
width=1,
height=1,
bg=EMPTY,
relief="flat",
bd=1,
highlightthickness=0,
activebackground="#E2E8F0",
activeforeground=TEXT,
cursor="hand2",
command=lambda r=row, c=col: self.cell_clicked(r, c)
)
cell.grid(
row=row,
column=col,
padx=2,
pady=2,
sticky="nsew"
)
current_row.append(cell)
self.cells.append(current_row)
# ===========================================
def set_tool(self, tool):
self.current_tool = tool
# ===========================================
def cell_clicked(self, row, col):
cell = self.cells[row][col]
# -------- Wall --------
if self.current_tool == "Wall":
if self.start == (row, col) or self.goal == (row, col):
return
cell.configure(bg=WALL)
# -------- Erase --------
elif self.current_tool == "Erase":
cell.configure(bg=EMPTY)
if self.start == (row, col):
self.start = None
if self.goal == (row, col):
self.goal = None
# -------- Start --------
elif self.current_tool == "Start":
if self.start is not None:
old_row, old_col = self.start
self.cells[old_row][old_col].configure(bg=EMPTY)
if self.goal == (row, col):
self.goal = None
self.start = (row, col)
cell.configure(bg=START)
# -------- Goal --------
elif self.current_tool == "Goal":
if self.goal is not None:
old_row, old_col = self.goal
self.cells[old_row][old_col].configure(bg=EMPTY)
if self.start == (row, col):
self.start = None
self.goal = (row, col)
cell.configure(bg=GOAL)
# ===========================================
def reset_grid(self):
"""
Reset the complete maze.
"""
for row in range(self.rows):
for col in range(self.cols):
self.cells[row][col].configure(bg=EMPTY)
self.start = None
self.goal = None
# ===========================================
def get_start(self):
"""
Return Start Position
"""
return self.start
# ===========================================
def get_goal(self):
"""
Return Goal Position
"""
return self.goal
# ===========================================
def get_maze(self):
"""
Convert GUI grid into 2D matrix.
0 = Empty
1 = Wall
"""
maze = []
for row in range(self.rows):
current = []
for col in range(self.cols):
color = self.cells[row][col]["bg"]
if color == WALL:
current.append(1)
else:
current.append(0)
maze.append(current)
return maze
# ===========================================
def color_path(self, path):
"""
Highlight the shortest path.
"""
if path is None:
return
self.clear_path()
for row, col in path:
if (row, col) == self.start:
continue
if (row, col) == self.goal:
continue
self.cells[row][col].configure(bg=PATH)
# ===========================================
def clear_path(self):
"""
Remove any previous path markings.
"""
for row in range(self.rows):
for col in range(self.cols):
if self.cells[row][col]["bg"] == PATH:
self.cells[row][col].configure(bg=EMPTY)
# ===========================================
def rebuild(self, rows, cols):
"""
Rebuild the grid when maze size changes.
"""
for row in self.cells:
for cell in row:
cell.destroy()
self.rows = rows
self.cols = cols
self.start = None
self.goal = None
self.cells = []
self.create_grid()