Class: Pawn

Inherits:
Piece show all
Defined in:
lib/pieces/pawn.rb

Constant Summary collapse

NORMAL_MOVE =
[1, 0]
INITIAL_MOVE =
[2, 0]
ATTACK_MOVES =
[[1, 1], [1, -1]]

Instance Attribute Summary

Attributes inherited from Piece

#color, #moved, #pos

Instance Method Summary collapse

Methods inherited from Piece

#dup, #in_range_of_enemy?, #initialize, #move_into_check?, #sum_positions, #valid_moves

Constructor Details

This class inherits a constructor from Piece

Instance Method Details

#movesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pieces/pawn.rb', line 14

def moves
  moves = []
  modifier = self.color == :white ? 1 : -1

  normal_move = sum_positions(NORMAL_MOVE.map { |move| move * modifier }, @pos)
  unless @board.occupied?(normal_move)
    moves << normal_move
  end

  initial_move = sum_positions(INITIAL_MOVE.map { |move| move * modifier }, @pos)
  if !@moved && moves.include?(normal_move) && !@board.occupied?(initial_move)
    moves << initial_move
  end

  ATTACK_MOVES.each do |attack_move|
    potential_pos = sum_positions(@pos.dup, attack_move.map { |move| move * modifier })
    if @board.occupied?(potential_pos) && @board.piece_at(potential_pos).color != @color
      moves << potential_pos
    end
  end

  moves.select { |move| @board.on_board?(move)}
end

#symbolObject



10
11
12
# File 'lib/pieces/pawn.rb', line 10

def symbol
  '♟'
end

#valueObject



38
39
40
# File 'lib/pieces/pawn.rb', line 38

def value
  1
end