Source

puzzle_nodes / 13 / lib / piece / pawn.rb

Full commit
class Piece::Pawn
 attr_reader :color, :location

  def initialize color, location, board
    @color, @location, @board = color, location, board
  end

  # Returns an array with the path from location to destination or an empty
  # array if none exists.
  def move destination
  end

  # Pawns can move forward one row (where forward depends on color).
  # They can also move forward two rows on their first move.
  # The can also capture diagonally forward left or right.
  # All moves must keep the piece on the chess board so filter out values not
  # between 0 and 63.
  def destinations
    [].tap do |dest|
      dest << forward_one if @board.empty? forward_one
      dest << forward_two if first_move? and @board.empty? forward_two
      dest << diagonal_left if @board.capturable? color, diagonal_left
      dest << diagonal_right if @board.capturable? color, diagonal_right
    end.select{ |location| location >= 0 && location <= 63 }
  end

  def forward_one
    if color == 'b'
      location + 8 * direction
    else
      location + 8 * direction
    end
  end

  def diagonal_left

  end

  def diagonal_right

  end

  def forward_two

  end

  def direction
    if color == 'b'
      1
    else
      -1
    end
  end

  def first_move?
    return true if color == 'b' and (8 - location / 8) == 7
    return true if color == 'w' and (8 - location / 8) == 2
    false
  end
end