Class: JustBackgammon::PointSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Common
Defined in:
lib/just_backgammon/point_set.rb

Overview

PointSet

A collection of points.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

load

Constructor Details

#initialize(points:) ⇒ PointSet

A new instance of Bar.

Example:

# Instantiates a new PointSet
JustBackgammon::PointSet.new({
  points: [point_a, point_b]
})

Parameters:

  • points (Array<Hash>)

    All the points in the set.



24
25
26
# File 'lib/just_backgammon/point_set.rb', line 24

def initialize(points:)
  @points = Point.load(points)
end

Instance Attribute Details

#pointsArray<Point> (readonly)

Returns all the points in the set.

Returns:

  • (Array<Point>)

    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_jsonHash

A hashed serialized representation of the bar.

Returns:

  • (Hash)


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.

Returns:



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.

Returns:



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.

Returns:



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.

Returns:



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.

Returns:

  • (Boolean)


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