dicechucker 0.6.0

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 ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ *.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Mark Tabler
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.md ADDED
@@ -0,0 +1,55 @@
1
+ Dicechucker
2
+ ===========
3
+
4
+ Dicechucker is a die-rolling library for Ruby that accepts standard die notation (XdY+Z). It can return results either as an integer total, an array of individual die rolls plus the modifier, or as a string describing results in English.
5
+
6
+ Basic Usage
7
+ -----------
8
+
9
+ Manually set a Dice object:
10
+ foo = Dicechucker::Dice.new(number_of_dice, sides_on_die, modifier)
11
+
12
+ Parse a string to make a Dice object:
13
+ foo = Dicechucker::Dice.parse(XdY+Z)`
14
+
15
+ Basic Rolls:
16
+ bar = foo.roll #total only
17
+ bar = foo.roll(true) #array of die rolls plus modifier
18
+ bar = foo.roll_english #string describing results`
19
+
20
+ Other Dice methods:
21
+ -------------------
22
+
23
+ check_dc(dc): rolls the dice and compares them to DC. Returns True if roll is greater than or equal to DC.
24
+
25
+ drop_high(number_to_drop = 1, individual_results = false): Rolls the dice, and drops the highest number_to_drop results. Returns either a total or an array of individual results.
26
+
27
+ drop_low(number_to_drop = 1, individual_results = false): Rolls the dice, and drops the lowest number_to_drop results. Returns either a total or an array of individual results.
28
+
29
+ explode(individual_results = false). Rolls the dice. For each die which has a maximum result (e.g., 6 on a d6), that result is kept and another die is rolled. Returns either a total or an array of individual results.
30
+
31
+ Examples
32
+ ------
33
+ To create a die set containing three six-sided dice and a modifier of one:
34
+ stat_dice = Dicechucker::Dice.new(3, 6, 1)
35
+
36
+ To parse standard notation for same:
37
+ stat_dice = Dicechucker::Dice.parse('3d6+1')
38
+
39
+ To roll the dice and get a total result (assume 4, 5, 6 rolled):
40
+ stat_dice.roll # => 16
41
+
42
+ To roll the dice and get individual dice and the modifier back:
43
+ stat_dice.roll(true) # => [4, 5, 6, 1]
44
+
45
+ To get a plain English result:
46
+ stat_dice.roll_english # => 'rolled 4, 5, 6 plus one for a total of 16.'
47
+
48
+ To roll 4d6+0 and drop the low die (results assume 3, 4, 5, 6 rolled):
49
+ easy_stat_dice = Dicechucker::Dice.parse('4d6')
50
+ stat = easy_stat_dice.drop_low # => 15
51
+
52
+ Copyright
53
+ ---------
54
+
55
+ Copyright (c) 2010 Mark Tabler. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dicechucker"
8
+ gem.summary = "dice notation (XdY+Z) library"
9
+ gem.description = "creates and rolls die sets using standard XdY+Z notation"
10
+ gem.email = "mark.tabler@fallingmanstudios.net"
11
+ gem.homepage = "http://github.com/marktabler/dicechucker"
12
+ gem.authors = ["Mark Tabler"]
13
+ gem.add_development_dependency "mocha", ">= 0"
14
+ end
15
+
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "dicechucker #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.0
data/bin/roll ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../lib/dicechucker.rb', __FILE__)
3
+
4
+ begin
5
+ dice = Dicechucker::Dice.parse(ARGV[0])
6
+ puts "You #{dice.roll_english}"
7
+ rescue => err
8
+ abort "#{err} -- aborting."
9
+ end
10
+
@@ -0,0 +1,71 @@
1
+ require File.expand_path('../game_logic.rb', __FILE__)
2
+
3
+ module Dicechucker
4
+
5
+ class Dice
6
+ include GameLogic
7
+
8
+ class NotationError < ArgumentError
9
+ end
10
+
11
+ PATTERN = /^(?:(?<dice>\d+)d)?(?<size>\d+)(?<mod>[+\-]\d+)?$/
12
+
13
+ attr_accessor :dice, :size, :mod
14
+
15
+ def self.parse(raw)
16
+ if (match = raw.match(PATTERN))
17
+ dice = Integer(match[:dice]) rescue 1
18
+ size = Integer(match[:size])
19
+ mod = Integer(match[:mod]) rescue 0
20
+ Dice.new(dice, size, mod)
21
+ else
22
+ raise NotationError, "Invalid die notation, #{raw}"
23
+ end
24
+ end
25
+
26
+ def initialize(dice, size, mod)
27
+ @dice = dice
28
+ @size = size
29
+ @mod = mod
30
+ end
31
+
32
+ def roll(individual_rolls = false)
33
+ dice = roll_dice
34
+ report(dice, individual_rolls)
35
+ end
36
+
37
+ def roll_english
38
+ dice = roll_dice
39
+ total = dice.inject(:+) + @mod
40
+ dice = dice.join(', ')
41
+ if @mod > 0
42
+ mod_english = "plus #{@mod} "
43
+ elsif @mod < 0
44
+ mod_english = "minus #{@mod.abs} "
45
+ else
46
+ mod_english = ''
47
+ end
48
+ "rolled #{dice} #{mod_english}for a total of #{total}."
49
+ end
50
+
51
+ def ==(other)
52
+ @dice == other.dice && @size == other.size && @mod == other.mod
53
+ end
54
+
55
+ private
56
+
57
+ def report(dice, individual_rolls)
58
+ if individual_rolls
59
+ dice << @mod
60
+ else
61
+ dice = dice.inject(:+) + @mod
62
+ end
63
+ dice
64
+ end
65
+
66
+ def roll_dice
67
+ Array.new(@dice) { (rand * @size + 1).to_i }
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,29 @@
1
+ module Dicechucker
2
+ module GameLogic
3
+
4
+ def check_dc(dc)
5
+ self.roll >= dc
6
+ end
7
+
8
+ def explode(individual_rolls = false)
9
+ dice = roll_dice
10
+ dice.each do |roll|
11
+ if roll == @size
12
+ dice << (rand(@size)+1)
13
+ end
14
+ end
15
+ report(dice, individual_rolls)
16
+ end
17
+
18
+ def drop_high(number_to_drop = 1, individual_rolls = false)
19
+ dice = roll_dice.sort.reverse.drop(number_to_drop)
20
+ report(dice, individual_rolls)
21
+ end
22
+
23
+ def drop_low(number_to_drop = 1, individual_rolls = false)
24
+ dice = roll_dice.sort.drop(number_to_drop)
25
+ report(dice, individual_rolls)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module Dicechucker
2
+ autoload :Dice, File.expand_path('../dicechucker/dice.rb', __FILE__)
3
+ autoload :GameLogic, File.expand_path('../dicechucker/game_logic.rb', __FILE__)
4
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'dicechucker'
2
+ require 'minitest/unit'
3
+ require 'mocha'
4
+ MiniTest::Unit.autorun
5
+
data/test/test_dice.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'helper'
2
+
3
+ class TestDice < MiniTest::Unit::TestCase
4
+ include Dicechucker
5
+
6
+ def setup
7
+ Dice.any_instance.stubs(:rand => 0.5)
8
+ end
9
+
10
+ def test_dice_parse_single_implied
11
+ first = Dice.new(1, 20, 0)
12
+ second = Dice.parse('20')
13
+ assert_equal first, second
14
+ end
15
+
16
+ def test_dice_parse_plural_positive_mod
17
+ first = Dice.new(2, 20, -4)
18
+ second = Dice.parse('2d20-4')
19
+ assert_equal first, second
20
+ end
21
+
22
+ def test_dice_parse_large_numbers
23
+ first = Dice.new(300, 5000, 2000)
24
+ second = Dice.parse('300d5000+2000')
25
+ assert_equal first, second
26
+ end
27
+
28
+ def test_dice_average_single
29
+ testroll = Dice.new(1, 6, 0)
30
+ assert_equal testroll.roll, 4
31
+
32
+ end
33
+
34
+ def test_dice_roll_plural
35
+ testroll = Dice.new(2, 6, 0)
36
+ assert_equal testroll.roll, 8
37
+ end
38
+
39
+ def test_dice_average_plural_mod
40
+ testroll = Dice.new(2, 6, 2)
41
+ assert_equal testroll.roll, 10
42
+ end
43
+
44
+ def test_dice_roll_english
45
+ testroll = Dice.new(2, 20, 4)
46
+ result = testroll.roll_english
47
+ sample = 'rolled 11, 11 plus 4 for a total of 26.'
48
+ assert_equal result, sample
49
+ end
50
+
51
+ end
52
+
@@ -0,0 +1,33 @@
1
+ require 'helper'
2
+
3
+ class TestLogic < MiniTest::Unit::TestCase
4
+ include Dicechucker
5
+
6
+ def setup
7
+ Dice.any_instance.stubs(:rand => 0.5)
8
+ end
9
+
10
+ def test_dice_drop_low
11
+ testroll = Dice.new(100, 6, 0)
12
+ result = testroll.drop_low(10, true)
13
+ assert_equal result.size, 91 #90 dice plus 1 modifier of 0
14
+ end
15
+
16
+ def test_dice_drop_high
17
+ testroll = Dice.new(100, 6, 0)
18
+ result = testroll.drop_high(10, true)
19
+ assert_equal result.size, 91 #90 dice plus 1 modifier of 0
20
+ end
21
+
22
+ def test_explode
23
+ testroll = Dice.new(100, 6, 0)
24
+ result = testroll.explode(true)
25
+ assert_equal result.size, 101 #all random rolls are 4, nothing explodes
26
+ end
27
+
28
+ def test_check_dc
29
+ testroll = Dice.new(1, 20, 2)
30
+ assert testroll.check_dc(14) == false
31
+ assert testroll.check_dc(13) == true
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dicechucker
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 6
8
+ - 0
9
+ version: 0.6.0
10
+ platform: ruby
11
+ authors:
12
+ - Mark Tabler
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-06 00:00:00 -07:00
18
+ default_executable: roll
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mocha
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: creates and rolls die sets using standard XdY+Z notation
34
+ email: mark.tabler@fallingmanstudios.net
35
+ executables:
36
+ - roll
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.md
42
+ files:
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - VERSION
48
+ - bin/roll
49
+ - lib/dicechucker.rb
50
+ - lib/dicechucker/dice.rb
51
+ - lib/dicechucker/game_logic.rb
52
+ - test/helper.rb
53
+ - test/test_dice.rb
54
+ - test/test_game_logic.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/marktabler/dicechucker
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: dice notation (XdY+Z) library
87
+ test_files:
88
+ - test/helper.rb
89
+ - test/test_game_logic.rb
90
+ - test/test_dice.rb