-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgrid.rb
87 lines (64 loc) · 1.78 KB
/
grid.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
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
class Grid
attr_accessor :visited, :edges, :size
def initialize(size:)
@size = size
@visited = {}
init_grid
end
def visit(point)
raise "Point #{point} is not a valid point" unless point.is_a?(Hash) && point.key?(:row) && point.key?(:col)
visited[point] = true
end
def all_visited?
visited.select { |_, value| value }.length == size * size
end
def is_valid_move?(from:, to:)
edges[from][to] != nil
end
def move_cost(from:, to:)
raise 'Invalid move' unless is_valid_move?(from: from, to: to)
edges[from][to]
end
def max_col
size - 1
end
def max_row
size - 1
end
private
attr_accessor :size
def init_grid
@edges = {}
moves = {
-1 => [0],
0 => [-1, 1],
1 => [0]
}
(0..size - 1).each do |row|
(0..size - 1).each do |col|
from = {row: row, col: col}
unless edges[from]
edges[from] = {}
edges[from][from] = 0
visited[from] = false
end
moves.each_key do |x_direction|
moves[x_direction].each do |y_direction|
neighbour_x = row + x_direction
neighbour_y = col + y_direction
next unless neighbour_x.between?(0, size - 1) && neighbour_y.between?(0, size - 1)
to = {row: neighbour_x, col: neighbour_y}
next if edges.key?(from) && edges[from].key?(to)
next if edges.key?(to) && edges[to].key?(from)
weight = rand(1..10)
edges[from] = {} unless edges[from]
edges[from][to] = weight
edges[to] = {} unless edges[to]
edges[to][from] = weight
puts "Building edge (#{row}, #{col}) -> (#{neighbour_x}, #{neighbour_y}) = #{weight}"
end
end
end
end
end
end