-
Notifications
You must be signed in to change notification settings - Fork 0
/
knight.rb
66 lines (49 loc) · 1.1 KB
/
knight.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
class Knight
VECTORS = [
[2, -1], [2, 1], [-2, -1], [-2, 1],
[1, -2], [1, 2], [-1, -2], [-1, 2]
]
attr_accessor :coordinates_x, :coordinates_y
def initialize(coordinates)
@coordinates_x = coordinates[0]
@coordinates_y = coordinates[1]
end
def cord_x
Cord.new.cord_x.index(@coordinates_x)
end
def cord_y
Cord.new.cord_y.index(@coordinates_y.to_s)
end
def to_s
"#{view}"
end
private
def view
convert_position.join(' ')
end
def posible
VECTORS.map{|x,y| [x + self.cord_x, y + self.cord_y] }
end
def real_posible
posible.select {|x,y| (1..8).include?(x) && (1..8).include?(y) }
end
def convert_position
real_posible.map {|x,y| "#{Cord.new.cord_x[x]}#{Cord.new.cord_y[y]}"}.sort
end
class Cord
CORD_X = "_abcdefgh"
CORD_Y = "_12345678"
attr_writer :cord_x, :cord_y
def inititialize(cord_x,cord_y)
@cord_x = cord_x
@cord_y = cord_y
end
def cord_x
self.cord_x = CORD_X
end
def cord_y
self.cord_y = CORD_Y
end
end
end
puts Knight.new("g2")