-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyamaze.py
301 lines (277 loc) · 9.11 KB
/
pyamaze.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from app import TileType
"""
to run simply run app.py or gui.py,
FAQs, Troubleshooting, and further instructions in the docs (report.pdf)
"""
"""
License
https://www.youtube.com/c/LearningOrbis
Copyright (c) 2021 Muhammad Ahsan Naeem
mahsan.naeem@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from tkinter import *
from enum import Enum
class COLOR(Enum):
'''
This class is created to use the Tkinter colors easily.
Each COLOR object has two color values.
The first two objects (dark and light) are for theme and the two color
values represent the Canvas color and the Maze Line color respectively.
The rest of the colors are for Agents.
The first value is the color of the Agent and the second is the color of
its footprint
'''
dark=('gray11','white')
light=('white','black')
black=('black','dim gray')
red=('red3','tomato')
cyan=('cyan4','cyan4')
green=('green4','pale green')
blue=('DeepSkyBlue4','DeepSkyBlue2')
yellow=('yellow2','yellow2')
class agent:
'''
The agents can be placed on the maze.
They can represent the virtual object just to indcate the cell selected in Maze.
Or they can be the physical agents (like robots)
They can have two shapes (square or arrow)
'''
def __init__(self,parentMaze,color:COLOR=COLOR.blue, count=False):
'''
parentmaze--> The maze on which agent is placed.
x,y--> Position of the agent i.e. cell inside which agent will be placed
Default value is the lower right corner of the Maze
shape--> square or arrow (as string)
goal--> Default value is the goal of the Maze
filled--> For square shape, filled=False is a smaller square
While filled =True is a biiger square filled in complete Cell
This option doesn't matter for arrow shape.
footprints--> When the aganet will move to some other cell, its footprints
on the previous cell can be placed by making this True
color--> Color of the agent.
_orient--> You don't need to pass this
It is used with arrow shape agent to shows it turning
position--> You don't need to pass this
This is the cell (x,y)
_head--> You don't need to pass this
It is actually the agent.
_body--> You don't need to pass this
Tracks the body of the agent (the previous positions of it)
'''
self._parentMaze=parentMaze
self.color=color
if(isinstance(color,str)):
if(color in COLOR.__members__):
self.color=COLOR[color]
else:
raise ValueError(f'{color} is not a valid COLOR!')
self.filled=True
self.shape='square'
self._orient=0
self.footprints=True
self._parentMaze._agents.append(self)
self._body=[]
self.count = count
self.steps = 0
self.x = -1
self.y = -1
@property
def x(self):
return self._x
@x.setter
def x(self,newX):
self._x=newX
@property
def y(self):
return self._y
@y.setter
def y(self,newY):
self._y=newY
def updatePos(self):
w=self._parentMaze._cell_width
x=self.x*w+self._parentMaze._LabWidth
y=self.y*w+self._parentMaze._LabWidth
coord = self._coord=(y, x,y + w, x + w)
pos = self.position
labels = self._parentMaze.maze_label
self._head=self._parentMaze._canvas.create_rectangle(*coord, fill=self.color.value[1], outline='')
if self.count:
self.steps += 1
labels[pos] = str(self.steps)
if pos in labels:
self._parentMaze._canvas.create_text((coord[0]+w/2,coord[1]+w/2), text = labels[pos], fill='black')
@property
def position(self):
return (self.x,self.y)
@position.setter
def position(self,newpos):
self.x=newpos[0]
self.y=newpos[1]
self._position=newpos
class textLabel:
'''
This class is to create Text Label to show different results on the window.
'''
def __init__(self,parentMaze,title,value):
'''
parentmaze--> The maze on which Label will be displayed.
title--> The title of the value to be displayed
value--> The value to be displayed
'''
self.title=title
self._value=value
self._parentMaze=parentMaze
# self._parentMaze._labels.append(self)
self._var=None
self.drawLabel()
@property
def value(self):
return self._value
@value.setter
def value(self,v):
self._value=v
self._var.set(f'{self.title} : {v}')
def drawLabel(self):
self._var = StringVar()
self.lab = Label(self._parentMaze._canvas, textvariable=self._var, bg="white", fg="black",font=('Helvetica bold',12),relief=RIDGE)
self._var.set(f'{self.title} : {self.value}')
self.lab.pack(expand = True,side=LEFT,anchor=NW)
class maze:
'''
This is the main class to create maze.
'''
def __init__(self):
'''
rows--> No. of rows of the maze
cols--> No. of columns of the maze
Need to pass just the two arguments. The rest will be assigned automatically
grid--> A list of all cells
path--> Shortest path from start(bottom right) to goal(by default top left)
It will be a dictionary
_win,_cell_width,_canvas --> _win and )canvas are for Tkinter window and canvas
_cell_width is cell width calculated automatically
_agents--> A list of aganets on the maze
markedCells--> Will be used to mark some particular cell during
path trace by the agent.
_
'''
self.maze_label={}
self.path={}
self._cell_width=50
self._win=None
self._canvas=None
self._agents=[]
self.markCells=[]
@property
def grid(self):
return self._grid
@grid.setter
def grid(self,n):
self._grid=[]
y=0
for _ in range(self.cols):
x = 1
y = 1+y
for _ in range(self.rows):
self.grid.append((x,y))
x = x + 1
def CreateMaze(self,map:list[list[TileType]],theme:COLOR=COLOR.dark):
'''
One very important function to create a Random Maze
pattern--> It can be 'v' for vertical or 'h' for horizontal
Just the visual look of the maze will be more vertical/horizontal
passages will be there.
loopPercent--> 0 means there will be just one path from start to goal (perfect maze)
Higher value means there will be multiple paths (loops)
Higher the value (max 100) more will be the loops
saveMaze--> To save the generated Maze as CSV file for future reference.
loadMaze--> Provide the CSV file to generate a desried maze
theme--> Dark or Light
'''
self.theme=theme
if(isinstance(theme,str)):
if(theme in COLOR.__members__):
self.theme=COLOR[theme]
else:
raise ValueError(f'{theme} is not a valid theme COLOR!')
self.rows = len(map)
self.cols = len(map[0])
self.grid=[]
self._drawMaze(self.theme)
def _drawMaze(self,theme):
'''
Creation of Tkinter window and maze lines
'''
self._LabWidth=26 # Space from the top for Labels
self._win=Tk()
self._win.state('zoomed')
self._win.title('PYTHON MAZE WORLD by Learning Orbis')
scr_width=self._win.winfo_screenwidth()
scr_height=self._win.winfo_screenheight()
self._win.geometry(f"{scr_width}x{scr_height}+0+0")
self._canvas = Canvas(width=scr_width, height=scr_height, bg=theme.value[0]) # 0,0 is top left corner
self._canvas.pack(expand=YES, fill=BOTH)
# Some calculations for calculating the width of the maze cell
k=3.25
if self.rows>=95 and self.cols>=95:
k=0
elif self.rows>=80 and self.cols>=80:
k=1
elif self.rows>=70 and self.cols>=70:
k=1.5
elif self.rows>=50 and self.cols>=50:
k=2
elif self.rows>=35 and self.cols>=35:
k=2.5
elif self.rows>=22 and self.cols>=22:
k=3
self._cell_width=round(min(((scr_height-self.rows-k*self._LabWidth)/(self.rows)),((scr_width-self.cols-k*self._LabWidth)/(self.cols)),90),3)
_tracePathList=[]
def _tracePathSingle(self,a: agent,p,delay):
'''
An interal method to help tracePath method for tracing a path by agent.
'''
if(len(p)==0):
del maze._tracePathList[0]
if len(maze._tracePathList) == 0:
return
for q,w in maze._tracePathList[0][0].items():
a=q
p=w
delay=maze._tracePathList[0][1]
a.x,a.y=p[0]
a.updatePos()
del p[0]
self._win.after(delay, self._tracePathSingle,a,p,delay)
def tracePath(self,d,delay=300):
'''
A method to trace path by agent
You can provide more than one agent/path details
'''
for a in d.items():
if len(a[1]) == 0:
return
self._tracePathList.append((d,delay))
if maze._tracePathList[0][0]==d:
for a,p in d.items():
self._tracePathSingle(a,p,delay)
def run(self):
'''
Finally to run the Tkinter Main Loop
'''
self._win.mainloop()