class BoardGameGrid::Piece
Piece
¶ ↑
A piece that can move on a board
Constants
- FORWARDS_DIRECTION
The forwards direciton of the piece for each player
Attributes
@return [Fixnum] the identifier of the piece.
@return [Fixnum] the owner of the piece.
Public Class Methods
# File lib/board_game_grid/piece.rb, line 10 def initialize(id: , player_number: , type: nil) @id = id @player_number = player_number end
Public Instance Methods
returns a serialized piece as a hash
@return [Hash]
# File lib/board_game_grid/piece.rb, line 106 def as_json { id: id, player_number: player_number, type: type } end
Can the piece move from a square, to a square, given the game state?
@param [Square] from
the origin square.
@param [Square] to
the destination square.
@param [GameState] game_state
the current game state.
@return [Boolean]
# File lib/board_game_grid/piece.rb, line 47 def can_move?(from, to, game_state) destinations(from, game_state).include?(to) end
All the squares that the piece can capture.
@param [Square] square
the origin square.
@param [GameState] game_state
the current game state.
@return [SquareSet]
# File lib/board_game_grid/piece.rb, line 86 def capture_squares(squares, game_state) destinations(squares, game_state) end
All the squares that the piece can move to and/or capture.
@param [Square] square
the origin square.
@param [GameState] game_state
the current game state.
@return [SquareSet]
# File lib/board_game_grid/piece.rb, line 60 def destinations(square, game_state) [] end
returns the direction the piece moves
player 1 moves up (-1) player 2 moves down (1)
@return [Fixnum]
# File lib/board_game_grid/piece.rb, line 120 def forwards_direction FORWARDS_DIRECTION[player_number] end
All the squares that the piece can move to.
@param [Square] square
the origin square.
@param [GameState] game_state
the current game state.
@return [SquareSet]
# File lib/board_game_grid/piece.rb, line 73 def move_squares(square, game_state) destinations(square, game_state) end
The opposing player number
@return [Fixnum]
# File lib/board_game_grid/piece.rb, line 24 def opponent player_number == 1 ? 2 : 1 end
All the squares that the piece could potentially capture. (i.e. if a piece was there)
@param [Square] square
the origin square.
@param [GameState] game_state
the current game state.
@return [SquareSet]
# File lib/board_game_grid/piece.rb, line 99 def potential_capture_squares(square, game_state) capture_squares(square, game_state) end
The stringified identifier of the piece type.
@return [Fixnum]
# File lib/board_game_grid/piece.rb, line 31 def type self.class.to_s.split('::').last.downcase end