class PhyshRoller::DiceRoll
Constants
- DICE_ROLL_REGEXP
Splits a dice roll string in to the :number_of_dice, :sides_on_dice and :roll_modifier capture groups.
Attributes
dice_array[R]
dice_roll_string[R]
The original dice roll string, downcased and validated.
roll_modifier[R]
sides_on_dice[R]
Public Class Methods
new(dice_roll_string, output=$stdout)
click to toggle source
Accepts the initial :dice_roll_string and sets the default attributes.
Can also take an :output attribute, which is currently only used for test doubles, but down the line may be used for other nefarious purposes.
# File lib/physh_roller/dice_roll.rb, line 18 def initialize(dice_roll_string, output=$stdout) @output = output @dice_roll_string = dice_roll_string.downcase.gsub(' ', '') if valid_dice_roll_string? set_default_values add_dice_to_dice_array(@number_of_dice, @sides_on_dice) else raise ArgumentError.new('Invalid dice roll string.') end end
Public Instance Methods
add_dice_to_dice_array(number_of_dice, sides_on_dice)
click to toggle source
Creates the required instances of the Die
class and adds them to the :dice_array
# File lib/physh_roller/dice_roll.rb, line 40 def add_dice_to_dice_array(number_of_dice, sides_on_dice) number_of_dice.times do die = Die.new(sides_on_dice) @dice_array << die end end
number_of_dice()
click to toggle source
# File lib/physh_roller/dice_roll.rb, line 47 def number_of_dice @dice_array.size end
output_results()
click to toggle source
# File lib/physh_roller/dice_roll.rb, line 60 def output_results results = self.results @output.puts "You rolled: #{results[:dice_roll]}" @output.puts "Dice rolls: #{results[:dice_rolls].join(', ')}" @output.puts "Total: #{results[:sum]}" end
results()
click to toggle source
# File lib/physh_roller/dice_roll.rb, line 51 def results results_hash = { :dice_roll => @dice_roll_string, :dice_rolls => @dice_array.map { |die| die.last_result }.sort, :roll_modifier => @roll_modifier, :sum => (@dice_array.map { |die| die.last_result }.reduce(:+) + @roll_modifier) } end
roll_dice()
click to toggle source
# File lib/physh_roller/dice_roll.rb, line 33 def roll_dice @dice_array.each { |die| die.roll } return self end
valid_dice_roll_string?()
click to toggle source
# File lib/physh_roller/dice_roll.rb, line 29 def valid_dice_roll_string? !!(@dice_roll_string =~ DICE_ROLL_REGEXP) end
Private Instance Methods
set_default_values()
click to toggle source
Generates the default attrributes from the validated :dice_roll_string.
# File lib/physh_roller/dice_roll.rb, line 71 def set_default_values @number_of_dice, @sides_on_dice, @roll_modifier = DICE_ROLL_REGEXP.match(@dice_roll_string).captures.map { |capture| capture.to_i } @number_of_dice = 1 unless (@number_of_dice > 0) @roll_modifier ||= 0 @dice_array = [] end