ruby-dice 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ # This gem provides classes for dices like they are used in pen&paper role
2
+ # playing games.
3
+ #
4
+ # Have a look at the examples section to see how they're used.
5
+ #
6
+ # Have a look at the +UA+ namespace to see how to create special dice
@@ -0,0 +1,16 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'dice/d6'
3
+ include Dice
4
+
5
+ [
6
+ 'D6 * 3',
7
+ '3 * D6',
8
+ 'D6 + 2',
9
+ '3 * D6 + 2',
10
+ '1 - D6'
11
+ ].each do |code|
12
+ puts '---'
13
+ puts "#{code} => #{eval code}"
14
+ end
15
+
16
+
@@ -0,0 +1,8 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 10-sided dice with a roll value range of 1-10
4
+ class Dice::D10 < Dice::Dice
5
+ def initialize
6
+ super 1, 10
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 100-sided dice with a roll value range of 00-99
4
+ class Dice::D100 < Dice::Dice
5
+ def initialize
6
+ super 0, 100
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 12-sided dice with a roll value range of 1-12
4
+ class Dice::D12 < Dice::Dice
5
+ def initialize
6
+ super 1, 12
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 20-sided dice with a roll value range of 1-20
4
+ class Dice::D20 < Dice::Dice
5
+ def initialize
6
+ super 1, 20
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 30-sided dice with a roll value range of 1-30
4
+ #
5
+ # The only game using this kind of dice as far as I know
6
+ # is the german rpg 'Ruf des Warlock'
7
+ class Dice::D30 < Dice::Dice
8
+ def initialize
9
+ super 1, 30
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 4-sided dice with a roll value range of 1-4
4
+ class Dice::D4 < Dice::Dice
5
+ def initialize
6
+ super 1, 4
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 6-sided dice with a roll value range of 1-6
4
+ class Dice::D6 < Dice::Dice
5
+ def initialize
6
+ super 1, 6
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'dice/dice'
2
+
3
+ # Implements a standard 8-sided dice with a roll value range of 1-8
4
+ class Dice::D8 < Dice::Dice
5
+ def initialize
6
+ super 1, 8
7
+ end
8
+ end
@@ -0,0 +1,89 @@
1
+ require 'singleton'
2
+
3
+ module Dice
4
+ autoload :D4, 'dice/d4'
5
+ autoload :D6, 'dice/d6'
6
+ autoload :D8, 'dice/d8'
7
+ autoload :D10, 'dice/d10'
8
+ autoload :D12, 'dice/d12'
9
+ autoload :D20, 'dice/d20'
10
+ autoload :D30, 'dice/d30'
11
+ autoload :D100, 'dice/d100'
12
+ end
13
+
14
+ class Dice::Dice
15
+
16
+ include Singleton
17
+ private_class_method :instance
18
+
19
+ attr_reader :min_val, :sides
20
+ attr_writer :coerced # :nodoc:
21
+
22
+ def initialize min_val_arg, sides_arg
23
+ @min_val = min_val_arg
24
+ @sides = sides_arg
25
+ end
26
+
27
+ # rolls dice and returns result as +Fixnum+
28
+ def roll
29
+ min_val + rand(sides)
30
+ end
31
+
32
+ # 3 * D6 rolls 3 times, returns added results
33
+ # D6 * 3 rolls 1 time and multiplies with 3
34
+ def * other
35
+ if @coerced
36
+ result = 0
37
+ other.to_i.times do
38
+ result += roll
39
+ end
40
+ result
41
+ else
42
+ roll * other
43
+ end
44
+ end
45
+
46
+ def /(other)
47
+ @coerced ? (other / roll) : (roll / other)
48
+ end
49
+
50
+ def -(other)
51
+ @coerced ? (other - roll) : (roll - other)
52
+ end
53
+
54
+ def +(other)
55
+ roll + other
56
+ end
57
+
58
+ ['*', '/', '+', '-'].each do |op|
59
+ class_eval <<-EOF
60
+ def self.#{op}(other)
61
+ instance.send(:#{op}, other)
62
+ end
63
+ EOF
64
+ end
65
+
66
+ def coerce(other)
67
+ [coerced_clone, other]
68
+ end
69
+
70
+ def self.coerce(other)
71
+ [coerced_instance, other]
72
+ end
73
+
74
+ def self.coerced_instance # :nodoc:
75
+ result = new
76
+ result.coerced = true
77
+ result
78
+ end
79
+ private_class_method :coerced_instance
80
+
81
+ private
82
+
83
+ def coerced_clone # :nodoc:
84
+ result = clone
85
+ result.coerced = true
86
+ result
87
+ end
88
+
89
+ end
@@ -0,0 +1,6 @@
1
+ module Dice
2
+ module UA
3
+ autoload :D100, 'dice/ua/d100'
4
+ autoload :D10, 'dice/ua/d10'
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'dice/dice'
2
+
3
+ module Dice::UA; end
4
+
5
+ # This is an ordinary D10 just in Namespace UA
6
+ class Dice::UA::D10 < Dice::D10; end
@@ -0,0 +1,79 @@
1
+ require 'dice/dice'
2
+
3
+ # Contains Dice for Unknown Armies by Atlas Games
4
+ module Dice::UA; end
5
+
6
+ # This is a special D100 which returns not only a number, but an instance of Result
7
+ # which has some special methods needed by Unknown Armies
8
+ class Dice::UA::D100 < Dice::Dice
9
+ def initialize
10
+ super 0, 10
11
+ end
12
+
13
+ def roll
14
+ Result.new(super, super)
15
+ end
16
+ end
17
+
18
+ # The methods are according to Rulebook 2nd (AG6020)
19
+ # Chapter 'About the rules' (P. 7)
20
+ # This result holds the values of two D10 rolls
21
+ class Dice::UA::Result
22
+ def initialize(tens, ones)
23
+ @tens = tens
24
+ @ones = ones
25
+ end
26
+
27
+ # creates cross sum with special cases as stated in paragraph "Rolling Dice"
28
+ def add
29
+ (@tens.zero? ? 10 : @tens) + (@ones.zero? ? 10 : @ones)
30
+ end
31
+
32
+ # returns +true+ if both dice show same result, else returns +false+
33
+ def matched?
34
+ @tens == @ones
35
+ end
36
+
37
+ def to_i
38
+ result = 10 * @tens + @ones
39
+ result.zero? ? 100 : result
40
+ end
41
+
42
+ # returns +true+ if both dice show 0, else returns +false+
43
+ def fumble?
44
+ @tens == 0 && @ones == 0
45
+ end
46
+
47
+ # returns +true+ if result is zero-one, else returns +false+
48
+ def crit?
49
+ @tens == 0 && @ones == 1
50
+ end
51
+
52
+ # switch the dice
53
+ def flip_flop!
54
+ @tens, @ones = @ones, @tens
55
+ end
56
+
57
+ def -(subtrahend)
58
+ to_i - subtrahend
59
+ end
60
+
61
+ def +(addend)
62
+ to_i + addend
63
+ end
64
+
65
+ def *(other)
66
+ to_i * other
67
+ end
68
+
69
+ def /(other)
70
+ to_i / other
71
+ end
72
+
73
+ # exact coercion is done in next step
74
+ def coerce other
75
+ [other, self.to_i]
76
+ end
77
+
78
+ end
79
+
@@ -0,0 +1,69 @@
1
+ require 'dice/dice'
2
+ require 'test/unit'
3
+
4
+ class DTest < Dice::Dice
5
+
6
+ @@call_count = 0
7
+
8
+ # always returns 6
9
+ def initialize
10
+ super 6, 1
11
+ end
12
+
13
+ # overwritten to provide roll count
14
+ def roll
15
+ @@call_count += 1
16
+ super
17
+ end
18
+
19
+ def self.call_count
20
+ @@call_count
21
+ end
22
+ end
23
+
24
+ class TestDice < Test::Unit::TestCase
25
+
26
+ def test_dice_multiply
27
+ multiplier = 3
28
+ result = 0
29
+ assert_rolled_n_times 1 do |dice|
30
+ result = dice * multiplier
31
+ end
32
+ assert_equal result, 6 * multiplier
33
+ end
34
+
35
+ def test_other_multiply
36
+ multiplier = 3
37
+ result = 0
38
+ assert_rolled_n_times multiplier do |dice|
39
+ result = multiplier * dice
40
+ end
41
+ assert_equal result, 6 * multiplier
42
+ end
43
+
44
+ def test_dice_subtract
45
+ assert_equal DTest - 2, 4
46
+ end
47
+
48
+ def test_other_subtract
49
+ assert_equal 2 - DTest, -4
50
+ end
51
+
52
+ def test_dice_divide
53
+ assert_equal DTest / 2, 3
54
+ end
55
+
56
+ def test_other_divide
57
+ assert_equal 12 / DTest, 2
58
+ end
59
+
60
+ private
61
+
62
+ def assert_rolled_n_times count, &block
63
+ before = DTest.call_count
64
+ block.call DTest
65
+ after = DTest.call_count
66
+ assert_equal(before + count, after)
67
+ end
68
+
69
+ end
@@ -0,0 +1,21 @@
1
+ require 'dice/ua'
2
+ require 'test/unit'
3
+
4
+ include Dice::UA
5
+
6
+ class TestUA < Test::Unit::TestCase
7
+
8
+ def test_both
9
+ a = D100.instance
10
+ b = D10.instance
11
+ assert_equal a.sides, 10 # its a special dice
12
+ assert_equal b.sides, 10
13
+ end
14
+
15
+ def test_calc_with_result
16
+ assert_nothing_raised do
17
+ D100 - 20
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,10 @@
1
+ # testsuite is called by gem or directly from folder tests
2
+ # rake-task uses testcases itself
3
+
4
+ require 'test/unit'
5
+
6
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
7
+
8
+ Dir.glob(File.join(File.dirname(__FILE__), 'tc_*.rb')).each do |file|
9
+ require file
10
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-dice
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Achatz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-13 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: " Provides dice representations for roleplaying games with neat syntax\n"
17
+ email: stefan_achatz@web.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - examples/example.rb
26
+ - lib/dice/d20.rb
27
+ - lib/dice/d8.rb
28
+ - lib/dice/d10.rb
29
+ - lib/dice/d4.rb
30
+ - lib/dice/dice.rb
31
+ - lib/dice/ua.rb
32
+ - lib/dice/d30.rb
33
+ - lib/dice/d100.rb
34
+ - lib/dice/ua/d10.rb
35
+ - lib/dice/ua/d100.rb
36
+ - lib/dice/d12.rb
37
+ - lib/dice/d6.rb
38
+ - tests/tc_ua.rb
39
+ - tests/ts_all.rb
40
+ - tests/tc_test.rb
41
+ - README
42
+ has_rdoc: true
43
+ homepage: http://rubyforge.org/projects/ruby-dice/
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README
50
+ - --title
51
+ - ruby-dice Documentation
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: ruby-dice
69
+ rubygems_version: 1.3.4
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Dice library for Ruby
73
+ test_files:
74
+ - tests/ts_all.rb