diceroller 0.0.2
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/.gitignore +2 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/README.txt +8 -0
- data/Rakefile +2 -0
- data/lib/diceroller.rb +1 -0
- data/lib/roll.rb +48 -0
- data/lib/roller/version.rb +3 -0
- data/roller.gemspec +24 -0
- data/spec/roll_spec.rb +82 -0
- metadata +78 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color -f n
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
roller (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
rspec (2.6.0)
|
11
|
+
rspec-core (~> 2.6.0)
|
12
|
+
rspec-expectations (~> 2.6.0)
|
13
|
+
rspec-mocks (~> 2.6.0)
|
14
|
+
rspec-core (2.6.0)
|
15
|
+
rspec-expectations (2.6.0)
|
16
|
+
diff-lcs (~> 1.1.2)
|
17
|
+
rspec-mocks (2.6.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
roller!
|
24
|
+
rspec (>= 2.6.0)
|
data/README.txt
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
The goal of this project is to provide an API for handling dice rolls in an RPG.
|
2
|
+
|
3
|
+
Currently it supports the following:
|
4
|
+
|
5
|
+
- Creating a new roll with dice of one size and a number of dice
|
6
|
+
- Rerolling all dice of a certain value
|
7
|
+
- Exploding all dice of a certain value (this creates a die with a value of original roll + the value of another die)
|
8
|
+
This makes it possible to roll a 16 on a ten-sided die
|
data/Rakefile
ADDED
data/lib/diceroller.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'roll'
|
data/lib/roll.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
class Roll
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
def initialize(num_of_dice, options = { :die_size => 10, :rand_generator => Random})
|
5
|
+
@options = options
|
6
|
+
@rand_generator = @options[:rand_generator]
|
7
|
+
@dice = roll(num_of_dice)
|
8
|
+
end
|
9
|
+
|
10
|
+
def reroll(*values)
|
11
|
+
each_die_that_matches(values) { roll_die }
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def explode(*values)
|
16
|
+
each_die_that_matches(values) { |die| last_die = roll_die; [die + last_die, last_die] }
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(other)
|
21
|
+
@dice == other
|
22
|
+
end
|
23
|
+
|
24
|
+
def each
|
25
|
+
@dice.each { |die| yield die }
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def roll(number)
|
31
|
+
number.times.collect { roll_die }
|
32
|
+
end
|
33
|
+
|
34
|
+
def roll_die
|
35
|
+
@rand_generator.rand(@options[:die_size])
|
36
|
+
end
|
37
|
+
|
38
|
+
def each_die_that_matches(values)
|
39
|
+
@dice.map! do |die|
|
40
|
+
last_die = die
|
41
|
+
while values.any? { |value| value == last_die}
|
42
|
+
die, last_die = yield(die)
|
43
|
+
last_die = (last_die.nil? && die) || last_die
|
44
|
+
end
|
45
|
+
die
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/roller.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "roller/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "diceroller"
|
7
|
+
s.version = Roller::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Edward Monical-Vuylsteke"]
|
10
|
+
s.email = ["solrane.altari@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/emonical/roller"
|
12
|
+
|
13
|
+
s.summary = %q{Dice roller for roleplaying games}
|
14
|
+
s.description = %q{A dice rolling gem that provides an effective
|
15
|
+
and hopefully extensible API to meet the extensive
|
16
|
+
needs of the roleplaying game community}
|
17
|
+
|
18
|
+
s.add_development_dependency "rspec", ">= 2.6.0"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
data/spec/roll_spec.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'roll'
|
3
|
+
|
4
|
+
describe Roll do
|
5
|
+
|
6
|
+
let(:rand_generator) do
|
7
|
+
rand_generator = double('rand_gen').as_null_object
|
8
|
+
rand_generator.stub(:rand).and_return(1,2,3,4,5,6,7,8,9,10)
|
9
|
+
rand_generator
|
10
|
+
end
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
unless Roll.respond_to?(:size)
|
14
|
+
class Roll
|
15
|
+
def size
|
16
|
+
@dice.count
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "roll" do
|
23
|
+
it "should have <parameters> elements" do
|
24
|
+
roll = Roll.new(2, rand_generator)
|
25
|
+
roll.should have(2).items
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns a list of numbers" do
|
29
|
+
roll = Roll.new(10, rand_generator)
|
30
|
+
roll.should == [1,2,3,4,5,6,7,8,9,10]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "calls Random once for each call" do
|
34
|
+
Random.should_receive(:rand).exactly(5)
|
35
|
+
roll = Roll.new(5)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "calls Random with default value of 10 when no options provided" do
|
39
|
+
Random.should_receive(:rand).with(10).exactly(5)
|
40
|
+
roll = Roll.new(5)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "reroll" do
|
45
|
+
it "rerolls all dice that match the parameter" do
|
46
|
+
roll = Roll.new(5, rand_generator)
|
47
|
+
roll.reroll(3).should == [1,2,6,4,5]
|
48
|
+
end
|
49
|
+
|
50
|
+
it "rerolls all dice that match any of the parameters" do
|
51
|
+
roll = Roll.new(5, rand_generator)
|
52
|
+
roll.reroll(3,4).should == [1,2,6,7,5]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "applies until it can't" do
|
56
|
+
rand_generator = double('rand_gen').as_null_object
|
57
|
+
rand_generator.stub(:rand).and_return(1,1,1,4)
|
58
|
+
roll = Roll.new(1, rand_generator)
|
59
|
+
roll.reroll(1).should == [4]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "explode" do
|
64
|
+
it "adds die to the result of all dice that match the parameter" do
|
65
|
+
roll = Roll.new(5, rand_generator)
|
66
|
+
roll.explode(3).should == [1,2,9,4,5]
|
67
|
+
end
|
68
|
+
|
69
|
+
it "adds die to the result of all dice that match any of the parameters" do
|
70
|
+
roll = Roll.new(5, rand_generator)
|
71
|
+
roll.explode(3,4).should == [1,2,9,11,5]
|
72
|
+
end
|
73
|
+
|
74
|
+
it "applies until it can't" do
|
75
|
+
rand_generator = double('rand_gen').as_null_object
|
76
|
+
rand_generator.stub(:rand).and_return(10,10,10,9)
|
77
|
+
roll = Roll.new(1, rand_generator)
|
78
|
+
roll.explode(10).should == [39]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: diceroller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Edward Monical-Vuylsteke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-06 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.6.0
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
description: |-
|
27
|
+
A dice rolling gem that provides an effective
|
28
|
+
and hopefully extensible API to meet the extensive
|
29
|
+
needs of the roleplaying game community
|
30
|
+
email:
|
31
|
+
- solrane.altari@gmail.com
|
32
|
+
executables: []
|
33
|
+
|
34
|
+
extensions: []
|
35
|
+
|
36
|
+
extra_rdoc_files: []
|
37
|
+
|
38
|
+
files:
|
39
|
+
- .gitignore
|
40
|
+
- .rspec
|
41
|
+
- Gemfile
|
42
|
+
- Gemfile.lock
|
43
|
+
- README.txt
|
44
|
+
- Rakefile
|
45
|
+
- lib/diceroller.rb
|
46
|
+
- lib/roll.rb
|
47
|
+
- lib/roller/version.rb
|
48
|
+
- roller.gemspec
|
49
|
+
- spec/roll_spec.rb
|
50
|
+
homepage: https://github.com/emonical/roller
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Dice roller for roleplaying games
|
77
|
+
test_files: []
|
78
|
+
|