-
Notifications
You must be signed in to change notification settings - Fork 0
/
pawn.rb
38 lines (34 loc) · 907 Bytes
/
pawn.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
class Pawn < SteppingPiece
def move_deltas
dy = color == :black ? 1 : -1
[ [dy, 0], [dy, 1], [dy, -1] ]
end
def possible_moves
moves = first_move
super.each do |move|
if move.last == @position.last && @board.empty?(move)
moves << move
elsif move.last != @position.last
next if @board.empty?(move)
moves << move if @board.capturable?(move, @color)
end
end
moves
end
def first_move
starting_row = color == :white ? 6 : 1
current_row = @position.first
move = []
if current_row == starting_row
x = @position.first + move_deltas.first[0]
y = @position.last
one_step = [x, y]
delta = color == :white ? -2 : 2
second_step = [current_row + delta, y]
if @board.empty?(second_step) && @board.empty?(one_step)
move << second_step
end
end
move
end
end