Class: JustBackgammon::Bar
- Inherits:
-
Object
- Object
- JustBackgammon::Bar
- Extended by:
- Forwardable, Common
- Defined in:
- lib/just_backgammon/bar.rb
Overview
Bar
The bar is where hit pieces go. Contains an array of pieces.
Instance Attribute Summary collapse
-
#pieces ⇒ Array<Piece>
readonly
All the pieces on the bar, each piece has an owner.
Instance Method Summary collapse
-
#any_pieces_for_player?(player_number) ⇒ Boolean
If the player has any pieces.
-
#as_json ⇒ Hash
A hashed serialized representation of the bar.
-
#empty_for_player?(player_number) ⇒ Boolean
If the player has no pieces.
-
#initialize(pieces:) ⇒ Bar
constructor
A new instance of Bar.
-
#number ⇒ String
The identifier of the bar, returns the string ‘bar’.
-
#number_of_pieces_owned_by_player(player_number) ⇒ Fixnum
Number of pieces owned by the specified player.
-
#pieces_owned_by_player(player_number) ⇒ Array<Piece>
ALl the pieces owned by the specified player.
-
#pop(player_number) ⇒ Piece, NilClass
Removes a piece owned by the specified player and return it.
-
#push(piece) ⇒ Array<Piece>
Adds a piece to the bar.
Methods included from Common
Constructor Details
#initialize(pieces:) ⇒ Bar
A new instance of Bar.
Example:
# Instantiates a new Bar
JustBackgammon::Bar.new({
pieces: [{owner: 1}, {owner: 1}]
})
24 25 26 |
# File 'lib/just_backgammon/bar.rb', line 24 def initialize(pieces:) @pieces = JustBackgammon::Piece.load(pieces) end |
Instance Attribute Details
#pieces ⇒ Array<Piece> (readonly)
29 30 31 |
# File 'lib/just_backgammon/bar.rb', line 29 def pieces @pieces end |
Instance Method Details
#any_pieces_for_player?(player_number) ⇒ Boolean
If the player has any pieces.
66 67 68 |
# 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 ⇒ Hash
A hashed serialized representation of the bar.
104 105 106 |
# File 'lib/just_backgammon/bar.rb', line 104 def as_json { pieces: pieces.map(&:as_json) } end |
#empty_for_player?(player_number) ⇒ Boolean
If the player has no pieces.
76 77 78 |
# File 'lib/just_backgammon/bar.rb', line 76 def empty_for_player?(player_number) pieces_owned_by_player(player_number).none? end |
#number ⇒ String
The identifier of the bar, returns the string ‘bar’.
36 37 38 |
# File 'lib/just_backgammon/bar.rb', line 36 def number 'bar' end |
#number_of_pieces_owned_by_player(player_number) ⇒ Fixnum
Number of pieces owned by the specified player.
56 57 58 |
# 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) ⇒ Array<Piece>
ALl the pieces owned by the specified player.
46 47 48 |
# File 'lib/just_backgammon/bar.rb', line 46 def pieces_owned_by_player(player_number) pieces.select { |p| p.owner == player_number } end |
#pop(player_number) ⇒ Piece, NilClass
Removes a piece owned by the specified player and return it.
86 87 88 89 |
# File 'lib/just_backgammon/bar.rb', line 86 def pop(player_number) p = pieces.find { |p| p.owner == player_number } pieces.delete(p) end |
#push(piece) ⇒ Array<Piece>
Adds a piece to the bar.
97 98 99 |
# File 'lib/just_backgammon/bar.rb', line 97 def push(piece) pieces.push(piece) end |