dice_box 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edf058b8c28a23ef4bb53a55a9580797bb633f9f
4
- data.tar.gz: 2a7e287a785e8d5dc8590092a3bb3fc9b91ab345
3
+ metadata.gz: 4a4dcaca70ba3a0420120279c4933da3f01c35e7
4
+ data.tar.gz: 54f5b75307f7caae345b22de1ed080ef9ff331ba
5
5
  SHA512:
6
- metadata.gz: c6fd3e0c0486d889b73f4ab4d365631a4db5e87d5be9f06b43458303cb61f733871846cdef2b38165a792a329fdcad7b9838806527a964433fc3656569235f5c
7
- data.tar.gz: 5fac7ec74f9d61bcd92a8fcc8299b1974880d597bce01b829e39f597cd037f731f9df7062c4a053ee662e6580cffbddccb2d7f5aeec04fe5a59097b3e8373998
6
+ metadata.gz: 0ebefdba428de973713c83c9b531dfe7fe11d06c9e5518a90c2cc170a7e41e2de3f4c9c5386b648a238e6c2e538ef8688635e6f8b2e7fbadec5c056ca7d88721
7
+ data.tar.gz: 508e21422f71948c1aea7507c4a757dcdf68afda48c697d80a8d3d758dcc3cc5e437fe3f9e74070dca13b855359e2a13c60db0c7487b1607bd7ed092a9a15000
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  coverage/
5
5
  doc/
6
+ pkg/
data/.travis.yml CHANGED
@@ -2,7 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 2.1.0
4
4
  - 2.0.0
5
- - 1.9.3
5
+ - jruby-1.7.13
6
6
 
7
7
  addons:
