nairda-dice 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2008 Adrian Pacała
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README ADDED
@@ -0,0 +1 @@
1
+ Simple Ruby class for simulating dice rolls.
data/lib/dice.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'pathname'
2
+
3
+ DICE_ROOT = Pathname(__FILE__).dirname.expand_path + 'dice'
4
+
5
+ require DICE_ROOT + 'core_ext'
6
+ require DICE_ROOT + 'version'
7
+
8
+ # = Dice
9
+ #
10
+ # Original code & copyright by Jamis Buck <jamis@37signals.com>.
11
+ #
12
+ # Class representing one or more traditional dice used in role-playing games.
13
+ #
14
+ # Dice.new(1, 6) # => Creates one six-sided die.
15
+ # Dice.new(9, 10, 20) # => Nine ten-sided dice modified by twenty.
16
+ #
17
+ # === Using role-play notation methods
18
+ #
19
+ # By extending the Integer class you get the feeling of the standard dice
20
+ # notation:
21
+ #
22
+ # 1.d6 # => Same as Dice.new(1, 6)
23
+ # 5.d20 + 7 # => Dice.new(5, 20, 7)
24
+ #
25
+ # By default, only the common dice types are implemented (d4, d6, d8, d10,
26
+ # d12, d20, and d100).
27
+ class Dice
28
+ include Comparable
29
+
30
+ # Number of dice.
31
+ attr_reader :count
32
+
33
+ # Number of sides.
34
+ attr_reader :sides
35
+
36
+ # Roll modifier.
37
+ attr_reader :modifier
38
+
39
+ # Creates new Dice object.
40
+ #
41
+ # * +dice+ - dice count
42
+ # * +die_type+ - sides count
43
+ # * +mod+ - value added (or subtracted) to the result (optional)
44
+ def initialize(dice, die_type, mod = 0)
45
+ @count = dice > 0 ? dice : 1
46
+ @sides = die_type > 0 ? die_type : 1
47
+ @modifier = mod
48
+ end
49
+
50
+ # Returns new Dice object incremented (or decremented) by +n+.
51
+ def +(n); self.class.new(@count, @sides, @modifier + n) end
52
+
53
+ # Same as calling + method with a negative modifier.
54
+ def -(n); self.+(-n) end
55
+
56
+ # Maximum result of the roll.
57
+ def max
58
+ @count * @sides + @modifier
59
+ end
60
+
61
+ # Minimum result of the roll.
62
+ def min
63
+ @count + @modifier
64
+ end
65
+
66
+ # Average result of the roll.
67
+ def avg
68
+ ((max + min) / 2.0).round
69
+ end
70
+
71
+ # Rolls the dice.
72
+ #
73
+ # * +collect+ - specifies if result should returned either as Fixnum or
74
+ # Array
75
+ # * +block+ - if given, every roll will be yielded separately
76
+ #
77
+ # Note that the modifier is only added when returning result as Fixnum.
78
+ def roll(collect = false, &block) #:yields: result
79
+ result = (1..@count).collect { block_given? ? yield(roll_one) : roll_one }
80
+ result << @modifier unless result.is_a?(Array) || @modifier.zero?
81
+ collect ? result : result.inject(nil) { |sum, x| sum ? sum + x : x }
82
+ end
83
+
84
+ alias_method :to_i, :roll
85
+
86
+ # Same as calling roll(+true+).
87
+ def to_a; roll(true) end
88
+
89
+ # Returns standard, role-play dice format. Custom die +mark+ can be
90
+ # specified (default is _d_).
91
+ def to_s(mark = 'd')
92
+ s = "#{@count}#{mark}#{@sides}"
93
+ s << "+#{@modifier}" unless @modifier.zero?
94
+ s
95
+ end
96
+
97
+ private
98
+ # Returns result of one particular roll.
99
+ def roll_one
100
+ rand(@sides) + 1
101
+ end
102
+ end
@@ -0,0 +1 @@
1
+ require DICE_ROOT + 'core_ext/fixnum'
@@ -0,0 +1,4 @@
1
+ class Fixnum
2
+ # Define aliases to create Dice objects easier.
3
+ [4, 6, 8, 10, 12, 20, 100].each { |sides| define_method("d#{sides}") { Dice.new(self, sides) } }
4
+ end
@@ -0,0 +1,3 @@
1
+ class Dice
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'pathname'
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require Pathname(__FILE__).dirname.expand_path.parent + 'lib/dice'
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Fixnum do
4
+ it 'should implement all dice methods' do
5
+ [4, 6, 8, 10, 12, 20, 100].each do |sides|
6
+ 1.send("d#{sides}").should be_instance_of(Dice)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Dice do
4
+ before(:each) do
5
+ @dice = Dice.new(6, 12)
6
+ @complex_dice = Dice.new(10, 20, 7)
7
+ end
8
+
9
+ it 'should create new Dice object' do
10
+ @dice.should be_instance_of(Dice)
11
+ @dice.count.should equal(6)
12
+ @dice.sides.should equal(12)
13
+ @dice.modifier.should equal(0)
14
+ end
15
+
16
+ it 'should create new Dice object with modifier' do
17
+ @complex_dice.count.should equal(10)
18
+ @complex_dice.sides.should equal(20)
19
+ @complex_dice.modifier.should equal(7)
20
+ end
21
+
22
+ it 'should add positive modifier' do
23
+ dice = @dice + 7
24
+ dice.modifier.should equal(7)
25
+ complex_dice = @complex_dice + 3
26
+ complex_dice.modifier.should equal(10)
27
+ end
28
+
29
+ it 'should add negative modifier' do
30
+ dice = @dice - 9
31
+ dice.modifier.should equal(-9)
32
+ complex_dice = @complex_dice - 6
33
+ complex_dice.modifier.should equal(1)
34
+ end
35
+
36
+ it 'should calculate maximum, minimum, and average result of the roll' do
37
+ @dice.max.should equal(72)
38
+ @dice.min.should equal(6)
39
+ @dice.avg.should equal(39)
40
+ @complex_dice.max.should equal(207)
41
+ @complex_dice.min.should equal(17)
42
+ @complex_dice.avg.should equal(112)
43
+ end
44
+
45
+ it 'should roll and return the result as a number' do
46
+ @dice.roll.should satisfy { |r| r > 0 && r < 73 }
47
+ @complex_dice.roll.should satisfy { |r| r > 0 && r < 208 }
48
+ end
49
+
50
+ it 'should roll and return the result as a array' do
51
+ @dice.roll(true).should have(6).items
52
+ @complex_dice.roll(true).should have(10).items
53
+ end
54
+
55
+ it 'should roll and yield the results' do
56
+ @dice.roll { |r| r.should satisfy { r > 0 && r < 13 } }
57
+ end
58
+
59
+ it 'should return a string representation of the dice' do
60
+ @dice.to_s.should eql('6d12')
61
+ @complex_dice.to_s.should eql('10d20+7')
62
+ end
63
+
64
+ it 'should return custom string representation of the dice' do
65
+ @dice.to_s('kitteh').should eql('6kitteh12')
66
+ @complex_dice.to_s('_hai_').should eql('10_hai_20+7')
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nairda-dice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - "Adrian Paca\xC5\x82a"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: adrian@zenbe.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ files:
26
+ - README
27
+ - LICENSE
28
+ - lib/dice/core_ext/fixnum.rb
29
+ - lib/dice/version.rb
30
+ - lib/dice/core_ext.rb
31
+ - lib/dice.rb
32
+ - spec/unit/dice_spec.rb
33
+ - spec/unit/core_ext/fixnum_spec.rb
34
+ - spec/spec_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/nairda/dice
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --line-numbers
40
+ - --inline-source
41
+ - - --charset
42
+ - utf-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.0.1
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Simple Ruby class for simulating dice rolls.
64
+ test_files: []
65
+