-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknights_travails.rb
61 lines (49 loc) · 1.55 KB
/
knights_travails.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
class Board
attr_accessor :root_node
def initialize
@moves = [[1, 2], [2, 1], [-1, 2], [2, -1], [-2, -1], [-1, -2], [-2, 1], [1, -2]]
@history = []
@root_node = nil
end
def knight_moves(position, destination)
@root_node = Knight.new(position)
queue = [@root_node]
current = queue.shift
@history << current.position
populate_possible_moves(current)
until current.position == destination
current.possible_moves.each do |pmove|
queue << pmove
end
current = queue.shift
@history << current.position
populate_possible_moves(current)
end
path = []
until current.position == @root_node.position
path.unshift(current.position)
current = current.parent
end
path.unshift(@root_node.position)
puts "You made it in #{path.size - 1} moves! Here is your path: "
path.each { |pos| p pos }
end
def populate_possible_moves(knight)
@moves.each do |move|
next unless (move[0] + knight.position[0]).between?(0, 7) && (move[1] + knight.position[1]).between?(0, 7)
unless @history.include?(Knight.new([knight.position[0] + move[0], knight.position[1] + move[1]]).position)
knight.possible_moves << Knight.new([knight.position[0] + move[0], knight.position[1] + move[1]], knight)
end
end
end
end
class Knight
attr_accessor :position, :possible_moves, :parent
def initialize(position, parent = nil)
@position = position
@possible_moves = []
@parent = parent
end
end
board = Board.new
board.knight_moves([3, 3], [4, 3])