-
Notifications
You must be signed in to change notification settings - Fork 0
/
knigth_move.rb
62 lines (48 loc) · 1.14 KB
/
knigth_move.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
class Knight < Struct.new(:position)
VECTORS = [[-2,1],[-2,-1],[2,1],[2,-1],[1,-2],[1,2],[-1,-2],[-1,2]]
def to_s
moves.join(' ')
end
def moves
vectors.
map { |vector| coordinates + vector }.
select { |coords| coords.valid? }.
sort
end
private
def coordinates
@coordinates ||= Coordinates.new_from_position(position)
end
def vectors
@vectors ||= VECTORS.map { |vx, vy| Coordinates.new(vx, vy)}
end
class Coordinates < Struct.new(:x, :y)
LETTERS = '_abcdefgh'
VALID_COORDINATE = 1..8
def self.new_from_position(position)
self.new(LETTERS.index(position[0]), position[1].to_i)
end
def +(coordinates)
self.class.new(x + coordinates.x, y + coordinates.y)
end
def valid?
VALID_COORDINATE.include?(x) &&
VALID_COORDINATE.include?(y)
end
def <=>(other)
[x, y] <=> [other.x, other.y]
end
def to_s
valid? ?
"#{x_position}#{y}" :
"invalid coordinates"
end
private
def x_position
LETTERS[x]
end
end
end
File.open('knigth.txt').each_line do |line|
puts Knight.new(line.chomp)
end