Skip to content

Commit a1a0349

Browse files
authored
Add files via upload
1 parent 485a7d7 commit a1a0349

File tree

5 files changed

+82
-46
lines changed

5 files changed

+82
-46
lines changed

algorithms/depth_first_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def algorithm(start, end, draw, win):
2929
if current_node not in (start, end):
3030
current_node.draw_open()
3131

32-
# At the start of every iteration, pop the top element from the queue
32+
# At the start of every iteration, pop the top element from the stack
3333
current_node = frontier.pop()
3434

3535
# If we found the end node, draw the path

algorithms/prims.py

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,91 @@
11
import random
22
import constants
3+
import pygame
34

45
# I followed along with this guide to implement this algorithm into my program.
56
# https://medium.com/swlh/fun-with-python-1-maze-generator-931639b4fb7e
67

78

8-
def algorithm(grid, draw):
9+
def algorithm(grid, draw, win):
910
# Choose a random starting point and reset all nodes
1011
grid, walls = init_path(grid)
1112

1213
while walls:
14+
15+
for event in pygame.event.get():
16+
if event.type == pygame.QUIT:
17+
pygame.quit()
18+
19+
if pygame.mouse.get_pressed(3)[0]:
20+
pos = pygame.mouse.get_pos()
21+
if 660 <= pos[1] <= 690:
22+
if 150 <= pos[0] <= 270:
23+
if win.speed == "Fast":
24+
win.speed = "Medium"
25+
elif win.speed == "Medium":
26+
win.speed = "Slow"
27+
else:
28+
win.speed = "Fast"
29+
30+
if win.speed == "Medium":
31+
pygame.time.wait(10)
32+
elif win.speed == "Slow":
33+
pygame.time.wait(50)
1334

1435
# Pick a random wall
1536
wall = walls.pop(int(random.random() * len(walls)) - 1)
1637

38+
row, col = wall
39+
1740
# Check if wall is to the left of the path
18-
if wall[1] != 0:
19-
if grid[wall[0]][wall[1] - 1].is_default() and grid[wall[0]][wall[1] + 1].is_path():
41+
if col != 0:
42+
if grid[row][col - 1].is_default() and grid[row][col + 1].is_path():
2043
neighbours = get_path_neighbours(wall, grid)
2144

2245
if neighbours < 2:
2346
# Denote the new path
24-
grid[wall[0]][wall[1]].draw_path()
47+
grid[row][col].draw_path()
2548

2649
walls, grid = top_wall(wall, walls, grid)
2750
walls, grid = bottom_wall(wall, walls, grid)
2851
walls, grid = left_wall(wall, walls, grid)
2952

3053
# Check if wall is above the path
31-
if wall[0] != 0:
32-
if grid[wall[0] - 1][wall[1]].is_default() and grid[wall[0] + 1][wall[1]].is_path():
54+
if row != 0:
55+
if grid[row - 1][col].is_default() and grid[row + 1][col].is_path():
3356
neighbours = get_path_neighbours(wall, grid)
3457

3558
if neighbours < 2:
3659
# Denote the new path
37-
grid[wall[0]][wall[1]].draw_path()
60+
grid[row][col].draw_path()
3861

3962
# Mark the new walls
4063
walls, grid = top_wall(wall, walls, grid)
4164
walls, grid = left_wall(wall, walls, grid)
4265
walls, grid = right_wall(wall, walls, grid)
4366

4467
# Check if wall is below the path
45-
if wall[0] != constants.rows - 1:
46-
if grid[wall[0] + 1][wall[1]].is_default() and grid[wall[0] - 1][wall[1]].is_path():
68+
if row != constants.rows - 1:
69+
if grid[row + 1][col].is_default() and grid[row - 1][col].is_path():
4770
neighbours = get_path_neighbours(wall, grid)
4871

4972
if neighbours < 2:
5073
# Denote the new path
51-
grid[wall[0]][wall[1]].draw_path()
74+
grid[row][col].draw_path()
5275

5376
# Mark the new walls
5477
walls, grid = bottom_wall(wall, walls, grid)
5578
walls, grid = left_wall(wall, walls, grid)
5679
walls, grid = right_wall(wall, walls, grid)
5780

5881
# Check if wall is to the right of the path
59-
if wall[1] != constants.cols - 1:
60-
if grid[wall[0]][wall[1] + 1].is_default() and grid[wall[0]][wall[1] - 1].is_path():
82+
if col != constants.cols - 1:
83+
if grid[row][col + 1].is_default() and grid[row][col - 1].is_path():
6184
neighbours = get_path_neighbours(wall, grid)
6285

6386
if neighbours < 2:
6487
# Denote the new path
65-
grid[wall[0]][wall[1]].draw_path()
88+
grid[row][col].draw_path()
6689

6790
# Mark the new walls
6891
walls, grid = right_wall(wall, walls, grid)
@@ -75,41 +98,45 @@ def algorithm(grid, draw):
7598

7699

