rpg_dice 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.0"
12
+ gem "rcov", ">= 0"
13
+ end
14
+
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.6.2)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.9.2)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.6.0)
27
+ rcov
28
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Will Gretz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = rpg_dice
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to rpg_dice
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Will Gretz. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rpg_dice"
18
+ gem.homepage = "http://github.com/WGretz/rpg-dice"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{An RPG Dice Library}
21
+ gem.description = %Q{Ruby code to roll varying amounts of polyhedral.}
22
+ gem.email = "me@willgretz.com"
23
+ gem.authors = ["Will Gretz"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ RDoc::Task.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "rpg-dice #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/lib/lib/dice.rb ADDED
@@ -0,0 +1,63 @@
1
+ class Dice
2
+
3
+ attr_accessor :num_of_dice, :sides
4
+
5
+ @@max_dice = nil
6
+ @@max_sides = nil
7
+
8
+
9
+ def initialize(num_of_dice,sides, options={})
10
+ options = filter_options(options)
11
+ raise ArgumentError, "Your sides exceeds the maximum number of sides allowed" if @@max_sides && sides > @@max_sides
12
+ raise ArgumentError, "Your dice exceeds the maximum dice allowed" if @@max_dice && num_of_dice > @@max_dice
13
+ @num_of_dice = num_of_dice
14
+ @sides = sides
15
+ end
16
+
17
+ def results
18
+ return @results.inject {|sum,x| sum+=x} if @results
19
+ @results = []
20
+ @num_of_dice.times do
21
+ @results << rand(sides) + 1
22
+ end
23
+ return @results.inject {|sum,x| sum+=x}
24
+ end
25
+
26
+ def reroll
27
+ @results = nil
28
+ return results
29
+ end
30
+
31
+ # Parses a dice string and returns a dice object
32
+ # More documentation to come later
33
+
34
+ def self.parse(string)
35
+ dice_regex = /(?<num_of_dice>\d*)d(?<num_of_sides>\d+)/
36
+ dice = dice_regex.match(string)
37
+ raise ArgumentError, "unable to parse dice string" if dice.nil?
38
+ num_of_dice = dice[:num_of_dice].to_i
39
+ num_of_dice = 1 if num_of_dice == 0
40
+ num_of_sides = dice[:num_of_sides].to_i
41
+ dice = Dice.new(num_of_dice,num_of_sides)
42
+ return dice
43
+ end
44
+
45
+ def self.max_dice=( value )
46
+ @@max_dice = value
47
+ end
48
+
49
+ def self.max_sides=( value )
50
+ @@max_sides = value
51
+ end
52
+
53
+ private
54
+
55
+ def filter_options(options)
56
+ defaults = {:highest=>nil}
57
+ options.each do |key,vale|
58
+ raise ArgumentError, "#{key} is not a valid option" unless defaults.has_key? key
59
+ end
60
+ return defaults.merge(options)
61
+ end
62
+
63
+ end
data/lib/rpg_dice.rb ADDED
@@ -0,0 +1 @@
1
+ require "lib/dice"
data/rpg_dice.gemspec ADDED
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rpg_dice}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Will Gretz"]
12
+ s.date = %q{2011-06-12}
13
+ s.description = %q{Ruby code to roll varying amounts of polyhedral.}
14
+ s.email = %q{me@willgretz.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/lib/dice.rb",
28
+ "lib/rpg_dice.rb",
29
+ "rpg_dice.gemspec",
30
+ "spec/rpg-dice_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/WGretz/rpg-dice}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.6.2}
37
+ s.summary = %q{An RPG Dice Library}
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
46
+ s.add_development_dependency(%q<rcov>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
49
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
51
+ s.add_dependency(%q<rcov>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
57
+ s.add_dependency(%q<rcov>, [">= 0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Dice do
4
+
5
+ it "should take 2 arguments" do
6
+ @dice = Dice.new(1,2)
7
+ @dice.num_of_dice.should be 1
8
+ @dice.sides.should be 2
9
+ end
10
+
11
+ it "should accept an optional third hash of arguments" do
12
+ lambda{ @dice = Dice.new(4,6,{:highest=>3}) }.should_not raise_error
13
+ end
14
+
15
+ it "should raise an argument error if the option does not exist" do
16
+ lambda{ @dice = Dice.new(4,6,{:no_way_this_exists=>3}) }.should raise_error(ArgumentError)
17
+ end
18
+
19
+ it "should be able to return results" do
20
+ @dice = Dice.new(1,6)
21
+ @dice.results.should be > 0
22
+ @dice.results.should be < 7
23
+ end
24
+
25
+ it "should not change results" do
26
+ @dice = Dice.new(100,10)
27
+ a = @dice.results
28
+ b = @dice.results
29
+ c = @dice.results
30
+ d = @dice.results
31
+ a.should be b
32
+ a.should be c
33
+ a.should be d
34
+ end
35
+
36
+ it "should change results on a reroll" do
37
+ @dice = Dice.new(100,10)
38
+ a = @dice.results
39
+ b = @dice.reroll
40
+ a.should_not be b
41
+ end
42
+
43
+ it "should be able to parse a dice string" do
44
+ @dice = Dice.parse("1d6")
45
+ @dice.should be_an_instance_of(Dice)
46
+ @dice.sides.should be 6
47
+ @dice.num_of_dice.should be 1
48
+ end
49
+
50
+ it "should assume 1 dice if no number is specified" do
51
+ @dice = Dice.parse("d6")
52
+ @dice.num_of_dice.should be 1
53
+ @dice.sides.should be 6
54
+ end
55
+
56
+ it "should raise an error if it can't parse the dice" do
57
+ lambda{ @dice = Dice.parse("1d")}.should raise_error
58
+ end
59
+
60
+ describe "Max Values" do
61
+
62
+ it "should throw an error if number of dice exceeds max dice" do
63
+ Dice.max_dice = 1000
64
+ lambda{ Dice.new(1001,6) }.should raise_error
65
+ end
66
+
67
+ it "should throw an error if the sides exceeds the maximum number of sides" do
68
+ Dice.max_sides = 1000
69
+ lambda{ Dice.new(1,1001) }.should raise_error
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'rpg-dice'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rpg_dice
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Will Gretz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-12 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description: Ruby code to roll varying amounts of polyhedral.
61
+ email: me@willgretz.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.rdoc
75
+ - Rakefile
76
+ - VERSION
77
+ - lib/lib/dice.rb
78
+ - lib/rpg_dice.rb
79
+ - rpg_dice.gemspec
80
+ - spec/rpg-dice_spec.rb
81
+ - spec/spec_helper.rb
82
+ has_rdoc: true
83
+ homepage: http://github.com/WGretz/rpg-dice
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: -2869280757293228466
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.6.2
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: An RPG Dice Library
113
+ test_files: []
114
+