Class: JustBackgammon::PointSet
- Inherits:
-
Object
- Object
- JustBackgammon::PointSet
- Extended by:
- Forwardable, Common
- Defined in:
- lib/just_backgammon/point_set.rb
Overview
PointSet
A collection of points.
Instance Attribute Summary collapse
-
#points ⇒ Array<Point>
readonly
All the points in the set.
Instance Method Summary collapse
-
#as_json ⇒ Hash
A hashed serialized representation of the bar.
-
#destinations(from, dice, player_number) ⇒ PointSet
Finds all destinations points from a point with a dice roll for the specified player.
-
#find_by_number(number) ⇒ Point
Finds a point with the matching number.
-
#initialize(points:) ⇒ PointSet
constructor
A new instance of Bar.
-
#not_home(player_number) ⇒ PointSet
Finds all points with pieces that are not yet home for the specified player.
-
#owned_by_player(player_number) ⇒ PointSet
Finds all points owned by the specified player.
-
#some_pieces_not_home?(player_number) ⇒ Boolean
Checks if any pieces are not yet home for the specified player.
Methods included from Common
Constructor Details
#initialize(points:) ⇒ PointSet
A new instance of Bar.
Example:
# Instantiates a new PointSet
JustBackgammon::PointSet.new({
points: [point_a, point_b]
})
24 25 26 |
# File 'lib/just_backgammon/point_set.rb', line 24 def initialize(points:) @points = Point.load(points) end |
Instance Attribute Details
#points ⇒ Array<Point> (readonly)
Returns all the points in the set.
29 30 31 |
# File 'lib/just_backgammon/point_set.rb', line 29 def points @points end |
Instance Method Details
#as_json ⇒ Hash
A hashed serialized representation of the bar.
77 78 79 |
# File 'lib/just_backgammon/point_set.rb', line 77 def as_json points.map(&:as_json) end |
#destinations(from, dice, player_number) ⇒ PointSet
Finds all destinations points from a point with a dice roll for the specified player.
68 69 70 71 72 |
# File 'lib/just_backgammon/point_set.rb', line 68 def destinations(from, dice, player_number) in_range = dice.map { |d| destination(from, d, player_number) }.compact possible = in_range.select { |p| p.empty? || p.owned_by_player?(player_number) || p.blot? } self.class.new(points: possible) end |
#find_by_number(number) ⇒ Point
Finds a point with the matching number.
38 39 40 |
# File 'lib/just_backgammon/point_set.rb', line 38 def find_by_number(number) points.find { |p| p.number == number } end |
#not_home(player_number) ⇒ PointSet
Finds all points with pieces that are not yet home for the specified player.
45 46 47 48 |
# File 'lib/just_backgammon/point_set.rb', line 45 def not_home(player_number) not_home_points = points.select { |p| p.owned_by_player?(player_number) && !p.home?(player_number) } self.class.new(points: not_home_points) end |
#owned_by_player(player_number) ⇒ PointSet
Finds all points owned by the specified player.
60 61 62 63 |
# File 'lib/just_backgammon/point_set.rb', line 60 def owned_by_player(player_number) owned = @points.select { |p| p.owned_by_player?(player_number) } self.class.new(points: owned) end |
#some_pieces_not_home?(player_number) ⇒ Boolean
Checks if any pieces are not yet home for the specified player.
53 54 55 |
# File 'lib/just_backgammon/point_set.rb', line 53 def some_pieces_not_home?(player_number) not_home(player_number).any? end |