Class: JustChess::Pawn
- 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
Instance Method Summary collapse
-
#capture_squares(square, game_state) ⇒ SquareSet
All the squares that the piece can capture.
-
#destinations(square, game_state) ⇒ SquareSet
All the squares that the piece can move to and/or capture.
-
#en_passant_square(square, game_state) ⇒ Square
The Square that a pawn can capture en passant.
-
#move_squares(square, game_state) ⇒ SquareSet
All the squares that the piece can move to.
-
#potential_capture_squares(square, game_state) ⇒ SquareSet
All the squares that the piece could potentially capture.
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.
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.
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.
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.
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.)
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 |