rollr 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/rollr.rb +43 -7
- metadata +2 -2
data/lib/rollr.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Rollr Simulates rolling dice of different size.
|
2
2
|
#
|
3
|
+
# In its most recent version, it also supports a kind of primitive logging,
|
4
|
+
# which can track the rolls made to the die over time, as well as the individual
|
5
|
+
# results of rolls within a pool of rolls (so, for instance, if you rolled 20
|
6
|
+
# 6 six sided die, you could see the result of each individual roll of each
|
7
|
+
# die.
|
8
|
+
#
|
3
9
|
# @author Alex Jarvis
|
4
10
|
|
5
11
|
require 'rubygems'
|
@@ -7,36 +13,66 @@ require 'rubygems'
|
|
7
13
|
module Rollr
|
8
14
|
|
9
15
|
|
10
|
-
# Represents a single instance of
|
16
|
+
# Represents a single instance of a die
|
11
17
|
#
|
12
18
|
class Die
|
13
19
|
|
14
20
|
|
15
21
|
# @attribute sides [Integer] The number of sides the die has
|
16
22
|
#
|
17
|
-
attr_accessor :sides
|
23
|
+
attr_accessor :sides, :rolls
|
18
24
|
|
19
|
-
# New instance of
|
25
|
+
# New instance of Die
|
20
26
|
#
|
21
27
|
# @param [Integer] sides
|
22
28
|
# @return [Object] Die object
|
23
29
|
#
|
24
30
|
def initialize(sides)
|
25
31
|
@sides = sides
|
32
|
+
@rolls = []
|
26
33
|
end #initialize
|
27
34
|
|
28
|
-
# Roll a new die.
|
35
|
+
# Roll a new die. This creates a DiceRoll Object, and adds it to the array of rolls from this Die.
|
36
|
+
# Then, return the total of that Roll.
|
29
37
|
#
|
30
38
|
# @param [Integer] count
|
31
|
-
# @return [
|
39
|
+
# @return [Integer] The result of the rolls, by way of a DiceRoll object.
|
32
40
|
#
|
33
|
-
def roll(count)
|
34
|
-
(1..count).map { |d| rand(self.sides) + 1 }
|
41
|
+
def roll(count=1)
|
42
|
+
individual_rolls = (1..count).map { |d| rand(self.sides) + 1 }
|
43
|
+
@rolls << DiceRoll.new(individual_rolls, self.sides, count)
|
44
|
+
@rolls.last.total
|
35
45
|
end #roll
|
36
46
|
|
37
47
|
end #Die
|
38
48
|
|
49
|
+
#DiceRoll is a class that holds information regarding a single instance of a rolled die.
|
50
|
+
#
|
51
|
+
class DiceRoll
|
52
|
+
|
53
|
+
# @attribute each_side [Array] the individual result of a single roll of a die.
|
54
|
+
# @attribute total [Integer] the combined total of the individual rolls
|
55
|
+
# @attribute dice_sides [Integer] the number of sides the rolled die had
|
56
|
+
# @attribute number_of_dice [Integer] the number of dice rolled.
|
57
|
+
#
|
58
|
+
attr_accessor :each_die, :total, :dice_sides, :number_of_dice
|
59
|
+
|
60
|
+
|
61
|
+
# New instance of DiceRoll
|
62
|
+
#
|
63
|
+
# @param [Array] individual
|
64
|
+
# @param [Integer] sides
|
65
|
+
# @param [Integer] number
|
66
|
+
# @return [Object] Die object
|
67
|
+
#
|
68
|
+
def initialize(individual, sides, number)
|
69
|
+
@each_die = individual
|
70
|
+
@dice_sides = sides
|
71
|
+
@number_of_dice = number
|
72
|
+
@total = individual.inject(0) { |total, d| total += d }
|
73
|
+
end #initialize
|
39
74
|
|
75
|
+
end #DiceRoll
|
40
76
|
#Die Constants
|
41
77
|
|
42
78
|
D3 = Die.new(3)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rollr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-12 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Rollr is a Gem that provides useful functions to simulate the Rolling
|
15
15
|
of Dice... because we weren't social enough as is.
|