Class: Dicechucker::Dice

Inherits:
Object
  • Object
show all
Defined in:
lib/dicechucker/dice.rb

Direct Known Subclasses

DiceDropper, DiceExplode, DieSingle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dice, sides, modifier) ⇒ Dice

Returns a new instance of Dice.



6
7
8
9
10
11
# File 'lib/dicechucker/dice.rb', line 6

def initialize(dice, sides, modifier)
  @number_of_dice = dice
  @sides = sides
  @modifier = modifier
  roll
end

Instance Attribute Details

#modifierObject

Returns the value of attribute modifier.



4
5
6
# File 'lib/dicechucker/dice.rb', line 4

def modifier
  @modifier
end

#number_of_diceObject

Returns the value of attribute number_of_dice.



4
5
6
# File 'lib/dicechucker/dice.rb', line 4

def number_of_dice
  @number_of_dice
end

#resultsObject

Returns the value of attribute results.



4
5
6
# File 'lib/dicechucker/dice.rb', line 4

def results
  @results
end

#sidesObject

Returns the value of attribute sides.



4
5
6
# File 'lib/dicechucker/dice.rb', line 4

def sides
  @sides
end

#totalObject

Returns the value of attribute total.



4
5
6
# File 'lib/dicechucker/dice.rb', line 4

def total
  @total
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
34
35
# File 'lib/dicechucker/dice.rb', line 30

def ==(other)
  [@number_of_dice == other.number_of_dice,
   @sides == other.sides,
   @modifier == other.modifier,
   self.class == other.class].all?
end

#reportObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dicechucker/dice.rb', line 18

def report
  rep = 'rolled '
  rep << @results.join(', ')
  if @modifier > 0
    rep << " plus #{@modifier}"
  elsif @modifier < 0
    rep << " minus #{@modifier}"
  end
  rep << " for a total of #{@total}."
  rep
end

#rollObject



13
14
15
16
# File 'lib/dicechucker/dice.rb', line 13

def roll
  roll_dice
  @total = @results.inject(:+) + @modifier
end