Class: JustChess::Pawn

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

Overview

Pawn

The piece that moves 1 forward or 2 forwards the first time, or 1 diagonal to capture.

Constant Summary collapse

FORWARDS_DIRECTION =

The forwards direction of the pawn for each player.

{ 1 => -1, 2 => 1 }
STARTING_RANK =

THe starting rank of the pawn for each player

{ 1 => 6, 2 => 1 }

Instance Attribute Summary

Attributes inherited from Piece

#has_moved

Instance Method Summary collapse

Methods inherited from Piece

#as_json, #has_not_moved?, #initialize, #moved

Constructor Details

This class inherits a constructor from JustChess::Piece

Instance Method Details

#capture_squares(square, game_state) ⇒ SquareSet

All the squares that the piece can capture.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



60
61
62
# File 'lib/just_chess/pieces/pawn.rb', line 60

def capture_squares(square, game_state)
  potential_capture_squares(square, game_state).occupied_by_opponent(player_number)
end

#destinations(square, game_state) ⇒ SquareSet

All the squares that the piece can move to and/or capture.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/just_chess/pieces/pawn.rb', line 26

def destinations(square, game_state)
  moves = move_squares(square, game_state)
  captures = capture_squares(square, game_state)
  en_passant = en_passant_square(square, game_state)

  if en_passant
    (moves + captures) << en_passant
  else
    moves + captures
  end
end

#en_passant_square(square, game_state) ⇒ Square

The Square that a pawn can capture en passant.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/just_chess/pieces/pawn.rb', line 86

def en_passant_square(square, game_state)
  if square.rank_number(player_number) == 5 && game_state.last_double_step_pawn_id
    double_step = game_state.squares.find_by_piece_id(game_state.last_double_step_pawn_id)
    vector = BoardGameGrid::Vector.new(square, double_step)
    if vector.magnitude.abs == 1
      x = double_step.x
      y = square.y + forwards_direction
      game_state.squares.find_by_x_and_y(x, y)
    else
      nil
    end
  else
    nil
  end
end

#move_squares(square, game_state) ⇒ SquareSet

All the squares that the piece can move to.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



47
48
49
# File 'lib/just_chess/pieces/pawn.rb', line 47

def move_squares(square, game_state)
  game_state.squares.in_range(square, range(square)).in_direction(square, forwards_direction).orthogonal(square).unoccupied.unblocked(square, game_state.squares)
end

#potential_capture_squares(square, game_state) ⇒ SquareSet

All the squares that the piece could potentially capture. (i.e. even if a piece wasn’t on the square.)

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



73
74
75
# File 'lib/just_chess/pieces/pawn.rb', line 73

def potential_capture_squares(square, game_state)
  game_state.squares.in_range(square, 1).in_direction(square, forwards_direction).diagonal(square)
end