rdice 0.0.4 → 1.0.0

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/README.md CHANGED
@@ -26,6 +26,10 @@ dice.roll_two
26
26
  dice.roll_d20
27
27
  dice.roll_three_d8
28
28
  dice.roll_10_d3
29
+
30
+ # You can call the method roll directly if you preffer, just pass the number of sides and dices
31
+ # dice.roll(20,4)
32
+
29
33
  ```
30
34
 
31
35
  This gem comes with shortcuts of one to twenty so you can call *.roll_four_d12*. If you want a dice with more than twenty sides you can explicit declare the number of sides with *.roll_32_d3*
@@ -39,15 +43,26 @@ When you call *.roll* method this roll a dice with the defaults sides and dices
39
43
  ```ruby
40
44
  dice = Dice.new(20,2)
41
45
 
42
- dice.roll # rolls two d20 by default
43
- dice.roll_two # rolls two d20
44
- dice.roll_four # rolls four d20
45
- dice.roll_d20 # rolls two d20
46
+ dice.roll # rolls two d20 by default
47
+ dice.roll_two # rolls two d20
48
+ dice.roll_four # rolls four d20
49
+ dice.roll_d20 # rolls two d20
46
50
  dice.roll_three_d8 # rolls three d8
47
- dice.roll_10_d3 # rolls 10 d3
51
+ dice.roll_10_d3 # rolls 10 d3
52
+ ```
53
+
54
+ There are three configs that you can change
55
+
56
+ ```ruby
57
+ Dice::Config.sides= 12 # set the default dice sides to 12
58
+ Dice::Config.dices= 3 # set default dices rolled to 3
59
+ Dice::Config.array= true # change the result to a array without sum the dices
60
+
61
+ # All these preferences can be setted on creation with
62
+ dice = Dice.new(6,3,true) # by default roll three dices with six sides each and return a array as result
48
63
  ```
49
64
 
50
65
  ## License
51
66
 
52
- Ruby on Rails is released under the MIT license:
67
+ RDice is released under the MIT license:
53
68
  [http://www.opensource.org/licenses/MIT](http://www.opensource.org/licenses/MIT)
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["de.tierno@gmail.com"]
10
10
  s.homepage = "https://github.com/detierno/dice"
11
11
  s.summary = %q{Roll dices with different sides}
12
- s.description = %q{This gem allow you to set dices and roll it. You can set custom defaults and change behavior to fit you purpose }
12
+ s.description = %q{This gem allow you to set dices and roll it. You can set custom defaults and change behavior to fit your purpose }
13
13
 
14
14
  s.rubyforge_project = "rdice"
15
15
 
@@ -5,6 +5,7 @@ class Dice
5
5
  end
6
6
 
7
7
  def self.sides=(sides)
8
+ raise ArgumentError, 'Must be a Integer' unless sides.is_a? Integer
8
9
  @sides = sides
9
10
  end
10
11
 
@@ -13,7 +14,17 @@ class Dice
13
14
  end
14
15
 
15
16
  def self.dices=(dices)
17
+ raise ArgumentError, 'Must be a Integer' unless dices.is_a? Integer
16
18
  @dices = dices
17
19
  end
20
+
21
+ def self.array?
22
+ @array || false
23
+ end
24
+
25
+ def self.array=(array)
26
+ raise ArgumentError, 'Must be true or false' unless [true, false].include?(array)
27
+ @array = array
28
+ end
18
29
  end
19
30
  end
@@ -1,7 +1,8 @@
1
1
  class Dice
2
- def initialize(sides=6, dices=1)
2
+ def initialize(sides=6, dices=1, array=false)
3
3
  Dice::Config.sides = sides
4
4
  Dice::Config.dices = dices
5
+ Dice::Config.array = array
5
6
  end
6
7
 
7
8
  def roll(sides=preferred_sides, dices=preferred_dices)
@@ -30,8 +31,18 @@ private
30
31
  Dice::Config.dices
31
32
  end
32
33
 
34
+ def result_in_array?
35
+ Dice::Config.array?
36
+ end
37
+
33
38
  def get_result(sides, dices)
34
- (1..dices).inject(0) {|a, b| a + rand(sides) + 1 }
39
+ if result_in_array?
40
+ results = []
41
+ dices.times { results << (rand(sides) + 1).to_i }
42
+ results
43
+ else
44
+ (1..dices).inject(0) {|a, b| a + rand(sides) + 1 }
45
+ end
35
46
  end
36
47
 
37
48
  def translate_value(number)
@@ -1,3 +1,3 @@
1
1
  class Dice
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dice do
4
+ NTIMES = 300
5
+
6
+ describe "new with default values as array" do
7
+
8
+ describe ".roll" do
9
+ let(:dice) { Dice.new(6,1,true) }
10
+
11
+ it "return value between a array with one element" do
12
+ NTIMES.times { dice.roll.size.should == 1 }
13
+ end
14
+ end
15
+
16
+ describe ".roll_two" do
17
+ let(:dice) { Dice.new(6,1,true) }
18
+
19
+ it "return a array of 2 elements" do
20
+ NTIMES.times do
21
+ result = dice.roll_two
22
+ result.size.should == 2
23
+ result.class.should == Array
24
+ end
25
+ end
26
+
27
+ it "and values are between 1 and 6" do
28
+ NTIMES.times do
29
+ result = dice.roll_two
30
+ result.each do |item|
31
+ 1.upto(6).include?(item).should be_true
32
+ end
33
+ end
34
+ end
35
+
36
+ it "cant return 0" do
37
+ NTIMES.times do
38
+ result = dice.roll_two
39
+ result.each do |item|
40
+ item.should_not < 1
41
+ end
42
+ end
43
+ end
44
+
45
+ it "cant be more then 6" do
46
+ NTIMES.times do
47
+ result = dice.roll_two
48
+ result.each do |item|
49
+ item.should_not > 6
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ describe ".roll_d20" do
56
+ let(:dice) { Dice.new(6,1,true) }
57
+
58
+ it "return a array of 1 element" do
59
+ NTIMES.times do
60
+ result = dice.roll_d20
61
+ result.size.should == 1
62
+ result.class.should == Array
63
+ end
64
+ end
65
+
66
+ it "return value between 1 to 20" do
67
+ NTIMES.times do
68
+ dice.roll_d20.each { |item| 1.upto(20).include?(item).should be_true }
69
+ end
70
+ end
71
+
72
+ it "cant return 21" do
73
+ NTIMES.times do
74
+ dice.roll_d20.each { |item| item.should_not > 20 }
75
+ end
76
+ end
77
+ end
78
+
79
+ describe ".roll_two_d10" do
80
+ let(:dice) { Dice.new(6,1,true) }
81
+
82
+ it "return a array of 2 elements" do
83
+ NTIMES.times do
84
+ dice.roll_two_d10.size.should == 2
85
+ dice.roll_two_d10.class.should == Array
86
+ end
87
+ end
88
+
89
+ it "return value between 1 to 10" do
90
+ NTIMES.times { dice.roll_two_d10.each { |item| 1.upto(10).include?(item).should be_true } }
91
+ end
92
+ end
93
+ end
94
+
95
+ describe "new with values of 12 sides with three dices as array" do
96
+
97
+ describe ".roll" do
98
+ let(:dice) { Dice.new(12,3,true) }
99
+
100
+ it "return value between a array with three elements" do
101
+ NTIMES.times { dice.roll.size.should == 3 }
102
+ end
103
+ end
104
+
105
+ describe ".roll_two" do
106
+ let(:dice) { Dice.new(12,3,true) }
107
+
108
+ it "return a array of 2 elements" do
109
+ NTIMES.times do
110
+ result = dice.roll_two
111
+ result.size.should == 2
112
+ result.class.should == Array
113
+ end
114
+ end
115
+
116
+ it "and values are between 1 and 12" do
117
+ NTIMES.times do
118
+ result = dice.roll_two
119
+ result.each do |item|
120
+ 1.upto(12).include?(item).should be_true
121
+ end
122
+ end
123
+ end
124
+
125
+ it "cant return 0" do
126
+ NTIMES.times do
127
+ result = dice.roll_two
128
+ result.each do |item|
129
+ item.should_not < 1
130
+ end
131
+ end
132
+ end
133
+
134
+ it "cant be more then 12" do
135
+ NTIMES.times do
136
+ result = dice.roll_two
137
+ result.each do |item|
138
+ item.should_not > 12
139
+ end
140
+ end
141
+ end
142
+ end
143
+
144
+ describe ".roll_d20" do
145
+ let(:dice) { Dice.new(12,3,true) }
146
+
147
+ it "return a array of 1 element" do
148
+ NTIMES.times do
149
+ result = dice.roll_d20
150
+ result.size.should == 3
151
+ result.class.should == Array
152
+ end
153
+ end
154
+
155
+ it "return value between 1 to 20" do
156
+ NTIMES.times do
157
+ dice.roll_d20.each { |item| 1.upto(20).include?(item).should be_true }
158
+ end
159
+ end
160
+
161
+ it "cant return 21" do
162
+ NTIMES.times do
163
+ dice.roll_d20.each { |item| item.should_not > 20 }
164
+ end
165
+ end
166
+ end
167
+
168
+ describe ".roll_two_d10" do
169
+ let(:dice) { Dice.new(12,3,true) }
170
+
171
+ it "return a array of 2 elements" do
172
+ NTIMES.times do
173
+ dice.roll_two_d10.size.should == 2
174
+ dice.roll_two_d10.class.should == Array
175
+ end
176
+ end
177
+
178
+ it "return value between 1 to 10" do
179
+ NTIMES.times { dice.roll_two_d10.each { |item| 1.upto(10).include?(item).should be_true } }
180
+ end
181
+ end
182
+ end
183
+
184
+ end
@@ -7,9 +7,23 @@ describe Dice::Config do
7
7
  Dice::Config.dices.should == 1
8
8
  end
9
9
 
10
- it "set dices and sides as requested" do
10
+ it "set dices and sides and result_type as requested" do
11
11
  dice = Dice.new(12,3)
12
12
  Dice::Config.sides.should == 12
13
13
  Dice::Config.dices.should == 3
14
+ Dice::Config.array?.should be_false
15
+ end
16
+
17
+ it "set array to true" do
18
+ dice = Dice.new(12,3, true)
19
+ Dice::Config.sides.should == 12
20
+ Dice::Config.dices.should == 3
21
+ Dice::Config.array?.should be_true
22
+ end
23
+
24
+ it "raise ArgumentError if values are of wrong type" do
25
+ lambda {Dice.new('12',3,true)}.should raise_error(ArgumentError)
26
+ lambda {Dice.new(12,'3',true)}.should raise_error(ArgumentError)
27
+ lambda {Dice.new(12,3,'true')}.should raise_error(ArgumentError)
14
28
  end
15
29
  end
@@ -109,7 +109,7 @@ describe Dice do
109
109
  describe ".roll_two_d10" do
110
110
  let(:dice) { Dice.new(12,3) }
111
111
 
112
- it "return value between 1 to 10" do
112
+ it "return value between 2 to 20" do
113
113
  NTIMES.times { 2.upto(20).include?(dice.roll_two_d10).should be_true }
114
114
  end
115
115
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-02 00:00:00.000000000 Z
12
+ date: 2012-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70335605167600 !ruby/object:Gem::Requirement
16
+ requirement: &70235608766060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70335605167600
24
+ version_requirements: *70235608766060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70335605166720 !ruby/object:Gem::Requirement
27
+ requirement: &70235608765140 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,9 +32,9 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70335605166720
35
+ version_requirements: *70235608765140
36
36
  description: ! 'This gem allow you to set dices and roll it. You can set custom defaults
37
- and change behavior to fit you purpose '
37
+ and change behavior to fit your purpose '
38
38
  email:
39
39
  - de.tierno@gmail.com
40
40
  executables: []
@@ -52,6 +52,7 @@ files:
52
52
  - lib/dice/config.rb
53
53
  - lib/dice/dice.rb
54
54
  - lib/dice/version.rb
55
+ - spec/array_dice_spec.rb
55
56
  - spec/config_spec.rb
56
57
  - spec/dice_spec.rb
57
58
  - spec/spec_helper.rb
@@ -69,7 +70,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
70
  version: '0'
70
71
  segments:
71
72
  - 0
72
- hash: 97332267924461556
73
+ hash: 747301994299439198
73
74
  required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  none: false
75
76
  requirements:
@@ -78,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
79
  version: '0'
79
80
  segments:
80
81
  - 0
81
- hash: 97332267924461556
82
+ hash: 747301994299439198
82
83
  requirements: []
83
84
  rubyforge_project: rdice
84
85
  rubygems_version: 1.8.10
@@ -86,6 +87,7 @@ signing_key:
86
87
  specification_version: 3
87
88
  summary: Roll dices with different sides
88
89
  test_files:
90
+ - spec/array_dice_spec.rb
89
91
  - spec/config_spec.rb
90
92
  - spec/dice_spec.rb
91
93
  - spec/spec_helper.rb