Class: JustBackgammon::OffBoard
- Inherits:
-
Object
- Object
- JustBackgammon::OffBoard
- Extended by:
- Common
- Defined in:
- lib/just_backgammon/off_board.rb
Overview
OffBoard
The off board is where pieces go when bearing off. 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
-
#as_json ⇒ Hash
A hashed serialized representation of off board.
- #hittable?(_ = nil) ⇒ Boolean
-
#initialize(pieces:) ⇒ OffBoard
constructor
A new instance of OffBoard.
-
#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.
-
#push(piece) ⇒ Array<Piece>
Adds a piece off board.
Methods included from Common
Constructor Details
#initialize(pieces:) ⇒ OffBoard
A new instance of OffBoard.
Example:
# Instantiates a new OffBoard
JustBackgammon::OffBoard.new({
pieces: [{owner: 1}, {owner: 1}]
})
22 23 24 |
# File 'lib/just_backgammon/off_board.rb', line 22 def initialize(pieces:) @pieces = Piece.load(pieces) end |
Instance Attribute Details
#pieces ⇒ Array<Piece> (readonly)
Returns all the pieces on the bar, each piece has an owner.
27 28 29 |
# File 'lib/just_backgammon/off_board.rb', line 27 def pieces @pieces end |
Instance Method Details
#as_json ⇒ Hash
A hashed serialized representation of off board.
73 74 75 |
# File 'lib/just_backgammon/off_board.rb', line 73 def as_json { pieces: pieces.map(&:as_json) } end |
#hittable?(_ = nil) ⇒ Boolean
66 67 68 |
# File 'lib/just_backgammon/off_board.rb', line 66 def hittable?(_=nil) false end |
#number ⇒ String
The identifier of the bar, returns the string ‘bar’.
32 33 34 |
# File 'lib/just_backgammon/off_board.rb', line 32 def number 'off_board' end |
#number_of_pieces_owned_by_player(player_number) ⇒ Fixnum
Number of pieces owned by the specified player.
52 53 54 |
# File 'lib/just_backgammon/off_board.rb', line 52 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.
42 43 44 |
# File 'lib/just_backgammon/off_board.rb', line 42 def pieces_owned_by_player(player_number) pieces.select { |p| p.owner == player_number } end |
#push(piece) ⇒ Array<Piece>
Adds a piece off board.
62 63 64 |
# File 'lib/just_backgammon/off_board.rb', line 62 def push(piece) @pieces.push(piece) end |