Skip to content

Instantly share code, notes, and snippets.

@saraid
Last active August 29, 2015 14:22
Show Gist options
  • Save saraid/56f7251dbc760d8d1420 to your computer and use it in GitHub Desktop.
Save saraid/56f7251dbc760d8d1420 to your computer and use it in GitHub Desktop.
Quick dice roller script.
class Array
def sum
reduce(0) { |sum, x| sum + x }
end
end
class Dice
def self.roll(sides = 6)
rand(sides) + 1
end
end
class RollResults
def initialize(rolls = [], opts = {})
@rolls = rolls
@opts = opts
end
attr_reader :rolls, :opts
def self.roll(num = 1, opts = {:sides => 6})
rolls = num.times.collect { Dice.roll(opts[:sides]) }
new(rolls, opts)
end
def describe
msg = if rolls.length > 1 || opts[:change]
"[#{rolls.join(',')}]" << " Sum: #{rolls.sum + (opts[:change] || 0)}"
else
rolls.first.to_s
end
end
end
class WoDRoller < RollResults
def self.roll(num = 1, opts = {})
opts = default_options.merge! opts
if num == :chance
opts[:chance] = true
num = 1
end
rolls = [num.times.collect { Dice.roll(10) }]
return new(rolls, opts) if opts[:chance]
until (exploders = rolls.last.select { |num| num >= opts[:explosion_threshold] }).empty?
rolls << exploders.collect { Dice.roll(10) }
end
return new(rolls, opts)
end
def self.default_options
{ :chance => false,
:success_threshold => 8,
:explosion_threshold => 10 }
end
def describe
if opts[:chance]
chance_die = rolls.flatten.first
(chance_die == 10 ? 'Success' : 'Failure') << " [#{chance_die}]"
else
successes = rolls.flatten.select { |num| num >= opts[:success_threshold] }
success = successes.empty?.!
if success
exceptional = successes.length >= 5
[ (exceptional ? 'Exceptional ' : '') << 'Success',
rolls.collect { |roll| "[#{roll.join(',')}]" }.join(' '),
"with #{successes.length} successes"
].join(' ')
else
"Failure [#{rolls.first.join(',')}]"
end
end
end
end
class ORERoller < RollResults
def self.roll(num = 1, opts = {})
rolls = [num.times.collect { Dice.roll(10) }]
new(rolls, opts)
end
def describe
sets = rolls.first.group_by { |x| x }.sort.collect do |height, set|
next if set.length == 1
"#{set.length}x#{height}"
end.compact.<<("[#{rolls.first.sort.join(',')}]").join(' ')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment