class JustBackgammon::DiceSet

DiceSet

The collections of dice.

Attributes

dice[R]

@return [Array<Dice>] allthe dice in the set, each with a number

Public Class Methods

new(dice:) click to toggle source

A new instance of DiceSet.

@param [Array<Hash>] dice

All the dice in the set, each with a number.

Example:

# Instantiates a new DiceSet
JustBackgammon::DiceSet.new({
  dice: [{number: 1}, {number: 2}]
})
# File lib/just_backgammon/dice_set.rb, line 24
def initialize(dice:)
  @dice = Die.load(dice)
end

Public Instance Methods

as_json() click to toggle source

A hashed serialized representation of the dice set.

@return [Hash]

# File lib/just_backgammon/dice_set.rb, line 72
def as_json
  dice.map(&:as_json)
end
find_by_number(number) click to toggle source

finds a die that matches the specified number.

@return [Die]

# File lib/just_backgammon/dice_set.rb, line 37
def find_by_number(number)
  @dice.find { |d| d.number == number }
end
numbers() click to toggle source

returns an array of the numbers on the dice.

@return [Array<Fixnum>]

# File lib/just_backgammon/dice_set.rb, line 44
def numbers
  @dice.map(&:number)
end
reset() click to toggle source

sets all dice numbers to nil and reduces the number of dice to two.

@return [Array<Die>]

# File lib/just_backgammon/dice_set.rb, line 62
def reset
  @dice.each(&:reset)
  if @dice.size > 2
    @dice.slice!(-2..-1)
  end
end
roll() click to toggle source

randomizes each die and duplicates if they are the same number.

@return [Array<Die>]

# File lib/just_backgammon/dice_set.rb, line 51
def roll
  @dice.each(&:roll)
  if @dice.first == @dice.last
    dup_dice = @dice.map { |d| Die.new(id: d.id + 2, number: d.number) }
    @dice.concat(dup_dice)
  end
end