class JustBackgammon::OffBoard

OffBoard

The off board is where pieces go when bearing off. Contains an array of pieces.

Attributes

pieces[R]

@return [Array<Piece>] all the pieces on the bar, each piece has an player_number

Public Class Methods

new(pieces:) click to toggle source

A new instance of OffBoard.

@param [Array<Hash>] pieces

All the pieces off board, each piece has an player_number.

Example:

# Instantiates a new OffBoard
JustBackgammon::OffBoard.new({
  pieces: [{player_number: 1}, {player_number: 1}]
})
# File lib/just_backgammon/off_board.rb, line 21
def initialize(pieces:)
  @pieces = Piece.load(pieces)
end

Public Instance Methods

as_json() click to toggle source

A hashed serialized representation of off board.

@return [Hash]

# File lib/just_backgammon/off_board.rb, line 72
def as_json
  { pieces: pieces.map(&:as_json) }
end
hittable?(_=nil) click to toggle source
# File lib/just_backgammon/off_board.rb, line 65
def hittable?(_=nil)
  false
end
number() click to toggle source

The identifier of the bar, returns the string 'bar'.

@return [String]

# File lib/just_backgammon/off_board.rb, line 31
def number
  'off_board'
end
number_of_pieces_owned_by_player(player_number) click to toggle source

Number of pieces owned by the specified player.

@param [Fixnum] player_number

the specified player number.

@return [Fixnum]

# File lib/just_backgammon/off_board.rb, line 51
def number_of_pieces_owned_by_player(player_number)
  pieces_owned_by_player(player_number).size
end
pieces_owned_by_player(player_number) click to toggle source

ALl the pieces owned by the specified player.

@param [Fixnum] player_number

the specified player number.

@return [Array<Piece>]

# File lib/just_backgammon/off_board.rb, line 41
def pieces_owned_by_player(player_number)
  pieces.select { |p| p.player_number == player_number }
end
push(piece) click to toggle source

Adds a piece off board.

@param [Piece] piece

the piece to push off board.

@return [Array<Piece>]

# File lib/just_backgammon/off_board.rb, line 61
def push(piece)
  @pieces.push(piece)
end