Skip to content

Instantly share code, notes, and snippets.

@phrozen
Created July 14, 2010 01:36
Show Gist options
  • Save phrozen/474871 to your computer and use it in GitHub Desktop.
Save phrozen/474871 to your computer and use it in GitHub Desktop.
class Dicer
def Dicer.roll(faces, n = 1)
result = 0
n.times { result = result + 1 + rand(faces) }
result
end
end
class Dice
attr_reader :faces, :rolls, :result
def initialize(faces)
@faces = faces
@rolls = []
end
def roll( n = 1 )
reset!
n.times { @rolls << 1 + rand(@faces) }
@result = @rolls.inject(:+)
end
def reset!
@rolls.clear
@result = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment