-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkruskal.py
62 lines (46 loc) · 1.53 KB
/
kruskal.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
""" Helper module to implement maze generation using kruskal's algorithm
"""
import random
import disjoint_set as ds
class Cell:
def __init__(self):
self.top = True
self.bottom = True
self.left = True
self.right = True
def init_variables(rows, cols):
cell_list = []
for i in xrange(rows):
for j in xrange(cols):
cell_list.append((i, j))
cell_set = ds.disjoint_set(cell_list)
cells = [[ Cell()
for y in range(cols) ]
for x in range(rows) ]
# print cells[2][0]
# print cells[1][0]
edges = []
for x in xrange(rows):
for y in xrange(cols):
edges.append((x, y, 'L'))
edges.append((x, y, 'U'))
return (edges, cell_set, cell_list, cells)
""" Function that takes partially formed cells array and performs one step on it"""
def generate_maze(edges, cell_set, cell_list, cells):
# choose random wall
wall = random.choice(edges)
edges.remove(wall)
x = wall[0]
y = wall[1]
if y > 0 and wall[2] == 'L' and cell_set.find((x, y)) != cell_set.find((x, y-1)):
cell_set.union((x, y), (x, y-1))
cells[x][y].left = False
cells[x][y-1].right = False
if x > 0 and wall[2] == 'U' and cell_set.find((x, y)) != cell_set.find((x-1, y)):
cell_set.union((x, y), (x-1, y))
cells[x][y].top = False
cells[x-1][y].bottom = False
# if y == 0 and wall[2] == 'L' or x == 0 and wall[2] == 'U':
# edges.remove(wall)
return cells
# cells = generate_maze(3, 3)