Class: ParaDice::Cup

Inherits:
Object
  • Object
show all
Defined in:
lib/para_dice/cup.rb

Overview

A Cup hold a selection of dice and a selection of readers. It’s main purpose

is to provide a reusable component that you can call #roll on and get a
complex result from

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#DiceArray<ParaDice::Die]

Returns Array<ParaDice::Die].

Returns:



12
# File 'lib/para_dice/cup.rb', line 12

attribute :dice, Array[ParaDice::Die], default: []

#readersArray<#resolvce>

Returns:

  • (Array<#resolvce>)


20
# File 'lib/para_dice/cup.rb', line 20

attribute :readers, Array, default: []

#rngRandom

Returns:

  • (Random)


16
# File 'lib/para_dice/cup.rb', line 16

attribute :rng, Random, default: ->(*a) { Random.new }

Instance Method Details

#roll(roll_rng = rng) ⇒ Object

roll reach die in cup and call each reader in turn on the results, return

the result of the last reader

Parameters:

  • roll_rng (Random, #rand) (defaults to: rng)

    default: self.rng



25
26
27
# File 'lib/para_dice/cup.rb', line 25

def roll(roll_rng = rng)
  readers.reduce(roll!(roll_rng)) { |faces, reader| reader.resolve(faces) }
end

#roll!(roll_rng = rng) ⇒ Object

roll each die in cup using roll_rng and return array of raw results from

Parameters:

  • roll_rng (Random, #rand) (defaults to: rng)

    default: self.rng



31
32
33
# File 'lib/para_dice/cup.rb', line 31

def roll!(roll_rng = rng)
  dice.map { |d| d.roll(roll_rng) }
end