-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.py
160 lines (140 loc) · 5.37 KB
/
Maze.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
import pygame as pg
import sys
from settings import *
import random
from Solve import Solve
from SolveGBFS import SolveGBFS
from util import QueueFrontier, StackFrontier, Frontier
class Maze:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode(RES)
pg.display.set_caption("Generation and solving maze using DFS")
self.clock = pg.time.Clock()
self.maze_width = MAZE_WIDTH * 2 + 1
self.maze_height = MAZE_HEIGHT * 2 + 1
self.elem_list = []
self.zero_amount = 0
self.cur_x = 1
self.cur_y = 1
self.direction = 0
self.path_list = []
self.zero_amount = MAZE_HEIGHT * MAZE_WIDTH
self.goal_x = self.maze_width - 2
self.goal_y = self.maze_height - 2
def generate(self):
for i in range(0, self.maze_width):
self.elem_list.append([])
for j in range(0, self.maze_height):
if (
i in [0, self.maze_width - 1]
or j in [0, self.maze_height - 1]
or i % 2 == 0
or j % 2 == 0
):
self.elem_list[i].append(1)
else:
self.elem_list[i].append(0)
self.find_path()
def find_path(self):
self.elem_list[self.cur_x][self.cur_y] = 2
self.zero_amount -= 1
while self.zero_amount > 0:
possible_list = self.check_valid()
if len(possible_list):
self.direction = random.choice(possible_list)
if len(possible_list) > 1:
self.path_list.append([self.cur_x, self.cur_y])
self.set_path()
self.zero_amount -= 1
else:
path = random.choice(self.path_list)
self.path_list.remove(path)
self.cur_x = path[0]
self.cur_y = path[1]
def check_valid(self):
temp_list = []
if self.cur_x + 1 != self.maze_width - 1:
if (
self.elem_list[self.cur_x + 1][self.cur_y] != 3
and self.elem_list[self.cur_x + 2][self.cur_y] != 2
):
temp_list.append(0)
if self.cur_y + 1 != self.maze_height - 1:
if (
self.elem_list[self.cur_x][self.cur_y + 1] != 3
and self.elem_list[self.cur_x][self.cur_y + 2] != 2
):
temp_list.append(1)
if self.cur_x - 1 != 0:
if (
self.elem_list[self.cur_x - 1][self.cur_y] != 3
and self.elem_list[self.cur_x - 2][self.cur_y] != 2
):
temp_list.append(2)
if self.cur_y - 1 != 0:
if (
self.elem_list[self.cur_x][self.cur_y - 1] != 3
and self.elem_list[self.cur_x][self.cur_y - 2] != 2
):
temp_list.append(3)
return temp_list
def set_path(self):
if self.direction == 0:
self.elem_list[self.cur_x + 1][self.cur_y] = 3
self.elem_list[self.cur_x + 2][self.cur_y] = 2
self.cur_x += 2
if self.direction == 1:
self.elem_list[self.cur_x][self.cur_y + 1] = 3
self.elem_list[self.cur_x][self.cur_y + 2] = 2
self.cur_y += 2
if self.direction == 2:
self.elem_list[self.cur_x - 1][self.cur_y] = 3
self.elem_list[self.cur_x - 2][self.cur_y] = 2
self.cur_x -= 2
if self.direction == 3:
self.elem_list[self.cur_x][self.cur_y - 1] = 3
self.elem_list[self.cur_x][self.cur_y - 2] = 2
self.cur_y -= 2
def check_events(self):
for event in pg.event.get():
if event.type == pg.QUIT or (
event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE
):
pg.quit()
sys.exit()
def draw_maze(self):
cell_width, cell_height = WIDTH // self.maze_width, HEIGHT // self.maze_height
for y in range(self.maze_height):
for x in range(self.maze_width):
if self.elem_list[y][x] == 1:
color = (0, 0, 0) # Wall cell
elif self.elem_list[y][x] in [2, 3]:
color = (255, 255, 255) # Empty cell
elif self.elem_list[y][x] == 5:
color = (0, 255, 0) # Solution path cell
# Goal cell
if x == self.goal_x and y == self.goal_y:
color = (255, 0, 0)
pg.draw.rect(
self.screen,
color,
(x * cell_width, y * cell_height, cell_width, cell_height),
)
pg.display.flip()
def run(self, method):
print(method)
if method == "-DFS":
solve = Solve(frontier=StackFrontier(), elem_maze=self.elem_list, draw=self.draw_maze)
elif method == "-BFS":
solve = Solve(frontier=QueueFrontier(), elem_maze=self.elem_list, draw=self.draw_maze)
elif method == "-GBFS":
solve = SolveGBFS(frontier=Frontier(), elem_maze=self.elem_list, draw=self.draw_maze)
else:
print("usage: python main.py -DFS | -BFS | -GBFS")
exit()
self.generate()
self.draw_maze()
solve.solve()
while True:
self.check_events()