class JustBackgammon::Bar

Bar

The bar is where hit pieces go. Contains an array of pieces.

Attributes

pieces[R]

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

Public Class Methods

new(pieces:) click to toggle source

A new instance of Bar.

@param [Array<Hash>] pieces

All the pieces on the bar, each piece has an owner.

Example:

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

Public Instance Methods

any_pieces_for_player?(player_number) click to toggle source

If the player has any pieces.

@param [Fixnum] player_number

the specified player number.

@return [Boolean]

# File lib/just_backgammon/bar.rb, line 66
def any_pieces_for_player?(player_number)
  pieces_owned_by_player(player_number).any?
end
as_json() click to toggle source

A hashed serialized representation of the bar.

@return [Hash]

# File lib/just_backgammon/bar.rb, line 104
def as_json
  { pieces: pieces.map(&:as_json) }
end
empty_for_player?(player_number) click to toggle source

If the player has no pieces.

@param [Fixnum] player_number

the specified player number.

@return [Boolean]

# File lib/just_backgammon/bar.rb, line 76
def empty_for_player?(player_number)
  pieces_owned_by_player(player_number).none?
end
number() click to toggle source

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

@return [String]

# File lib/just_backgammon/bar.rb, line 36
def number
  'bar'
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/bar.rb, line 56
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/bar.rb, line 46
def pieces_owned_by_player(player_number)
  pieces.select { |p| p.player_number == player_number }
end
pop(player_number) click to toggle source

Removes a piece owned by the specified player and return it.

@param [Fixnum] player_number

the specified player number.

@return [Piece,NilClass]

# File lib/just_backgammon/bar.rb, line 86
def pop(player_number)
  p = pieces.find { |p| p.player_number == player_number }
  pieces.delete(p)
end
push(piece) click to toggle source

Adds a piece to the bar.

@param [Piece] piece

the piece to push onto the bar.

@return [Array<Piece>]

# File lib/just_backgammon/bar.rb, line 97
def push(piece)
  pieces.push(piece)
end