8
8
  code_climate:
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ![Dice Box](https://raw.githubusercontent.com/rafaelgonzalez/dice_box/master/dice.jpg)
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/dice_box.svg)](http://badge.fury.io/rb/dice_box)
5
6
  [![Build Status](https://travis-ci.org/rafaelgonzalez/dice_box.svg?branch=master)](https://travis-ci.org/rafaelgonzalez/dice_box)
6
7
  [![Code Climate](https://codeclimate.com/github/rafaelgonzalez/dice_box.png)](https://codeclimate.com/github/rafaelgonzalez/dice_box)
7
8
  [![Test Coverage](https://codeclimate.com/github/rafaelgonzalez/dice_box/coverage.png)](https://codeclimate.com/github/rafaelgonzalez/dice_box)
@@ -10,9 +11,9 @@ A gem of dices, to get rolling with Ruby.
10
11
 
11
12
  **Supported Ruby versions:**
12
13
 
13
- - 2.1.0
14
- - 2.0.0
15
- - 1.9.3
14
+ - MRI 2.1.0
15
+ - MRI 2.0.0
16
+ - JRuby 1.7.13
16
17
 
17
18
  ## Installation
18
19
 
@@ -38,9 +39,9 @@ Complete documentation available [here](http://rubydoc.info/github/rafaelgonzale
38
39
 
39
40
  # Using an instance
40
41
  dice = DiceBox::Dice.new(12)
41
- dice.rolled # => nil
42
+ dice.result # => nil
42
43
  dice.roll # => 24
43
- dice.rolled # => 24
44
+ dice.result # => 24
44
45
  ```
45
46
 
46
47
  - [DiceBox::Dice::Sides](http://rubydoc.info/github/rafaelgonzalez/dice_box/DiceBox/Dice/Side) (cheating with sides weights)
@@ -56,17 +57,35 @@ Complete documentation available [here](http://rubydoc.info/github/rafaelgonzale
56
57
  dice.roll # => 2
57
58
  ```
58
59
 
60
+ - [DiceBox::Cup](http://rubydoc.info/github/rafaelgonzalez/dice_box/DiceBox/Cup) (rolling multiple dice instances)
61
+
62
+ ```ruby
63
+ dices = [DiceBox::Dice.new(6), DiceBox::Dice.new(20), DiceBox::Dice.new(100)]
64
+ cup = DiceBox::Cup.new(dices)
65
+
66
+ cup.result # => nil
67
+ cup.roll # => 103
68
+ cup.result # => 103
69
+
70
+ cup.dices[0].result # => 2
71
+ cup.dices[1].result # => 19
72
+ cup.dices[2].result # => 88
73
+ ```
74
+
59
75
  ## Versioning
60
76
 
61
77
  DiceBox follows the principles of [semantic versioning](http://semver.org).
62
78
 
63
- > Given a version number MAJOR.MINOR.PATCH, increment the:
79
+ Given a version number MAJOR.MINOR.PATCH:
80
+
81
+ - MAJOR is incremented when incompatible API changes are made
82
+ - MINOR is incremented when functionalities are added in a backwards-compatible manner
83
+ - PATCH is incremented when backwards-compatible bug fixes are made
64
84
 
65
- > - MAJOR version when you make incompatible API changes,
66
- > - MINOR version when you add functionality in a backwards-compatible manner, and
67
- > - PATCH version when you make backwards-compatible bug fixes.
85
+ ## Similar Libraries
68
86
 
69
- > Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
87
+ - [GamesDice](https://github.com/neilslater/games_dice) (Ruby gem)
88
+ - [Droll](https://github.com/thebinarypenguin/droll) (Javascript)
70
89
 
71
90
  ## License
72
91
 
data/lib/dice_box.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  module DiceBox
3
3
  end
4
4
 
5
- require 'dice_box/version'
5
+ require 'dice_box/cup'
6
6
  require 'dice_box/dice'
7
7
  require 'dice_box/dice/side'
8
+ require 'dice_box/version'
@@ -0,0 +1,47 @@
1
+ module DiceBox
2
+ # Commodity class to use multiple Dice instances at the same time
3
+ class Cup
4
+ # @!attribute [rw] dices
5
+ # @return [Array] the Array of Dices
6
+ attr_accessor :dices
7
+
8
+ # @!attribute [r] rolled_sides
9
+ # @return [Array] the last rolled Sides of each Dice
10
+ attr_reader :rolled_sides
11
+
12
+ # @!attribute [r] result
13
+ # @return [Integer] the result from the previous roll
14
+ attr_reader :result
15
+ alias_method :rolled, :result
16
+ alias_method :rolled_value, :result
17
+
18
+ # @param dices [Array] an Array of Dices to put in the Cup
19
+ def initialize(dices = [])
20
+ @dices = dices
21
+ @rolled_sides = []
22
+ end
23
+
24
+ # Rolls all the Dices in the Cup
25
+ # @return [Integer] the sum of the rolled Dices
26
+ def roll
27
+ @result = dices.map { |dice| dice.roll }.reduce(&:+)
28
+ @rolled_sides = dices.map(&:rolled_side)
29
+
30
+ result
31
+ end
32
+
33
+ # Returns the highest value the Cup can roll
34
+ # @return [Integer] minimum roll value
35
+ def maximum
36
+ dices.map(&:maximum).reduce(:+)
37
+ end
38
+ alias_method :max, :maximum
39
+
40
+ # Returns the lowest value the Cup can roll
41
+ # @return [Integer] minimum roll value
42
+ def minimum
43
+ dices.map(&:minimum).reduce(:+)
44
+ end
45
+ alias_method :min, :minimum
46
+ end
47
+ end
data/lib/dice_box/dice.rb CHANGED
@@ -6,13 +6,20 @@ module DiceBox
6
6
  attr_reader :sides
7
7
 
8
8
  # @!attribute [r] rolled_side
9
- # @return [Array] the last rolled Side
9
+ # @return [Dice::Side] the last rolled Side
10
10
  attr_reader :rolled_side
11
11
 
12
+ # @!attribute [r] result
13
+ # @return [Integer] the result from the previous roll
14
+ attr_reader :result
15
+ alias_method :rolled, :result
16
+ alias_method :rolled_value, :result
17
+
12
18
  # @param sides_number [Integer] the number of Sides this Dice should have
13
19
  def initialize(sides_number)
14
20
  @sides = build_sides(sides_number)
15
21
  @rolled_side = nil
22
+ @rolled = nil
16
23
  end
17
24
 
18
25
  # Rolls multiple dices with the same number of sides
@@ -29,17 +36,26 @@ module DiceBox
29
36
 
30
37
  # Rolls the dice
31
38
  # @note Sets #rolled_side to the rolled Side
39
+ # @note Sets #rolled_value to the rolled Side's value
32
40
  # @return [Integer] the value of the rolled Side
33
41
  def roll
34
42
  @rolled_side = balanced? ? sides.sample : weighted_roll
35
- rolled_side.value
43
+ @result = rolled_side.value
44
+ end
45
+
46
+ # Returns the highest value the Dice can roll
47
+ # @return [Integer] maximum roll value
48
+ def maximum
49
+ sides.map(&:value).max
36
50
  end
51
+ alias_method :max, :maximum
37
52
 
38
- # Returns the last value rolled
39
- # @return [Integer] the last rolled value.
40
- def rolled
41
- @rolled_side ? @rolled_side.value : nil
53
+ # Returns the lowest value the Dice can roll
54
+ # @return [Integer] minimum roll value
55
+ def minimum
56
+ sides.map(&:value).min
42
57
  end
58
+ alias_method :min, :minimum
43
59
 
44
60
  # Determines if all Sides of the Dice have the same weight
45
61
  # @return [Boolean]
@@ -63,9 +79,9 @@ module DiceBox
63
79
 
64
80
  private
65
81
 
66
- # Instanciates multiple Sides
67
- # @param sides_number [Integer] the number of Sides to instanciate
68
- # @return [Array] the Array of instanciated Sides
82
+ # Instantiates multiple Sides
83
+ # @param sides_number [Integer] the number of Sides to instantiate
84
+ # @return [Array] the Array of instantiated Sides
69
85
  def build_sides(sides_number)
70
86
  sides_number.times.map do |number|
71
87
  Side.new(number + 1)
@@ -2,9 +2,9 @@ module DiceBox
2
2
  class Dice
3
3
  # Representation of the side of a dice
4
4
  class Side
5
- # @!attribute [r] value
5
+ # @!attribute [rw] value
6
6
  # @return [Integer] the actual value of the Side
7
- attr_reader :value
7
+ attr_accessor :value
8
8
 
9
9
  # @!attribute [rw] weight
10
10
  # @return [Float] the weight of the Side
@@ -1,4 +1,4 @@
1
1
  module DiceBox
2
2
  # Current version number of the library
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -0,0 +1,126 @@
1
+ describe DiceBox::Cup do
2
+ let(:dices) { [] }
3
+ subject { described_class.new(dices) }
4
+
5
+ describe '#roll' do
6
+ let(:dice_1) { DiceBox::Dice.new(6) }
7
+ let(:dice_2) { DiceBox::Dice.new(4) }
8
+ let(:dice_3) { DiceBox::Dice.new(12) }
9
+ let(:dices) { [dice_1, dice_2, dice_3] }
10
+
11
+ it 'returns the sum of the rolled values' do
12
+ expect(dice_1).to receive(:roll).and_return(3)
13
+ expect(dice_2).to receive(:roll).and_return(1)
14
+ expect(dice_3).to receive(:roll).and_return(10)
15
+
16
+ expect(subject.roll).to eql 14
17
+ end
18
+ end
19
+
20
+ describe '#result' do
21
+ let(:dice_1) { DiceBox::Dice.new(6) }
22
+ let(:dice_2) { DiceBox::Dice.new(4) }
23
+ let(:dice_3) { DiceBox::Dice.new(12) }
24
+ let(:dices) { [dice_1, dice_2, dice_3] }
25
+
26
+ it 'returns the sum of the last rolled values' do
27
+ rolled_value = subject.roll
28
+
29
+ expect(subject.result).to eql rolled_value
30
+ end
31
+
32
+ it 'does not change value if dices are rerolled' do
33
+ rolled_value = subject.roll
34
+
35
+ expect(subject.result).to eql rolled_value
36
+
37
+ expect(dice_1).to receive(:roll).and_return(1)
38
+ expect(dice_2).to receive(:roll).and_return(1)
39
+ expect(dice_3).to receive(:roll).and_return(1)
40
+
41
+ dice_1.roll
42
+ dice_2.roll
43
+ dice_3.roll
44
+
45
+ expect(subject.result).to eql rolled_value
46
+ end
47
+ end
48
+
49
+ describe '#rolled_sides' do
50
+ let(:dice_1) { DiceBox::Dice.new(6) }
51
+ let(:dice_2) { DiceBox::Dice.new(4) }
52
+ let(:dice_3) { DiceBox::Dice.new(12) }
53
+ let(:dices) { [dice_1, dice_2, dice_3] }
54
+
55
+ it 'returns an Array of Sides' do
56
+ expect(subject.rolled_sides).to be_an(Array)
57
+ expect(subject.rolled_sides).to be_empty
58
+
59
+ subject.roll
60
+
61
+ expect(subject.rolled_sides).to be_an(Array)
62
+ expect(subject.rolled_sides).to be_an(Array)
63
+ end
64
+
65
+ it 'does not change if dices are not rolled with the cup' do
66
+ subject.roll
67
+ rolled_sides = subject.rolled_sides
68
+
69
+ dice_1.roll
70
+ dice_2.roll
71
+ dice_3.roll
72
+ expect(subject.rolled_sides).to eql rolled_sides
73
+ end
74
+ end
75
+
76
+ describe '#maximum' do
77
+ let(:dices) { [DiceBox::Dice.new(6), DiceBox::Dice.new(20), DiceBox::Dice.new(100)] }
78
+ subject { DiceBox::Cup.new(dices) }
79
+
80
+ context 'with classical dices' do
81
+ it 'returns the highest value the cup can roll' do
82
+ expect(subject.maximum).to eql 126
83
+ end
84
+ end
85
+
86
+ context 'with dices with changed side values' do
87
+ before do
88
+ dices[0].sides[0].value = 56
89
+ dices[1].sides[0].value = 98
90
+ dices[2].sides[0].value = 112
91
+ end
92
+
93
+ it 'returns the highest value the cup can roll' do
94
+ expect(subject.maximum).to eql 266
95
+ end
96
+ end
97
+ end
98
+
99
+ describe '#minimum' do
100
+ let(:dices) { [DiceBox::Dice.new(6), DiceBox::Dice.new(6), DiceBox::Dice.new(6)] }
101
+ subject { DiceBox::Cup.new(dices) }
102
+
103
+ context 'with classical dices' do
104
+ it 'returns the lowest value the cup can roll' do
105
+ expect(subject.minimum).to eql 3
106
+ end
107
+ end
108
+
109
+ context 'with dices with changed side values' do
110
+ before do
111
+ dices.each do |dice|
112
+ dice.sides[0].value = 102
113
+ dice.sides[1].value = 206
114
+ dice.sides[2].value = 191
115
+ dice.sides[3].value = 76
116
+ dice.sides[4].value = 213
117
+ dice.sides[5].value = 87
118
+ end
119
+ end
120
+
121
+ it 'returns the lowest value the cup can roll' do
122
+ expect(subject.minimum).to eql 228
123
+ end
124
+ end
125
+ end
126
+ end
@@ -112,6 +112,68 @@ describe DiceBox::Dice do
112
112
  expect(subject.rolled).to eql rolled_value
113
113
  end
114
114
  end
115
+
116
+ context 'with a rolled side that changes value' do
117
+ it 'returns the rolled value' do
118
+ rolled_value = subject.roll
119
+
120
+ expect(subject.rolled).to eql rolled_value
121
+
122
+ subject.rolled_side.value = 1000
123
+
124
+ expect(subject.rolled).to eql rolled_value
125
+ end
126
+ end
127
+ end
128
+
129
+ describe '#maximum' do
130
+ subject { DiceBox::Dice.new(6) }
131
+
132
+ context 'with a classical dice' do
133
+ it 'returns the highest value the dice can roll' do
134
+ expect(subject.maximum).to eql 6
135
+ end
136
+ end
137
+
138
+ context 'with a dice with changed side values' do
139
+ before do
140
+ subject.sides[0].value = 247
141
+ subject.sides[1].value = 12
142
+ subject.sides[2].value = 271
143
+ subject.sides[3].value = 9
144
+ subject.sides[4].value = 46
145
+ subject.sides[5].value = 5
146
+ end
147
+
148
+ it 'returns the highest value the dice can roll' do
149
+ expect(subject.maximum).to eql 271
150
+ end
151
+ end
152
+ end
153
+
154
+ describe '#minimum' do
155
+ subject { DiceBox::Dice.new(6) }
156
+
157
+ context 'with a classical dice' do
158
+ it 'returns the lowest value the dice can roll' do
159
+ expect(subject.minimum).to eql 1
160
+ end
161
+ end
162
+
163
+ context 'with a dice with changed side values' do
164
+ before do
165
+ subject.sides[0].value = 247
166
+ subject.sides[1].value = 12
167
+ subject.sides[2].value = 271
168
+ subject.sides[3].value = 9
169
+ subject.sides[4].value = 46
170
+ subject.sides[5].value = 5
171
+ end
172
+
173
+ it 'returns the lowest value the dice can roll' do
174
+ expect(subject.minimum).to eql 5
175
+ end
176
+ end
115
177
  end
116
178
 
117
179
  describe '#balanced?' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dice_box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafaël Gonzalez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-27 00:00:00.000000000 Z
11
+ date: 2014-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,9 +142,11 @@ files:
142
142
  - dice.jpg
143
143
  - dice_box.gemspec
144
144
  - lib/dice_box.rb
145
+ - lib/dice_box/cup.rb
145
146
  - lib/dice_box/dice.rb
146
147
  - lib/dice_box/dice/side.rb
147
148
  - lib/dice_box/version.rb
149
+ - spec/dice_box/cup_spec.rb
148
150
  - spec/dice_box/dice/side_spec.rb
149
151
  - spec/dice_box/dice_spec.rb
150
152
  - spec/spec_helper.rb
@@ -173,6 +175,7 @@ signing_key:
173
175
  specification_version: 4
174
176
  summary: A gem with dices, to get rolling with Ruby.
175
177
  test_files:
178
+ - spec/dice_box/cup_spec.rb
176
179
  - spec/dice_box/dice/side_spec.rb
177
180
  - spec/dice_box/dice_spec.rb
178
181
  - spec/spec_helper.rb