Sha256: 998d5c86a1e394ea0eb3405148e0a60865332e1496f0e4aef8f2b7f44a176b97

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

require 'forwardable'
require 'just_backgammon/common'
require 'just_backgammon/die'

module JustBackgammon

  # = DiceSet
  #
  # The collections of dice.
  class DiceSet
    extend Common
    extend Forwardable

    # 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}]
    #   })
    def initialize(dice:)
      @dice = Die.load(dice)
    end

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

    def_delegator :dice, :map
    def_delegator :dice, :each

    # finds a die that matches the specified number.
    #
    # @return [Die]
    def find_by_number(number)
      @dice.find { |d| d.number == number }
    end

    # returns an array of the numbers on the dice.
    #
    # @return [Array<Fixnum>]
    def numbers
      @dice.map(&:number)
    end

    # randomizes each die and duplicates if they are the same number.
    #
    # @return [Array<Die>]
    def roll
      @dice.each(&:roll)
      if @dice.first == @dice.last
        @dice.concat(@dice)
      end
    end

    # sets all dice numbers to nil and reduces the number of dice to two.
    #
    # @return [Array<Die>]
    def reset
      @dice.each(&:reset)
      if @dice.size > 2
        @dice.slice!(-2..-1)
      end
    end

    # A hashed serialized representation of the dice set.
    #
    # @return [Hash]
    def as_json
      dice.map(&:as_json)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
just_backgammon-1.0.0 lib/just_backgammon/dice_set.rb
just_backgammon-0.2.0 lib/just_backgammon/dice_set.rb
just_backgammon-0.1.2 lib/just_backgammon/dice_set.rb
just_backgammon-0.1.1 lib/just_backgammon/dice_set.rb