Class: DiceBox::Cup

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

Overview

Commodity class to use multiple Dice instances at the same time

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dices = []) ⇒ Cup

Returns a new instance of Cup.

Parameters:

  • dices (Array) (defaults to: [])

    an Array of Dices to put in the Cup



19
20
21
22
# File 'lib/dice_box/cup.rb', line 19

def initialize(dices = [])
  @dices = dices
  @rolled_sides = []
end

Instance Attribute Details

#dicesArray

Returns the Array of Dices.

Returns:

  • (Array)

    the Array of Dices



6
7
8
# File 'lib/dice_box/cup.rb', line 6

def dices
  @dices
end

#resultInteger (readonly) Also known as: rolled, rolled_value

Returns the result from the previous roll.

Returns:

  • (Integer)

    the result from the previous roll



14
15
16
# File 'lib/dice_box/cup.rb', line 14

def result
  @result
end

#rolled_sidesArray (readonly)

Returns the last rolled Sides of each Dice.

Returns:

  • (Array)

    the last rolled Sides of each Dice



10
11
12
# File 'lib/dice_box/cup.rb', line 10

def rolled_sides
  @rolled_sides
end

Instance Method Details

#maximumInteger Also known as: max

Returns the highest value the Cup can roll

Returns:

  • (Integer)

    minimum roll value



35
36
37
# File 'lib/dice_box/cup.rb', line 35

def maximum
  dices.map(&:maximum).reduce(:+)
end

#minimumInteger Also known as: min

Returns the lowest value the Cup can roll

Returns:

  • (Integer)

    minimum roll value



42
43
44
# File 'lib/dice_box/cup.rb', line 42

def minimum
  dices.map(&:minimum).reduce(:+)
end

#rollInteger

Rolls all the Dices in the Cup

Returns:

  • (Integer)

    the sum of the rolled Dices



26
27
28
29
30
31
# File 'lib/dice_box/cup.rb', line 26

def roll
  @result = dices.map { |dice| dice.roll }.reduce(&:+)
  @rolled_sides = dices.map(&:rolled_side)

  result
end