-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims_algorithm.py
More file actions
63 lines (53 loc) · 2.08 KB
/
Copy pathprims_algorithm.py
File metadata and controls
63 lines (53 loc) · 2.08 KB
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
import pygame
import sys
import random
from maze_generator import draw_maze, units
from utility_methods import update_display
def get_walls(cell):
valid_walls = []
if cell.right and not (units[cell.x][cell.y + 1].visited): # Right Wall
valid_walls.append(((cell.x, cell.y), 'R'))
if cell.bottom and not (units[cell.x + 1][cell.y].visited): # Bottom Wall
valid_walls.append(((cell.x, cell.y), 'B'))
if units[cell.x - 1][cell.y].bottom and not (units[cell.x - 1][cell.y].visited): # Top Wall
valid_walls.append(((cell.x, cell.y), 'U'))
if units[cell.x][cell.y - 1].right and not (units[cell.x][cell.y - 1].visited): # Left Wall
valid_walls.append(((cell.x, cell.y), 'L'))
return valid_walls
def prim_generate():
walls = set()
start = units[(int)(len(units)/2)][(int)(len(units)/2)]
start.visited = True
walls.update(get_walls(start))
while walls:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
next_wall = random.choice(list(walls))
x, y = next_wall[0]
direct = next_wall[1]
if direct == 'R':
if not (units[x][y + 1].visited):
units[x][y].right = False
units[x][y + 1].visited = True
new_cell = units[x][y + 1]
elif direct == 'B':
if not (units[x + 1][y].visited):
units[x][y].bottom = False
units[x + 1][y].visited = True
new_cell = units[x + 1][y]
elif direct == 'L':
if not (units[x][y - 1].visited):
units[x][y - 1].right = False
units[x][y - 1].visited = True
new_cell = units[x][y - 1]
elif direct == 'U':
if not (units[x - 1][y].visited):
units[x - 1][y].bottom = False
units[x - 1][y].visited = True
new_cell = units[x - 1][y]
walls.update(get_walls(new_cell))
walls.remove(next_wall)
draw_maze()
update_display(0)