-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
125 lines (90 loc) · 3.3 KB
/
main.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
from cell import *
import random
pygame.init()
# Set up
grid = [] #Maze grid
for y in range(rows):
temp_row = [] #Temporary row
for x in range(cols):
temp = Cell(x,y) #Create cells in that row
temp_row.append(temp) #Put them in that row
grid.append(temp_row) #Put row in grid
#For any given cell, returns a random neighboring cell
def get_random_neighbor(cell):
#Create a list to store possible choices
neighbors = []
#Loop through grid to find the input cell
for y in range(rows):
for x in range(cols):
#Once we have found the cell
if cell.y == y and cell.x == x:
#Top neighbor
if y-1 >= 0 and not grid[y-1][x].visited:
neighbors.append(grid[y-1] [x])
#Right neighbor
if x+1 <= cols-1 and not grid[y][x+1].visited:
neighbors.append(grid[y] [x+1])
#Bottom neighbor
if y+1 <= rows-1 and not grid[y+1][x].visited:
neighbors.append(grid[y+1] [x])
#Left neighbor
if x-1 >= 0 and not grid[y][x-1].visited:
neighbors.append(grid[y] [x-1])
#If there are potential neighbors
if len(neighbors) > 0:
#Randomly select one
random_index = random.randint(0, len(neighbors)-1)
return neighbors[random_index]
#Removes the walls from one cell to another
def remove_walls(current, next):
delta_x = current.x - next.x
delta_y = current.y - next.y
if delta_x == -1:
current.right_line = False
next.left_line = False
elif delta_x == 1:
current.left_line = False
next.right_line = False
elif delta_y == -1:
current.bottom_line = False
next.top_line = False
elif delta_y == 1:
current.top_line = False
next.bottom_line = False
#Stack for the backtracking part of the algo
stack = []
#Sets the starting point in the top right corner
current_cell = grid[0][0]
maze_complete = False
#Main loop
while 1:
clock.tick(FPS)
# Checks to see if user exits
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if not maze_complete:
#Mark current cell as visited
current_cell.visited = True
next_cell = get_random_neighbor(current_cell)
#Checks to make sure we actually have a valid neighbor
if next_cell:
#Add the cell to the stack
stack.append(current_cell)
#Remove walls between current_cell and next_cell,
#then let the next_cell become the next current_cell
remove_walls(current_cell, next_cell)
current_cell = next_cell
#If no valid neighbor, backtrack
elif len(stack) > 0:
current_cell = stack.pop()
#When stack empties, maze is complete
elif len(stack) == 0 and not next_cell:
maze_complete = True
#Draw all cells
for y in range(rows):
for x in range(cols):
grid[y][x].draw()
else:
print("Maze complete!")
pygame.display.update()