physh_roller 0.0.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.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +7 -0
  3. data/README +1 -0
  4. data/bin/dice +42 -0
  5. data/doc/Object.html +204 -0
  6. data/doc/Output.html +226 -0
  7. data/doc/PhyshRoller.html +140 -0
  8. data/doc/PhyshRoller/DiceRoll.html +498 -0
  9. data/doc/PhyshRoller/Die.html +266 -0
  10. data/doc/created.rid +14 -0
  11. data/doc/doc/created_rid.html +97 -0
  12. data/doc/features/physh_roller/parse_dice_roll_string_feature.html +117 -0
  13. data/doc/features/physh_roller/roll_dice_feature.html +119 -0
  14. data/doc/features/physh_roller/roll_die_feature.html +117 -0
  15. data/doc/images/add.png +0 -0
  16. data/doc/images/arrow_up.png +0 -0
  17. data/doc/images/brick.png +0 -0
  18. data/doc/images/brick_link.png +0 -0
  19. data/doc/images/bug.png +0 -0
  20. data/doc/images/bullet_black.png +0 -0
  21. data/doc/images/bullet_toggle_minus.png +0 -0
  22. data/doc/images/bullet_toggle_plus.png +0 -0
  23. data/doc/images/date.png +0 -0
  24. data/doc/images/delete.png +0 -0
  25. data/doc/images/find.png +0 -0
  26. data/doc/images/loadingAnimation.gif +0 -0
  27. data/doc/images/macFFBgHack.png +0 -0
  28. data/doc/images/package.png +0 -0
  29. data/doc/images/page_green.png +0 -0
  30. data/doc/images/page_white_text.png +0 -0
  31. data/doc/images/page_white_width.png +0 -0
  32. data/doc/images/plugin.png +0 -0
  33. data/doc/images/ruby.png +0 -0
  34. data/doc/images/tag_blue.png +0 -0
  35. data/doc/images/tag_green.png +0 -0
  36. data/doc/images/transparent.png +0 -0
  37. data/doc/images/wrench.png +0 -0
  38. data/doc/images/wrench_orange.png +0 -0
  39. data/doc/images/zoom.png +0 -0
  40. data/doc/index.html +94 -0
  41. data/doc/js/darkfish.js +155 -0
  42. data/doc/js/jquery.js +18 -0
  43. data/doc/js/navigation.js +142 -0
  44. data/doc/js/search.js +94 -0
  45. data/doc/js/search_index.js +1 -0
  46. data/doc/js/searcher.js +228 -0
  47. data/doc/rdoc.css +595 -0
  48. data/doc/table_of_contents.html +98 -0
  49. data/features/physh_roller/parse_dice_roll_string.feature +19 -0
  50. data/features/physh_roller/roll_dice.feature +22 -0
  51. data/features/physh_roller/roll_die.feature +19 -0
  52. data/features/step_definitions/physh_roller/physh_roller_steps.rb +79 -0
  53. data/features/support/env.rb +3 -0
  54. data/lib/physh_roller.rb +2 -0
  55. data/lib/physh_roller/dice_roll.rb +62 -0
  56. data/lib/physh_roller/die.rb +14 -0
  57. data/spec/physh_roller/dice_roll_spec.rb +145 -0
  58. data/spec/physh_roller/die_spec.rb +44 -0
  59. data/spec/spec_helper.rb +3 -0
  60. metadata +133 -0
