-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.rb
executable file
·64 lines (47 loc) · 1.13 KB
/
slider.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
# encoding: utf-8
require_relative 'piece'
class Slider < Piece
def moves
@vectors.reduce([]) { |moves, vector| moves += slide(vector) }
end
def slide(vector)
moves = []
pos = vector_add(@pos, vector) # step once in the direction of vector
while @board.empty_or_capture?(pos, @color)
moves << pos
break unless @board[pos].nil?
pos = vector_add(pos, vector)
end
moves
end
end
class Bishop < Slider
def initialize(pos, color, board, has_moved = false)
super
@vectors = VECTOR_DIAGO
end
def to_s
return '♗' if @color == :white
return '♝' if @color == :black
end
end
class Rook < Slider
def initialize(pos, color, board, has_moved = false)
super(pos, color, board)
@vectors = VECTOR_ORTHO
end
def to_s
return '♖' if @color == :white
return '♜' if @color == :black
end
end
class Queen < Slider
def initialize(pos, color, board, has_moved = false)
super(pos, color, board)
@vectors = VECTOR_DIAGO + VECTOR_ORTHO
end
def to_s
return '♕' if @color == :white
return '♛' if @color == :black
end
end