-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_gen.rb
48 lines (39 loc) · 1.12 KB
/
maze_gen.rb
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
Node = Array
Edge = Struct.new :nodes, :weight
Neigborhoods = {
:cubic => [[ 0 , 0, -1],
[ 0, 0, 1],
[ 0, -1, 0],
[ 0, 1, 0],
[-1, 0, 0],
[ 1, 0, 0]],
:hexagonal => [[ 0, 1, -1],
[ 1, 0, -1],
[ 1, -1, 0],
[ 0, -1, 1],
[-1, 0, 1],
[-1, 1, 0]]
}
Neigborhood = Neigborhoods[:cubic]
def neighbors coords
Neigborhood.map {|d| coords.zip(d).map{|c, d| (c + d)}}
end
def priority c1, c2
c1[2] == c2[2] ? 2 : 1
end
nodes = {[0, 0, 1] => Node.new}
edges = neighbors([0, 0, 1]).map{|c| Edge.new [[0, 0, 1], c], rand * priority([0, 0, 1], c)}.sort_by(&:weight)
until edges.empty?
edge_in = edges.pop
from, to = edge_in.nodes
next if nodes[from].size >= 3
next if nodes[to]
next if to[2] < 1
next if edge_in.nodes.last.any? {|c| c.abs > 3}
nodes[edge_in.nodes.last] = Node.new
edge_in.nodes.each{|n| nodes[n] << edge_in}
edges += neighbors(edge_in.nodes.last).map{|c| Edge.new [to, c], rand * priority(to, c) }
edges.sort_by!(&:weight)
p edge_in
gets
end