Class: JustBackgammon::Point
- Inherits:
-
Object
- Object
- JustBackgammon::Point
- Extended by:
- Forwardable, Common
- Defined in:
- lib/just_backgammon/point.rb
Overview
Point
A point where pieces can land on. Each one has a number.
Instance Attribute Summary collapse
-
#number ⇒ Fixnum
readonly
The point number and identifier.
-
#pieces ⇒ Array<Piece>
readonly
All the pieces on this point.
Instance Method Summary collapse
-
#as_json ⇒ Hash
A hashed serialized representation of the bar.
-
#blocked? ⇒ Boolean
Checks if point has more than one piece.
-
#blot? ⇒ Boolean
Checks if point has only one piece.
-
#home?(player_number) ⇒ Boolean
Checks if point is a home point for specified player.
-
#initialize(pieces:, number:) ⇒ Point
constructor
A new instance of Point.
-
#owned_by_opponent?(player_number) ⇒ Boolean
Checks if point has pieces owned by the opponent of the specified player.
-
#owned_by_player?(player_number) ⇒ Boolean
Checks if point has pieces owned by the specified player.
-
#pop ⇒ Piece, NilClass
Removes a piece and returns it.
-
#push(piece) ⇒ Array<Piece>
Adds a piece to the point.
Methods included from Common
Constructor Details
#initialize(pieces:, number:) ⇒ Point
A new instance of Point.
Example:
# Instantiates a new Point
JustBackgammon::Point.new({
pieces: [{owner: 1}, {owner: 1}],
number: 1
})
28 29 30 31 |
# File 'lib/just_backgammon/point.rb', line 28 def initialize(pieces: , number:) @pieces = JustBackgammon::Piece.load(pieces) @number = number end |
Instance Attribute Details
#number ⇒ Fixnum (readonly)
Returns the point number and identifier.
37 38 39 |
# File 'lib/just_backgammon/point.rb', line 37 def number @number end |
#pieces ⇒ Array<Piece> (readonly)
Returns all the pieces on this point.
34 35 36 |
# File 'lib/just_backgammon/point.rb', line 34 def pieces @pieces end |
Instance Method Details
#as_json ⇒ Hash
A hashed serialized representation of the bar.
101 102 103 |
# File 'lib/just_backgammon/point.rb', line 101 def as_json { number: number, pieces: pieces.map(&:as_json) } end |
#blocked? ⇒ Boolean
Checks if point has more than one piece.
73 74 75 |
# File 'lib/just_backgammon/point.rb', line 73 def blocked? size > 1 end |
#blot? ⇒ Boolean
Checks if point has only one piece.
80 81 82 |
# File 'lib/just_backgammon/point.rb', line 80 def blot? size == 1 end |
#home?(player_number) ⇒ Boolean
Checks if point is a home point for specified player.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/just_backgammon/point.rb', line 87 def home?(player_number) case player_number when 1 (19..24).include?(number) when 2 (1..6).include?(number) else true end end |
#owned_by_opponent?(player_number) ⇒ Boolean
Checks if point has pieces owned by the opponent of the specified player.
66 67 68 |
# File 'lib/just_backgammon/point.rb', line 66 def owned_by_opponent?(player_number) pieces.any? { |p| p.owner != player_number } end |
#owned_by_player?(player_number) ⇒ Boolean
Checks if point has pieces owned by the specified player.
59 60 61 |
# File 'lib/just_backgammon/point.rb', line 59 def owned_by_player?(player_number) pieces.any? { |p| p.owner == player_number } end |
#pop ⇒ Piece, NilClass
Removes a piece and returns it.
45 46 47 |
# File 'lib/just_backgammon/point.rb', line 45 def pop @pieces.pop end |
#push(piece) ⇒ Array<Piece>
Adds a piece to the point.
52 53 54 |
# File 'lib/just_backgammon/point.rb', line 52 def push(piece) @pieces.push(piece) end |