Class: Chess::Pawn

Inherits:
BoardgameEngine::Piece show all
Defined in:
lib/boardgame_engine/chess.rb

Instance Attribute Summary

Attributes inherited from BoardgameEngine::Piece

#owner, #status

Instance Method Summary collapse

Methods inherited from BoardgameEngine::Piece

#kill, #to_s

Constructor Details

#initialize(owner, front) ⇒ Pawn

Returns a new instance of Pawn.



84
85
86
87
88
# File 'lib/boardgame_engine/chess.rb', line 84

def initialize(owner, front)
  super(owner, 'p')
  @first_move = true
  @front = front
end

Instance Method Details

#valid_move?(start_location, end_location, board) ⇒ Boolean

Check whether the intended destination is a valid destination

Parameters:

  • start_location (Array<Integer, Integer>)

    the start coords

  • end_location (Array<Integer, Integer>)

    the intended destination

  • board (ChessBoard)

    the chess board

Returns:

  • (Boolean)

    whether the pawn can move to the intended destination



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/boardgame_engine/chess.rb', line 97

def valid_move?(start_location, end_location, board)
  row, col = start_location
  end_row, end_col = end_location

  # Checks if moving 1 (or 2 if its the first move) cell forward
  return false unless valid_forward_move?(row, end_row)

  if col == end_col
    valid_line_move?(end_row, end_col, board)
  elsif (col - end_col).abs == 1 && (row + @front == end_row)
    valid_diag_move?(end_row, end_col, board)
  else
    false
  end
end