@@ -0,0 +1,98 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
6
+
7
+ <title>Table of Contents - RDoc Documentation</title>
8
+
9
+ <link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">
10
+
11
+ <script type="text/javascript">
12
+ var rdoc_rel_prefix = "./";
13
+ </script>
14
+
15
+ <script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
16
+ <script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
17
+ <script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
18
+ <script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
19
+ <script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
20
+ <script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>
21
+
22
+
23
+ <body class="indexpage">
24
+ <h1>Table of Contents - RDoc Documentation</h1>
25
+
26
+ <h2>Pages</h2>
27
+ <ul>
28
+ <li class="file">
29
+ <a href="doc/created_rid.html">created.rid</a>
30
+ </li>
31
+ <li class="file">
32
+ <a href="features/physh_roller/parse_dice_roll_string_feature.html">parse_dice_roll_string.feature</a>
33
+ </li>
34
+ <li class="file">
35
+ <a href="features/physh_roller/roll_dice_feature.html">roll_dice.feature</a>
36
+ </li>
37
+ <li class="file">
38
+ <a href="features/physh_roller/roll_die_feature.html">roll_die.feature</a>
39
+ </li>
40
+
41
+ </ul>
42
+
43
+ <h2 id="classes">Classes/Modules</h2>
44
+ <ul>
45
+ <li class="class">
46
+ <a href="Object.html">Object</a>
47
+ </li>
48
+ <li class="class">
49
+ <a href="Output.html">Output</a>
50
+ </li>
51
+ <li class="module">
52
+ <a href="PhyshRoller.html">PhyshRoller</a>
53
+ </li>
54
+ <li class="class">
55
+ <a href="PhyshRoller/DiceRoll.html">PhyshRoller::DiceRoll</a>
56
+ </li>
57
+ <li class="class">
58
+ <a href="PhyshRoller/Die.html">PhyshRoller::Die</a>
59
+ </li>
60
+
61
+ </ul>
62
+
63
+ <h2 id="methods">Methods</h2>
64
+ <ul>
65
+
66
+ <li class="method"><a href="PhyshRoller/Die.html#method-c-new">::new &mdash; PhyshRoller::Die</a>
67
+
68
+ <li class="method"><a href="PhyshRoller/DiceRoll.html#method-c-new">::new &mdash; PhyshRoller::DiceRoll</a>
69
+
70
+ <li class="method"><a href="PhyshRoller/DiceRoll.html#method-i-add_dice_to_dice_array">#add_dice_to_dice_array &mdash; PhyshRoller::DiceRoll</a>
71
+
72
+ <li class="method"><a href="Output.html#method-i-messages">#messages &mdash; Output</a>
73
+
74
+ <li class="method"><a href="PhyshRoller/DiceRoll.html#method-i-number_of_dice">#number_of_dice &mdash; PhyshRoller::DiceRoll</a>
75
+
76
+ <li class="method"><a href="Object.html#method-i-output">#output &mdash; Object</a>
77
+
78
+ <li class="method"><a href="Output.html#method-i-puts">#puts &mdash; Output</a>
79
+
80
+ <li class="method"><a href="PhyshRoller/DiceRoll.html#method-i-results">#results &mdash; PhyshRoller::DiceRoll</a>
81
+
82
+ <li class="method"><a href="PhyshRoller/Die.html#method-i-roll">#roll &mdash; PhyshRoller::Die</a>
83
+
84
+ <li class="method"><a href="PhyshRoller/DiceRoll.html#method-i-roll_dice">#roll_dice &mdash; PhyshRoller::DiceRoll</a>
85
+
86
+ <li class="method"><a href="PhyshRoller/DiceRoll.html#method-i-set_default_values">#set_default_values &mdash; PhyshRoller::DiceRoll</a>
87
+
88
+ <li class="method"><a href="PhyshRoller/DiceRoll.html#method-i-valid_dice_roll_string-3F">#valid_dice_roll_string? &mdash; PhyshRoller::DiceRoll</a>
89
+
90
+ </ul>
91
+
92
+
93
+ <footer id="validator-badges">
94
+ <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
95
+ <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 4.0.0.
96
+ <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
97
+ </footer>
98
+
@@ -0,0 +1,19 @@
1
+ Feature: Parse String To Dice Roll
2
+
3
+ As a user
4
+ To create a dice roll
5
+ I want to parse a string in to its components
6
+
7
+ Scenario Outline: parse valid dice roll string
8
+ Given I want to roll some dice
9
+ When I create a new dice roll with the string "<string>"
10
+ Then the number of dice should be "<number_of_dice>"
11
+ And the sides on the dice should be "<sides_on_dice>"
12
+ And the roll modifier should be "<roll_modifier>"
13
+
14
+ Scenarios: different dice strings
15
+ | string | number_of_dice | sides_on_dice | roll_modifier |
16
+ | 1d6 | 1 | 6 | 0 |
17
+ | 3d4 | 3 | 4 | 0 |
18
+ | 1d6+1 | 1 | 6 | 1 |
19
+ | 3d4-2 | 3 | 4 | -2 |
@@ -0,0 +1,22 @@
1
+ Feature: Roll dice
2
+
3
+ As a user
4
+ I want to roll a set of dice
5
+ To get a set of rolls and a total
6
+
7
+ Scenario Outline: getting the result of rolling some dice
8
+ Given I have a dice roll string described by "<dice_roll_string>"
9
+ When I roll the set of dice
10
+ Then the result should have "<number_of_dice>"
11
+ And each die roll should be equal to or greater than "<lower_limit>"
12
+ And each die roll should be equal to or less than "<upper_limit>"
13
+ And the total should be equal to or greater than "<lowest_total>"
14
+ And the total should be equal to or less than "<upper_total>"
15
+
16
+
17
+ Scenarios: different dice strings
18
+ | dice_roll_string | lower_limit | upper_limit | lowest_total | upper_total | number_of_dice |
19
+ | 1d6 | 1 | 6 | 1 | 6 | 1 |
20
+ | 3d4 | 1 | 4 | 3 | 12 | 3 |
21
+ | 1d6+1 | 1 | 6 | 2 | 7 | 1 |
22
+ | 3d4-2 | 1 | 4 | 1 | 10 | 3 |
@@ -0,0 +1,19 @@
1
+ Feature: Simple Die Roll
2
+
3
+ As a user
4
+ I want to roll a die
5
+ To generate a random number between 1 and the number of sides on the die
6
+
7
+ Scenario Outline: Roll a die
8
+ Given I want to roll a die with "<sides_on_die>" sides
9
+ When I roll the die
10
+ Then the result should be between "1" and "<sides_on_die>"
11
+
12
+ Scenarios: different sides on die
13
+ | sides_on_die |
14
+ | 2 |
15
+ | 6 |
16
+ | 12 |
17
+ | 100 |
18
+ | 1000 |
19
+ | 3 |
@@ -0,0 +1,79 @@
1
+ class Output
2
+ def messages
3
+ @messages = []
4
+ end
5
+
6
+ def puts(message)
7
+ messages << message
8
+ end
9
+ end
10
+
11
+ def output
12
+ @output ||= Output.new
13
+ end
14
+
15
+ Given /^I want to roll some dice$/ do
16
+
17
+ end
18
+
19
+ When /^I create a new dice roll with the string "(.*?)"$/ do |dice_roll_string|
20
+ @dice_roll = PhyshRoller::DiceRoll.new(dice_roll_string, output)
21
+ end
22
+
23
+ Then /^the number of dice should be "(.*?)"$/ do |number_of_dice|
24
+ @dice_roll.number_of_dice.should == number_of_dice.to_i
25
+ end
26
+
27
+ Then /^the sides on the dice should be "(.*?)"$/ do |sides_on_dice|
28
+ @dice_roll.sides_on_dice.should == sides_on_dice.to_i
29
+ end
30
+
31
+ Then /^the roll modifier should be "(.*?)"$/ do |roll_modifier|
32
+ @dice_roll.roll_modifier.should == roll_modifier.to_i
33
+ end
34
+
35
+ Given /^I want to roll a die with "(.*?)" sides$/ do |sides_on_die|
36
+ @die = PhyshRoller::Die.new(sides_on_die)
37
+ end
38
+
39
+ When /^I roll the die$/ do
40
+ @die.roll
41
+ end
42
+
43
+ Then /^the result should be between "(.*?)" and "(.*?)"$/ do |lower_limit, upper_limit|
44
+ @die.last_result.should be >= lower_limit.to_i
45
+ puts @die.last_result
46
+ @die.last_result.should be <= upper_limit.to_i
47
+ end
48
+
49
+ Given /^I have a dice roll string described by "(.*?)"$/ do |dice_roll_string|
50
+ @dice_roll = PhyshRoller::DiceRoll.new(dice_roll_string, output)
51
+ end
52
+
53
+ When /^I roll the set of dice$/ do
54
+ @dice_roll.roll_dice
55
+ end
56
+
57
+ Then /^I should see "(.*?)"$/ do |message|
58
+ @output.messages.should include message
59
+ end
60
+
61
+ Then /^the result should have "(.*?)"$/ do |number_of_dice|
62
+ @dice_roll.results[:dice_rolls].size.should == number_of_dice.to_i
63
+ end
64
+
65
+ Then /^each die roll should be equal to or greater than "(.*?)"$/ do |lower_limit|
66
+ @dice_roll.results[:dice_rolls].each { |roll| roll.should be >= lower_limit.to_i }
67
+ end
68
+
69
+ Then /^each die roll should be equal to or less than "(.*?)"$/ do |upper_limit|
70
+ @dice_roll.results[:dice_rolls].each { |roll| roll.should be <= upper_limit.to_i }
71
+ end
72
+
73
+ Then /^the total should be equal to or greater than "(.*?)"$/ do |lowest_total|
74
+ @dice_roll.results[:sum].should be >= lowest_total.to_i
75
+ end
76
+
77
+ Then /^the total should be equal to or less than "(.*?)"$/ do |highest_total|
78
+ @dice_roll.results[:sum].should be <= highest_total.to_i
79
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
2
+
3
+ require 'physh_roller'
@@ -0,0 +1,2 @@
1
+ require_relative 'physh_roller/dice_roll'
2
+ require_relative 'physh_roller/die'
@@ -0,0 +1,62 @@
1
+ module PhyshRoller
2
+ class DiceRoll
3
+ DICE_ROLL_REGEXP = /\A(\d+)?d(\d+)([\+-]\d*)?\z/i
4
+
5
+ attr_reader :dice_roll_string, :sides_on_dice, :roll_modifier,
6
+ :dice_array
7
+
8
+ def initialize(dice_roll_string, output=$stdout)
9
+ @output = output
10
+ @dice_roll_string = dice_roll_string.downcase.gsub(' ', '')
11
+ if valid_dice_roll_string?
12
+ set_default_values
13
+ add_dice_to_dice_array(@number_of_dice, @sides_on_dice)
14
+ else
15
+ raise ArgumentError.new('Invalid dice roll string.')
16
+ end
17
+ end
18
+
19
+ def valid_dice_roll_string?
20
+ !!(@dice_roll_string =~ DICE_ROLL_REGEXP)
21
+ end
22
+
23
+ def set_default_values
24
+ @number_of_dice, @sides_on_dice, @roll_modifier =
25
+ DICE_ROLL_REGEXP.match(@dice_roll_string).captures.map { |capture| capture.to_i }
26
+ @number_of_dice = 1 unless (@number_of_dice > 0)
27
+ @roll_modifier ||= 0
28
+ @dice_array = []
29
+ end
30
+
31
+ def roll_dice
32
+ @dice_array.each { |die| die.roll }
33
+ end
34
+
35
+ def add_dice_to_dice_array(number_of_dice, sides_on_dice)
36
+ number_of_dice.times do
37
+ die = Die.new(sides_on_dice)
38
+ @dice_array << die
39
+ end
40
+ end
41
+
42
+ def number_of_dice
43
+ @dice_array.size
44
+ end
45
+
46
+ def results
47
+ results_hash = {
48
+ :dice_roll => @dice_roll_string,
49
+ :dice_rolls => @dice_array.map { |die| die.last_result }.sort,
50
+ :roll_modifier => @roll_modifier,
51
+ :sum => (@dice_array.map { |die| die.last_result }.reduce(:+) + @roll_modifier)
52
+ }
53
+ end
54
+
55
+ def output_results
56
+ results = self.results
57
+ @output.puts "You rolled: #{results[:dice_roll]}"
58
+ @output.puts "Dice rolls: #{results[:dice_rolls].join(',')}"
59
+ @output.puts "Total: #{results[:sum]}"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,14 @@
1
+ module PhyshRoller
2
+ class Die
3
+ attr_reader :sides_on_die, :last_result
4
+
5
+ def initialize(sides_on_die)
6
+ @sides_on_die = sides_on_die.to_i
7
+ @last_result = 0
8
+ end
9
+
10
+ def roll
11
+ @last_result = rand(1..@sides_on_die)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ module PhyshRoller
5
+ describe DiceRoll do
6
+ before(:each) { @output = double('output').as_null_object }
7
+
8
+ describe "attributes and method tests" do
9
+ context "with a valid dice_roll_string" do
10
+ subject { DiceRoll.new("4d6-1", @output) }
11
+
12
+ it { should respond_to(:dice_roll_string) }
13
+ it { should respond_to(:number_of_dice) }
14
+ it { should respond_to(:sides_on_dice) }
15
+ it { should respond_to(:roll_modifier) }
16
+ it { should respond_to(:dice_array) }
17
+ it { should respond_to(:results) }
18
+ end
19
+
20
+ context "with an invalid dice_roll_string" do
21
+ it "should raise an error" do
22
+ expect { DiceRoll.new("this_is_not_a_valid_dice_roll_string", @output) }.to \
23
+ raise_error(ArgumentError, 'Invalid dice roll string.')
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "#new" do
29
+ subject do
30
+ DiceRoll.new(passed_dice_roll_string, @output)
31
+ end
32
+
33
+ context "passed '3d6-1' as dice roll string" do
34
+ let(:passed_dice_roll_string) { "3d6-1" }
35
+ its(:dice_roll_string) { should == "3d6-1" }
36
+ its(:number_of_dice) { should == 3 }
37
+ its(:sides_on_dice) { should == 6 }
38
+ its(:roll_modifier) { should == -1 }
39
+ end
40
+
41
+ context "passed 'D4' as dice roll string" do
42
+ let(:passed_dice_roll_string) { "D4" }
43
+ its(:dice_roll_string) { should == "d4" }
44
+ its(:number_of_dice) { should == 1 }
45
+ its(:sides_on_dice) { should == 4 }
46
+ its(:roll_modifier) { should == 0 }
47
+ end
48
+ end
49
+
50
+ describe "#valid_dice_roll_string?" do
51
+ context "with a valid dice_roll_string" do
52
+ subject { DiceRoll.new("12d20-10", @output) }
53
+
54
+ its(:valid_dice_roll_string?) { should be_true }
55
+ end
56
+
57
+ context "with an invalid dice_roll_string" do
58
+ it "should raise an error" do
59
+ expect { DiceRoll.new("this_is_not_a_valid_dice_roll_string", @output) }.to \
60
+ raise_error(ArgumentError, 'Invalid dice roll string.')
61
+ end
62
+ end
63
+ end
64
+
65
+ describe "#roll_dice" do
66
+ context "passed '3D6-1' as the dice_roll_string" do
67
+ before(:each) { @dice_roll = DiceRoll.new("3D6-1", @output) }
68
+ after(:each) { @dice_roll.roll_dice }
69
+
70
+ it "should roll each die in :dice_array" do
71
+ @dice_roll.dice_array.each do |die|
72
+ die.should_receive(:roll)
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "#add_dice_to_dice_array" do
79
+ before(:each) { @dice_roll = DiceRoll.new("3d6-1", @output) }
80
+
81
+ context "with number_of_dice == 1 and sides_on_dice == 6" do
82
+ it "should add another die to the :dice-array" do
83
+ expect { @dice_roll.add_dice_to_dice_array(1, 6) }.to \
84
+ change { @dice_roll.dice_array.size }.by(1)
85
+ end
86
+ end
87
+
88
+ context "with number_of_dice == 3 and sides_on_dice == 6" do
89
+ it "should add another die to the :dice-array" do
90
+ expect { @dice_roll.add_dice_to_dice_array(3, 6) }.to \
91
+ change { @dice_roll.dice_array.size }.by(3)
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "#results" do
97
+ context "before rolling the dice '99d20-10'" do
98
+ subject { DiceRoll.new("99d20-10", @output).results }
99
+
100
+ it { should be_a_kind_of(Hash) }
101
+ its([:dice_roll]) { should eq("99d20-10") }
102
+ its([:dice_rolls]) { should have(99).dice }
103
+ its([:roll_modifier]) { should == -10 }
104
+ its([:sum]) { should be == -10 }
105
+ end
106
+
107
+ context "after rolling the dice '100d20+42'" do
108
+ before(:each) do
109
+ @dice_roll = DiceRoll.new("100d20+42", @output)
110
+ @dice_roll.roll_dice
111
+ end
112
+
113
+ subject { @dice_roll.results }
114
+
115
+ it { should be_a_kind_of(Hash) }
116
+ its([:dice_roll]) { should eq("100d20+42") }
117
+ its([:dice_rolls]) { should have(100).dice }
118
+ its([:roll_modifier]) { should == 42 }
119
+ its([:sum]) { should be >= 142 }
120
+ its([:sum]) { should be <= 2042 }
121
+ end
122
+ end
123
+
124
+ describe "#output_results" do
125
+ before(:each) do
126
+ @dice_roll = DiceRoll.new("24d3-23", @output)
127
+ @dice_roll.roll_dice
128
+ end
129
+
130
+ after(:each) { @dice_roll.output_results }
131
+
132
+ it "should output the dice roll string" do
133
+ @output.should_receive(:puts).with('You rolled: 24d3-23')
134
+ end
135
+
136
+ it "should output the dice rolls" do
137
+ @output.should_receive(:puts).with(/\ADice rolls: (\d,)+\d\z/)
138
+ end
139
+
140
+ it "should output the total" do
141
+ @output.should_receive(:puts).with(/\ATotal: \d+\z/)
142
+ end
143
+ end
144
+ end
145
+ end