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

id[R]

@return [Fixnum] the identifier of the piece.

player_number[R]

@return [Fixnum] the owner of the piece.

Public Class Methods

new(id: , player_number: , type: nil) click to toggle source
# 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

as_json() click to toggle source

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_move?(from, to, game_state) click to toggle source

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
capture_squares(squares, game_state) click to toggle source

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
destinations(square, game_state) click to toggle source

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
forwards_direction() click to toggle source

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
move_squares(square, game_state) click to toggle source

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
opponent() click to toggle source

The opposing player number

@return [Fixnum]

# File lib/board_game_grid/piece.rb, line 24
def opponent
  player_number == 1 ? 2 : 1
end
potential_capture_squares(square, game_state) click to toggle source

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
type() click to toggle source

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