-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliding_piece.rb
70 lines (57 loc) · 1.25 KB
/
sliding_piece.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
require_relative 'piece.rb'
require 'byebug'
class SlidingPiece < Piece
DIRS = {
:diagonal => [[-1, -1], [-1, 1], [1, -1], [1, 1]],
:orthogonal => [[1, 0], [0, 1], [-1, 0], [0, -1]]
}
def initialize(color, pos, board)
super
end
def moves
all_possible_moves = []
move_dirs.each do |dir|
DIRS[dir].each do |offset|
i = 1
x = @pos[0] + offset[0]
y = @pos[1] + offset[1]
while @board.on_board?([x, y])
break if my_teammate([x, y])
all_possible_moves << [x, y]
break if other_team([x, y])
i += 1
x = @pos[0] + (offset[0] * i)
y = @pos[1] + (offset[1] * i)
end
end
end
all_possible_moves
end
end
class Bishop < SlidingPiece
def initialize(color, pos, board)
super
@picture = color == "black" ? "♝" : "♗"
end
def move_dirs
[:diagonal]
end
end
class Rook < SlidingPiece
def initialize(color, pos, board)
super
@picture = color == "black" ? "♜" : "♖"
end
def move_dirs
[:orthogonal]
end
end
class Queen < SlidingPiece
def initialize(color, pos, board)
super
@picture = color == "black" ? "♛" : "♕"
end
def move_dirs
[:orthogonal, :diagonal]
end
end