class JustBackgammon::Bar
Bar
¶ ↑
The bar is where hit pieces go. Contains an array of pieces.
Attributes
@return [Array<Piece>] all the pieces on the bar, each piece has an owner
Public Class Methods
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
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
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
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
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 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
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
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
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