Class: Droll

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(die_code) ⇒ Droll

The die_code argument is any valid die code recognized by Droll.

Droll.new '4x7+3'


150
151
152
153
154
155
156
157
# File 'lib/droll.rb', line 150

def initialize die_code
  @dcode = die_code.strip
  @allowed = Regexp.new(
    /^[1-9]{,2}[A-Za-z]\d?[1-9]{1,2}(\.\d+)?([+-]\d+)?(\s*.+)?$/
  )

  process_die
end

Instance Attribute Details

#dcodeObject (readonly)

Returns the value of attribute dcode.



132
133
134
# File 'lib/droll.rb', line 132

def dcode
  @dcode
end

#diceObject (readonly)

Returns the value of attribute dice.



132
133
134
# File 'lib/droll.rb', line 132

def dice
  @dice
end

#dmaxObject (readonly)

Returns the value of attribute dmax.



132
133
134
# File 'lib/droll.rb', line 132

def dmax
  @dmax
end

#dvalObject (readonly)

Returns the value of attribute dval.



132
133
134
# File 'lib/droll.rb', line 132

def dval
  @dval
end

#modifierObject (readonly)

Returns the value of attribute modifier.



132
133
134
# File 'lib/droll.rb', line 132

def modifier
  @modifier
end

#pcodeObject (readonly)

Returns the value of attribute pcode.



132
133
134
# File 'lib/droll.rb', line 132

def pcode
  @pcode
end

#roll_typeObject (readonly)

Returns the value of attribute roll_type.



132
133
134
# File 'lib/droll.rb', line 132

def roll_type
  @roll_type
end

#signObject (readonly)

Returns the value of attribute sign.



132
133
134
# File 'lib/droll.rb', line 132

def sign
  @sign
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



132
133
134
# File 'lib/droll.rb', line 132

def threshold
  @threshold
end

Class Method Details

.versionObject

This method returns the version number for the Droll gem.



140
# File 'lib/droll.rb', line 140

def self.version; '1.0.3'; end

Instance Method Details

#count_dice_max_thresh(dresults) ⇒ Object

This method takes an array of numeric values as its sole argument, and compares it to the instantiated die code’s threshold value. It returns an integer value equal to the number of values in the array argument that are equal to or greater than the threshold value.

Given a threshold of 1:

count_dice_max_thresh([0,1,2,3])        #=> 2

count_dice_max_thresh([2,3])            #=> 0


274
275
276
# File 'lib/droll.rb', line 274

def count_dice_max_thresh dresults
  dresults.reject {|n| n > threshold }.size
end

#count_dice_min_thresh(dresults) ⇒ Object

This method takes an array of numeric values as its sole argument, and compares it to the instantiated die code’s threshold value. It returns an integer value equal to the number of values in the array argument that are equal to or greater than the threshold value.

Given a threshold of 2:

count_dice_min_thresh([0,1,2,3])        #=> 2

count_dice_min_thresh([0,1])            #=> 0


255
256
257
# File 'lib/droll.rb', line 255

def count_dice_min_thresh dresults
  dresults.reject {|n| n < threshold }.size
end

#explode_on_max_total(dresults) ⇒ Object

This method takes an array of numeric values as its sole argument, and compares the total of the values in the array to the product of the instantiated die code’s threshold value and number of dice value. Unless that product is greater than that total, the results of a die roll of 1xX.Y are appended to the array provided in the method argument, where X is the value of the instantiated die code and Y is the threshold of the instantiated die code.

Given a die code of 2e2:

explode_on_max_total([2, 2])                #=> [2, 2, ...]

explode_on_max_total([2, 1])                #=> [2, 1]


343
344
345
346
347
348
349
350
351
# File 'lib/droll.rb', line 343

def explode_on_max_total dresults
  roll_total = dresults.flatten.map {|result| result.to_i }.inject(:+)

  unless roll_total < (threshold * dice)
    dresults.push(roll_die dval, 'x', threshold)
  end

  dresults.flatten
end

#roll(formatted = true) ⇒ Object

By default, this method returns a string showing the die code rolled, the individual die rolls that make up the complete roll of dice, the modifier applied to the roll (showing “+ 0” if no modifier was given), and the result, in the following format:

3d6+2: [3, 4, 1] + 2 = 10

If the formatted argument is given a false value, this method returns only an integer equal to the total result.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/droll.rb', line 367

def roll formatted=true
  results = Array.new

  return "bad die code: #{dcode}" unless valid?

  dice.times do
    results.push(
      roll_die dval, roll_type, threshold
    ).flatten!
  end

  if roll_type == 'e'
    results = explode_on_max_total results
  end

  total = if %w(k K n N).include? roll_type
    analyze_rolls roll_type, results
  else # "normal" totaling
    results.map {|s| s.to_i }.inject(:+)
  end

  if sign == '+'
    total += modifier
  elsif sign == '-'
    total -= modifier
  end

  if formatted
    "#{dcode}: #{results} #{sign} #{modifier} = #{total}"
  else
    total
  end
end

#sum_thresh_high_dice(dresults) ⇒ Object

This method takes an array of numeric values as its sole argument, and returns the total of the highest N values in the array, where N is the instantiated die code’s threshold value.

Given a die code of 3k02:

sum_thresh_high_dice([2, 2, 2])         #=> 2

sum_thresh_high_dice([0, 0, 1])         #=> 1

Given a die code of 3k02.2:

sum_thresh_high_dice([0, 1, 2])         #=> 3

sum_thresh_high_dice([0, 0, 1])         #=> 1


298
299
300
# File 'lib/droll.rb', line 298

def sum_thresh_high_dice dresults
  dresults.sort.reverse[0..(threshold - 1)].inject(:+)
end

#sum_thresh_low_dice(dresults) ⇒ Object

This method takes an array of numeric values as its sole argument, and returns the total of the lowest N values in the array, where N is the instantiated die code’s threshold value.

Given a die code of 3K02:

sum_thresh_low_dice([1, 1, 1])          #=> 1

sum_thresh_low_dice([0, 1, 2])          #=> 0

Given a die code of 3K02.2:

sum_thresh_low_dice([0, 1, 2])          #=> 1

sum_thresh_low_dice([1, 2, 2])          #=> 3


322
323
324
# File 'lib/droll.rb', line 322

def sum_thresh_low_dice dresults
  dresults.sort[0..(threshold - 1)].inject(:+)
end