-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpawn.rb
51 lines (38 loc) · 1.17 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
require_relative 'piece'
class Pawn < Piece
def initialize(pos, color, board, moved = false)
@direction = color == :black ? 1 : -1
super(pos, color, board, moved)
end
def render
@color == :white ? '♙' : '♟'
end
def moves
moves = []
one_forward = [@pos[0] + @direction, @pos[1]]
if is_on_board?(one_forward) && @board[one_forward].nil? # TODO this should call the board
moves << one_forward
end
# this is kind of a hack but it works given standard chess rules
two_forward = [@pos[0] + @direction * 2, @pos[1]]
if !(@moved) && moves.size == 1 # kludge. also don't need to check if on board
moves << two_forward unless @board[two_forward]
end
captures = [[@pos[0] + @direction, @pos[1] - 1],
[@pos[0] + @direction, @pos[1] + 1]]
captures.select! do |capture|
is_on_board?(capture) &&
@board[capture] &&
@board[capture].enemy?(@color)
end
moves.concat(captures)
moves
end
def can_promote?
(@color == :black && @pos[0] == 7) ||
(@color == :white && @pos[0] == 0)
end
def promote(new_piece)
@board[@pos] = new_piece.new(@pos, @color, @board)
end
end