77100
def top_wall(wall, walls, grid):
78-
if wall[0] != 0:
79-
if not grid[wall[0] - 1][wall[1]].is_path():
80-
grid[wall[0] - 1][wall[1]].place_wall()
81-
if [wall[0] - 1, wall[1]] not in walls:
82-
walls.append([wall[0] - 1, wall[1]])
101+
row, col = wall
102+
if row != 0:
103+
if not grid[row - 1][col].is_path():
104+
grid[row - 1][col].place_wall()
105+
if [row - 1, col] not in walls:
106+
walls.append([row - 1, col])
83107

84108
return walls, grid
85109

86110

87111
def left_wall(wall, walls, grid):
88-
if wall[1] != 0:
89-
if not grid[wall[0]][wall[1] - 1].is_path():
90-
grid[wall[0]][wall[1] - 1].place_wall()
91-
if [wall[0], wall[1] - 1] not in walls:
92-
walls.append([wall[0], wall[1] - 1])
112+
row, col = wall
113+
if col != 0:
114+
if not grid[row][col - 1].is_path():
115+
grid[row][col - 1].place_wall()
116+
if [row, col - 1] not in walls:
117+
walls.append([row, col - 1])
93118

94119
return walls, grid
95120

96121

97122
def bottom_wall(wall, walls, grid):
98-
if wall[0] != constants.rows - 1:
99-
if not grid[wall[0] + 1][wall[1]].is_path():
100-
grid[wall[0] + 1][wall[1]].place_wall()
101-
if [wall[0] + 1, wall[1]] not in walls:
102-
walls.append([wall[0] + 1, wall[1]])
123+
row, col = wall
124+
if row != constants.rows - 1:
125+
if not grid[row + 1][col].is_path():
126+
grid[row + 1][col].place_wall()
127+
if [row + 1, col] not in walls:
128+
walls.append([row + 1, col])
103129

104130
return walls, grid
105131

106132

107133
def right_wall(wall, walls, grid):
108-
if wall[1] != constants.cols - 1:
109-
if not grid[wall[0]][wall[1] + 1].is_path():
110-
grid[wall[0]][wall[1] + 1].place_wall()
111-
if [wall[0], wall[1] + 1] not in walls:
112-
walls.append([wall[0], wall[1] + 1])
134+
row, col = wall
135+
if col != constants.cols - 1:
136+
if not grid[row][col + 1].is_path():
137+
grid[row][col + 1].place_wall()
138+
if [row, col + 1] not in walls:
139+
walls.append([row, col + 1])
113140

114141
return walls, grid
115142

@@ -186,13 +213,22 @@ def finish_path(grid, draw):
186213

187214
def get_path_neighbours(node, grid):
188215
neighbours = 0
189-
if grid[node[0] - 1][node[1]].is_path():
216+
row, col = node
217+
218+
# Up
219+
if grid[row - 1][col].is_path():
190220
neighbours += 1
191-
if grid[node[0] + 1][node[1]].is_path():
221+
222+
# Down
223+
if grid[row + 1][col].is_path():
192224
neighbours += 1
193-
if grid[node[0]][node[1] - 1].is_path():
225+
226+
# Left
227+
if grid[row][col - 1].is_path():
194228
neighbours += 1
195-
if grid[node[0]][node[1] + 1].is_path():
229+
230+
# Right
231+
if grid[row][col + 1].is_path():
196232
neighbours += 1
197233

198234
return neighbours

constants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
white = [255, 255, 255] # Default Nodes
66
green = [16, 224, 92] # Start Nodes
77
red = [224, 18, 70] # End Nodes
8-
purple = [182, 96, 252] # Path Nodes
8+
purple = [156, 39, 230] # Path Nodes
99
gray = [50, 50, 50] # Wall Nodes
10-
blue1 = [56, 132, 224] # Closed Nodes
11-
blue2 = [60, 166, 232] # Open Nodes
10+
blue1 = [29, 150, 242] # Closed Nodes
11+
blue2 = [25, 190, 250] # Open Nodes

visualizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def start_visualizer():
121121

122122
# Generate a maze
123123
if event.key == pygame.K_g:
124-
start, end, grid = prims.algorithm(grid, lambda: win.draw(grid))
124+
start, end, grid = prims.algorithm(grid, lambda: win.draw(grid), win)
125125

126126
pygame.quit()
127127

window.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from constants import width, height, square_size, blue2, rows
1+
from constants import width, height, square_size, blue2, rows, cols
22
from node import Node
33
import pygame
44

@@ -17,15 +17,15 @@ def __init__(self):
1717
def make_grid(self):
1818
# Make an N by N grid with each index implemented as a node
1919
grid = []
20-
for i in range(width // square_size):
20+
for i in range(rows):
2121
grid.append([])
22-
for j in range(width // square_size):
22+
for j in range(cols):
2323
grid[i].append(Node(i, j))
2424

2525
return grid
2626

2727
def draw_grid(self):
28-
for i in range(rows):
28+
for i in range(rows + 1):
2929
pygame.draw.line(self.win, (0, 0, 0), (i * square_size, 0), (i * square_size, width), 1)
3030
pygame.draw.line(self.win, (0, 0, 0), (0, i * square_size), (width, i * square_size), 1)
3131

0 commit comments

Comments
 (0)