-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkruskals.py
190 lines (170 loc) · 6.37 KB
/
kruskals.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import random, cells
from bearlibterminal import terminal
class Kruskals_Algorithm:
"""
Class used to perform Kruskals Algorithm on an arr using randomly assigned
edge weights
"""
def __init__(self, arr):
"""
Initialize empty lists for nodes, edges and connections
arr -- the array of cells to perform Kruskals on
"""
self.arr = arr
self.nodes = []
self.edges = []
self.connections = []
def set_nodes(self):
"""
Create evenly spaced nodes from the 2d dungeon arr
"""
# Each node has a region ID, used later for checking region connectivity
region_id = 1
# For loops use every other x/y
for y in range(1, len(self.arr), 2):
for x in range(1, len(self.arr[y]), 2):
# If x/y cell is not empty (NoneType)
if self.arr[y][x] == 0:
# Add the node to the nodes list and increment region ID
node = Node(region_id, x, y)
self.arr[y][x] = node
self.nodes.append(node)
region_id += 1
def set_edges(self):
"""
Create edges between nodes
"""
# Only node points to the right and below are checked as starting from
# the top left of the arr, all potential edge points will be considered
for y in range(1, len(self.arr), 2):
for x in range(1, len(self.arr[y]), 2):
# If point is a node and potential node point to the right is
# also a node, add an edge
if self.is_node(x, y) and self.is_node(x + 2, y):
edge = Edge(x + 1, y, "h")
self.edges.append(edge)
# If point is a node and potential node point below is also a
# node, add an edge
if self.is_node(x, y) and self.is_node(x, y + 2):
edge = Edge(x, y + 1, "v")
self.edges.append(edge)
def is_node(self, x, y):
"""
Return boolean for whether a point is a node
x -- x coordinate of point to be checked
y -- y coordinate of point to be checked
"""
# If x and y coordinates are valid
if x in range(len(self.arr[0])) and y in range(len(self.arr)):
if self.arr[y][x] != None and self.arr[y][x] != 0 and self.arr[y][x].type == "node":
return True
return False
def is_edge(self, x, y):
"""
Return boolean for whether a point is an edge
x -- x coordinate of point to be checked
y -- y coordinate of point to be checked
"""
# If x and y coordinates are valid
if x in range(len(self.arr[0])) and y in range(len(self.arr)):
if self.arr[y][x] != None and self.arr[y][x] != 0 and self.arr[y][x].type == "edge":
return True
return False
def update_region(self, old_id, new_id):
"""
Update nodes and edges with a new region ID
old_id -- Region ID to be replaced
new_id -- ID to be replaced with
"""
for node in self.nodes:
# If node in region being updated, update it
if node.id == old_id:
x, y = node.x, node.y
self.arr[y][x].id = new_id
for connection in self.connections:
# If edge in region being updated, update it
if connection.id == old_id:
x, y = connection.x, connection.y
self.arr[y][x].id = new_id
def add_connecting_edge(self, edge):
"""
Add a potential edge point as a connection in arr
edge -- the edge to be added as a connection
"""
x, y = edge.x, edge.y
self.arr[y][x] = edge
self.connections.append(edge)
def step(self):
"""
Run a step of the algorithm
"""
# Pick a random edge (has the effect of using randomly assigned edge weights)
edge = random.choice(self.edges)
self.edges.remove(edge)
x, y = edge.x, edge.y
# If edge is vertical
if edge.dir == "v":
node1 = self.arr[y-1][x]
node2 = self.arr[y+1][x]
# If edge is horizontal
elif edge.dir == "h":
node1 = self.arr[y][x-1]
node2 = self.arr[y][x+1]
# If nodes are not already connected (if they have different region ids)
if node1.id != node2.id:
# Add the edge as a connection and update the region IDs so that they match
edge.id = node1.id
self.add_connecting_edge(edge)
self.update_region(node2.id, node1.id)
def set_regions(self):
"""
Convert from node/edge based arr to ID based array
"""
# For each point in the 2d arr
for y in range(len(self.arr)):
for x in range(len(self.arr[y])):
# If its not None and not 0 then it must be an edge or node
if self.arr[y][x] is not None and self.arr[y][x] != 0:
# Therefore set it to the node/edge region ID
self.arr[y][x] = self.arr[y][x].id
def gen_maze(self):
"""
Generate perfect mazes using Kruskals
"""
self.set_nodes()
self.set_edges()
# Loop until all potential edges have been considered
while len(self.edges) > 0:
self.step()
self.set_regions()
class Node:
"""
Class used to represent a node on a graph
"""
def __init__(self, region_id, x, y):
"""
Initialize node object
region_id -- the ID of the region the node belongs to
x -- the x coordinate of the node within a 2d array
y -- the y coordinate of the node within a 2d array
"""
self.type = "node"
self.id = region_id
self.x = x
self.y = y
class Edge:
"""
Class used to represent an edge between two nodes
"""
def __init__(self, x, y, direction):
"""
Initialize edge object
x -- the x coordinate of the edge within a 2d array
y -- the y coordinate of the edge within a 2d array
direction -- string ("v" or "h") representing the direction of the connection
"""
self.type = "edge"
self.x = x
self.y = y
self.id = None
self.dir = direction