-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
170 lines (146 loc) · 3.59 KB
/
main.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
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
# frozen_string_literal: true
class Node
attr_accessor :data, :neighbors, :visited
def initialize(data)
@data = data
@neighbors = []
@visited = false
end
def add_edge(neighbor)
@neighbors << neighbor
end
end
class Graph
attr_accessor :nodes, :path, :destination_square
def initialize
@nodes = []
@destination_square = nil
@path = []
end
def add_node(value)
@nodes << Node.new(value)
end
def get_node(data)
@nodes.each do |n|
return n if data == n.data
end
end
def get_idx(data)
@nodes.each_with_index do |n, idx|
return idx if data == n.data
end
end
def get_edge(data)
start = @nodes[get_idx(data)]
start.neighbors[0].data
end
def traverse_bfs(starting_square, destination_square)
@destination_square = destination_square
moves = 0
edge = get_edge(starting_square)
until @path.include?(edge) # until path include an edge of starting square
root = @nodes[get_idx(starting_square)]
queue = []
queue << root
path.unshift(@destination_square)
@nodes.each do |node|
node.visited = false
end
bfs(queue)
moves += 1
end
puts "You made it in #{moves} moves!"
p starting_square
@path.each { |a| p a }
@path = []
end
def bfs(queue)
array = []
until queue.empty?
current = queue[0]
unless current.visited
current.visited = true
current.neighbors.each do |neighbor|
queue << neighbor unless neighbor.visited
end
array << current.data
current.neighbors.each do |neighbor|
if neighbor.data == @destination_square
@destination_square = array[-1]
queue = []
end
end
end
queue = queue[1..] unless queue.empty?
end
end
end
# board
class Board
def initialize
@board = Array.new(8) { [*0..7] }
@knight = Knight.new(self)
@adjacency_list = Graph.new
end
attr_accessor :board, :array, :adjacency_list
def knight_moves(starting_square, destination_square)
return unless valid_input?(starting_square) && valid_input?(destination_square)
build
@adjacency_list.traverse_bfs(starting_square, destination_square)
end
def build
add_nodes
add_edges
end
def add_nodes
@board.each_with_index do |row, idx|
row.each do |col|
@adjacency_list.add_node([idx, col])
end
end
end
def add_edges
@adjacency_list.nodes.each do |node|
arr_data = @knight.adjacent(node.data[0], node.data[1])
arr_data.each do |data|
neigh = @adjacency_list.get_node(data) # get neighbor
node.add_edge(neigh)
end
end
end
def display_board
board = @board
board.each_with_index do |row, idx|
print "row #{idx}: #{row}\n"
end
end
def valid_input?(coord)
return true if (0..7).include?(coord[0]) && (0..7).include?(coord[1])
end
end
# Knight
class Knight
def initialize(game)
@game = game
end
def adjacent(row, col, count = 0, array = [])
steps_row = [-2, -2, -1, -1, +2, +2, +1, +1]
steps_col = [-1, +1, -2, +2, -1, +1, -2, +2]
8.times do
new_row = steps_row[count] + row
new_col = steps_col[count] + col
array << [new_row, new_col] if valid_square?(new_row, new_col)
count += 1
end
array
end
def valid_square?(row, col)
true if (0..7).include?(row) && (0..7).include?(col)
end
end
board = Board.new
board.display_board
board.knight_moves([3, 3], [0, 0])
board.knight_moves([0, 0], [3, 3])
board.knight_moves([3, 3], [4, 3])
board.knight_moves([0, 0], [7, 7])