-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implement.py
294 lines (260 loc) · 13.4 KB
/
Implement.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
import PySimpleGUI as sg
import numpy as np
import copy as cp
class ClassMaze:
def __init__(self, width, height) -> None:
self.__height = height
self.__width = width
self.__maze = [[],[]]
self.__start_cell = (0, 0)
self.__target_cell = (height-1, width-1)
self.__current_cell = list(self.__start_cell)
self.flag = False
self.__VARS = {'cellSizeh': '', 'cellSizew': '', 'gridSize': 400, 'canvas': False, 'window': False, 'cellMAP': False}
def create(self):
self.__maze = np.full((self.__height, self.__width), '#')
self.__maze[self.__start_cell[0]][self.__start_cell[1]] = 'S'
self.__maze[self.__target_cell[0]][self.__target_cell[1]] = 'T'
def display(self):
print(self.__maze)
def display_current_maze(self):
curr_maze = cp.deepcopy(self.__maze)
curr_maze[self.__current_cell[0]][self.__current_cell[1]] = 'C'
print(curr_maze)
def update_empty_cell(self, row, col):
if(self.__maze[row][col]!='T' and self.__maze[row][col]!='S'):
self.__maze[row][col] = ' '
def update_start_cell(self, cell):
self.__maze[self.__start_cell[0]][self.__start_cell[1]] = '#'
self.__start_cell = cell
self.__current_cell = list(self.__start_cell)
self.__maze[self.__start_cell[0]][self.__start_cell[1]] = 'S'
def update_target_cell(self, cell):
self.__maze[self.__target_cell[0]][self.__target_cell[1]] = '#'
self.__target_cell = cell
self.__maze[self.__target_cell[0]][self.__target_cell[1]] = 'T'
def check_path_covered(self):
return (list(self.__current_cell)==list(self.__target_cell))
def check_path_exists(self):
if self.solveMazeUtil(self.__start_cell[0], self.__start_cell[1]) == False:
print("Solution doesn't exist")
return False
return True
def check_valid_move(self, x, y): #checks if a position is empty
try:
return ((self.__maze[x][y] == ' ') or (self.__maze[x][y] == 'T'))
except:
return False
def solveMazeUtil1(self,x,y):
if x == self.__target_cell[0] and y == self.__target_cell[1]:
return True
if (x<self.__width and x>=0
and y<self.__height and y>=0 and
((self.check_valid_move(x, y) == True)
or self.__maze[x][y] =='S' or [x, y]==self.__current_cell)):
if self.solveMazeUtil1(x + 1, y) == True:
return True
if self.solveMazeUtil1(x, y + 1) == True:
return True
def solveMazeUtil2(self,x,y):
if x == self.__target_cell[0] and y == self.__target_cell[1]:
return True
if (x<self.__width and x>=0
and y<self.__height and y>=0 and
((self.check_valid_move(x, y) == True)
or self.__maze[x][y] =='S' or [x, y]==self.__current_cell)):
if self.solveMazeUtil2(x - 1, y) == True:
return True
if self.solveMazeUtil2(x, y + 1) == True:
return True
def solveMazeUtil3(self,x,y):
if x == self.__target_cell[0] and y == self.__target_cell[1]:
return True
if (x<self.__width and x>=0
and y<self.__height and y>=0 and
((self.check_valid_move(x, y) == True)
or self.__maze[x][y] =='S' or [x, y]==self.__current_cell)):
if self.solveMazeUtil3(x - 1, y) == True:
return True
if self.solveMazeUtil3(x, y - 1) == True:
return True
def solveMazeUtil4(self,x,y):
if x == self.__target_cell[0] and y == self.__target_cell[1]:
return True
if (x<self.__height and x>=0
and y<self.__width and y>=0 and
((self.check_valid_move(x, y) == True)
or self.__maze[x][y] =='S' or [x, y]==self.__current_cell)):
if self.solveMazeUtil4(x + 1, y) == True:
return True
if self.solveMazeUtil4(x, y - 1) == True:
return True
def validateMaze(self):
if self.__start_cell[0]<=self.__target_cell[0] and self.__start_cell[1]<=self.__target_cell[1]:
self.flag = self.solveMazeUtil1(self.__start_cell[0],self.__start_cell[1])
elif self.__start_cell[0]>=self.__target_cell[0] and self.__start_cell[1]<=self.__target_cell[1]:
self.flag = self.solveMazeUtil2(self.__start_cell[0],self.__start_cell[1])
elif self.__start_cell[0]>=self.__target_cell[0] and self.__start_cell[1]>=self.__target_cell[1]:
self.flag = self.solveMazeUtil3(self.__start_cell[0],self.__start_cell[1])
elif self.__start_cell[0]<=self.__target_cell[0] and self.__start_cell[1]>=self.__target_cell[1]:
self.flag = self.solveMazeUtil4(self.__start_cell[0],self.__start_cell[1])
def move_steps(self, direc, steps): #if a proposed move is valid, updates the current cell
#get the current cell values into x and y
if(self.flag!=True):
self.validateMaze()
if(self.flag!=True):
print("Maze has no proper path for ", self)
return
x, y = self.__current_cell[0], self.__current_cell[1]
if direc == 'L' or direc == 'l' or direc == "Left" or direc == "left":
i = 1
while(i <= steps):
if(self.check_valid_move(x, y - i)):
self.__current_cell = (x, y - i)
i += 1
self.__maze[self.__current_cell[0]][self.__current_cell[1]] = '\u2190' #to display the path travelled
if(self.__current_cell == self.__target_cell):
return -1, self.__current_cell
else:
break
return ((i - 1), self.__current_cell) #returns the valid traversed steps
if direc == 'R' or direc == 'r' or direc == "right" or direc == "Right":
i = 1
while(i <= steps):
chk = self.check_valid_move(x, y + i)
if(self.check_valid_move(x, y + i)):
self.__current_cell = (x, y + i)
i += 1
self.__maze[self.__current_cell[0]][self.__current_cell[1]] = '\u2192'
if(self.__current_cell == self.__target_cell):
return -1, self.__current_cell
else:
break
return ((i - 1), self.__current_cell)
if direc == 'T' or direc == 't' or direc == "top" or direc == "Top":
i = 1
while(i <= steps):
if(self.check_valid_move(x - i, y)):
self.__current_cell = (x - i, y)
i += 1
self.__maze[self.__current_cell[0]][self.__current_cell[1]] = '\u2191'
if(self.__current_cell == self.__target_cell):
return -1, self.__current_cell
else:
break
return ((i - 1), self.__current_cell)
if direc == 'B' or direc == 'b' or direc == "bottom" or direc == "Bottom":
i = 1
while(i <= steps):
if(self.check_valid_move(x + i, y)):
self.__current_cell = (x + i, y)
i += 1
self.__maze[self.__current_cell[0]][self.__current_cell[1]] = '\u2193'
if(self.__current_cell == self.__target_cell):
return -1, self.__current_cell
else:
break
return ((i - 1), self.__current_cell)
def validateEvents(self, e):
move = ''
if len(e) == 1:
if ord(e) == 63232: # UP
move = 'Up'
elif ord(e) == 63233: # DOWN
move = 'Down'
elif ord(e) == 63234: # LEFT
move = 'Left'
elif ord(e) == 63235: # RIGHT
move = 'Right'
# Filter key press Windows :
else:
if e.startswith('Up'):
move = 'Up'
elif e.startswith('Down'):
move = 'Down'
elif e.startswith('Left'):
move = 'Left'
elif e.startswith('Right'):
move = 'Right'
return move
def drawGrid(self):
self.__VARS['canvas'].TKCanvas.create_rectangle(
1, 1, self.__VARS['gridSize'], self.__VARS['gridSize'], outline='BLACK', width=1)
for x in range(self.__height):
self.__VARS['canvas'].TKCanvas.create_line(
((self.__VARS['cellSizeh'] * x), 0), ((self.__VARS['cellSizeh'] * x), self.__VARS['gridSize']),
fill='BLACK', width=1)
for y in range(self.__width):
self.__VARS['canvas'].TKCanvas.create_line(
(0, (self.__VARS['cellSizew'] * x)), (self.__VARS['gridSize'], (self.__VARS['cellSizeh'] * x)),
fill='BLACK', width=1)
def drawCell(self, x, y, color='GREY'):
self.__VARS['canvas'].TKCanvas.create_rectangle(
x, y, x + self.__VARS['cellSizeh'], y + self.__VARS['cellSizew'],
outline='BLACK', fill=color, width=1)
def placeCells(self):
for row in range(self.__VARS['cellMAP'].shape[0]):
for column in range(self.__VARS['cellMAP'].shape[1]):
if(self.__maze[column][row] == '#'):
self.drawCell((self.__VARS['cellSizeh']*row), (self.__VARS['cellSizew']*column), 'DodgerBlue4')
elif(self.__maze[column][row] == ' '):
self.drawCell((self.__VARS['cellSizeh']*row), (self.__VARS['cellSizew']*column), 'azure3')
def GUI(self, var):
AppFont = 'Any 18'
sg.theme('DarkGrey5')
self.__VARS['cellSizeh'] = self.__VARS['gridSize']/self.__height
self.__VARS['cellSizew'] = self.__VARS['gridSize']/self.__width
self.__VARS['cellMAP'] = self.__maze
layout = [[sg.Canvas(size=(self.__VARS['gridSize'], self.__VARS['gridSize']),
background_color='white',
key='canvas')], [sg.Exit(font=AppFont), sg.Text('', key='-exit-', font=AppFont, size=(15, 3))]]
self.__VARS['window'] = sg.Window(str('Maze Mania for - Maze '+var), layout, resizable=True, finalize=True, return_keyboard_events=True, titlebar_text_color='TOMATO', titlebar_font='Times New Roman', titlebar_background_color='black')
self.__VARS['canvas'] = self.__VARS['window']['canvas']
self.drawGrid()
self.placeCells()
self.drawCell((self.__VARS['cellSizeh']*self.__current_cell[1]), (self.__VARS['cellSizew']*self.__current_cell[0]), 'PaleGreen4')
self.drawCell(self.__target_cell[1]*self.__VARS['cellSizeh'], self.__target_cell[0]*self.__VARS['cellSizew'], 'Sienna4')
def GetScreen(self, var):
i = self.__current_cell[0]
j = self.__current_cell[1]
while True: # Event Loop
event, values = self.__VARS['window'].read()
if event in (None, 'Exit'):
break
# Filter key press
yPos = self.__current_cell[1]
xPos = self.__current_cell[0]
if self.validateEvents(event) == 'Up':
if self.__current_cell[0]>0:
if self.__maze[xPos-1][yPos] != '#':
self.__current_cell[0] = self.__current_cell[0] - 1
elif self.validateEvents(event) == 'Down':
if self.__current_cell[1]<self.__height:
if self.__maze[xPos+1][yPos] != '#':
#print("Moving down")
self.__current_cell[0] = self.__current_cell[0] + 1
elif self.validateEvents(event) == 'Left':
if (self.__current_cell[1]>0):
if self.__maze[xPos][yPos-1] != '#':
#print("Moving Left")
self.__current_cell[1] = self.__current_cell[1] - 1
elif self.validateEvents(event) == 'Right':
if (self.__current_cell[0]<self.__width):
if self.__maze[xPos][yPos+1] != '#':
self.__current_cell[1] = self.__current_cell[1] + 1
self.__VARS['canvas'].TKCanvas.delete("all")
self.drawGrid()
self.placeCells()
self.drawCell((self.__VARS['cellSizeh']*j), (self.__VARS['cellSizew']*i), 'GREY')
self.drawCell((self.__VARS['cellSizeh']*self.__current_cell[1]), (self.__VARS['cellSizew']*self.__current_cell[0]), 'PaleGreen4')
self.drawCell(self.__target_cell[1]*self.__VARS['cellSizeh'], self.__target_cell[0]*self.__VARS['cellSizew'], 'Sienna4')
# Check for Exit:
yPos = self.__current_cell[1]
xPos = self.__current_cell[0]
if [xPos, yPos] == list(self.__target_cell):
layout = [[sg.Text(text='Congrats! You reached the end!', font = 'Any 18')],
[sg.Exit(font='Any 12', size=(13, 1))]]
self.__VARS['window'] = sg.Window(str('Maze Mania for - Maze '+var), layout, resizable=True, finalize=True, return_keyboard_events=True, titlebar_text_color='TOMATO', titlebar_font='Times New Roman', titlebar_background_color='black')
else:
self.__VARS['window']['-exit-'].update('')
self.__VARS['window'].close()