/
rollr.rb
54 lines (39 loc) · 942 Bytes
/
rollr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Rollr Simulates rolling dice of different size.
#
# @author Alex Jarvis
require 'rubygems'
module Rollr
# Represents a single instance of the result of a die
#
class Die
# @attribute sides [Integer] The number of sides the die has
#
attr_accessor :sides
# New instance of Dice.
#
# @param [Integer] sides
# @return [Object] Die object
#
def initialize(sides)
@sides = sides
end #initialize
# Roll a new die.
#
# @param [Integer] count
# @return [Object] integer
#
def roll(count=1)
individual_rolls = (1..count).map { |d| rand(self.sides) + 1 }
total_roll = individual_rolls.inject(0) { |total, d| total += d }
return individual_rolls, total_roll
end #roll
end #Die
#Die Constants
D3 = Die.new(3)
D4 = Die.new(4)
D6 = Die.new(6)
D8 = Die.new(8)
D10= Die.new(10)
D12 = Die.new(12)
D20 = Die.new(20)
end #Rollr