-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive_backtracker.py
More file actions
66 lines (59 loc) · 1.9 KB
/
Copy pathrecursive_backtracker.py
File metadata and controls
66 lines (59 loc) · 1.9 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
64
65
66
import pygame
import sys
import random
from maze_generator import draw_maze, units
from utility_methods import update_display
def get_neighbors(cell):
neighbors = []
x, y = cell.x, cell.y
if not units[x - 1][y].visited: # up
neighbors.append('U')
if not units[x][y - 1].visited: # left
neighbors.append('L')
if not units[x][y + 1].visited: # right
neighbors.append('R')
if not units[x + 1][y].visited: # down
neighbors.append('D')
return neighbors
def recursive_backtrack():
stack = []
current = units[1][1]
current.visited = True
current.current = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
neighbors = get_neighbors(current)
# destory wall
if neighbors:
next = random.choice(neighbors) # direction
if next == 'U':
units[current.x - 1][current.y].bottom = False
next_cell = units[current.x - 1][current.y]
elif next == 'L':
units[current.x][current.y - 1].right = False
next_cell = units[current.x][current.y - 1]
elif next == 'D':
current.bottom = False
next_cell = units[current.x + 1][current.y]
elif next == 'R':
current.right = False
next_cell = units[current.x][current.y + 1]
stack.append(current)
current.current = False
current = next_cell
current.visited = True
current.current = True
elif stack:
current.current = False
current = stack.pop()
current.current = True
else:
break
draw_maze()
update_display(0)
current.current = False
draw_maze()
update_display(0)