Class: Gambit::Tools::Dice
- Inherits:
-
Object
- Object
- Gambit::Tools::Dice
- Includes:
- Enumerable, Viewable
- Defined in:
- lib/gambit/tools/dice.rb
Overview
An object for managing a roll of dice.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiply the sum() of this roll to an Integer.
-
#+(other) ⇒ Object
Add the sum() of this roll to an Integer.
-
#-(other) ⇒ Object
Subtract the sum() of this roll from an Integer.
-
#/(other) ⇒ Object
Divide the sum() of this roll by an Integer.
-
#[](index) ⇒ Object
Fetch the die at the provided index.
-
#coerce(other) ⇒ Object
Support for using Dice objects in ordinary Ruby math.
-
#count(pips) ⇒ Object
Returns a count of all dice in the roll matching the provided pips.
-
#each(&block) ⇒ Object
Iterate over each die in the roll.
-
#fail(maximum) ⇒ Object
Count the number of “failure” dice in this roll.
-
#initialize(*args) ⇒ Dice
constructor
Creates a new Dice object, rolling the indicated count of the indicated sided dice.
-
#matches?(*pattern) ⇒ Boolean
A call to matches?() allows pattern matching for dice rolls.
-
#reroll(*indices) ⇒ Object
A call to this method will reroll the indicated dice.
-
#success(minimum) ⇒ Object
Count the number of “success” dice in this roll.
-
#sum(pips = nil) ⇒ Object
Returns a total sum of all dice in the roll matching the provided pips.
Methods included from Viewable
Constructor Details
#initialize(*args) ⇒ Dice
Creates a new Dice object, rolling the indicated count of the indicated sided dice. The sides on a die need not be realistic, so this object can also be used to manage a coin toss (a two-sided die).
:call-seq:
Dice.new(count, sides) -> count_d_sides_roll
Dice.new(sides) -> one_d_sides_roll
Dice.new() -> one_d_six_roll
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gambit/tools/dice.rb', line 41 def initialize( *args ) @dice, @sides = if args.size == 2 args elsif args.size == 1 [1, args.first] else [1, 6] end @roll = Array.new(@dice) { rand(@sides) + 1 } end |
Instance Method Details
#*(other) ⇒ Object
Multiply the sum() of this roll to an Integer.
154 |
# File 'lib/gambit/tools/dice.rb', line 154 def *( other ) sum * other end |
#+(other) ⇒ Object
Add the sum() of this roll to an Integer.
150 |
# File 'lib/gambit/tools/dice.rb', line 150 def +( other ) sum + other end |
#-(other) ⇒ Object
Subtract the sum() of this roll from an Integer.
152 |
# File 'lib/gambit/tools/dice.rb', line 152 def -( other ) sum - other end |
#/(other) ⇒ Object
Divide the sum() of this roll by an Integer.
156 |
# File 'lib/gambit/tools/dice.rb', line 156 def /( other ) sum / other end |
#[](index) ⇒ Object
Fetch the die at the provided index.
54 55 56 |
# File 'lib/gambit/tools/dice.rb', line 54 def []( index ) @roll[index] end |
#coerce(other) ⇒ Object
Support for using Dice objects in ordinary Ruby math. Rolls are added to an Integer or Float by sum().
162 163 164 165 166 167 168 |
# File 'lib/gambit/tools/dice.rb', line 162 def coerce( other ) if other.is_a? Integer [other, self.sum] else [Float(other), Float(self.sum)] end end |
#count(pips) ⇒ Object
Returns a count of all dice in the roll matching the provided pips.
62 63 64 65 66 |
# File 'lib/gambit/tools/dice.rb', line 62 def count( pips ) @roll.inject(0) do |total, die| if die == pips then total + 1 else total end end end |
#each(&block) ⇒ Object
Iterate over each die in the roll.
71 72 73 |
# File 'lib/gambit/tools/dice.rb', line 71 def each( &block ) @roll.each(&block) end |
#fail(maximum) ⇒ Object
Count the number of “failure” dice in this roll. A die is a failure if it is less than or equal to the provided maximum.
79 80 81 82 83 |
# File 'lib/gambit/tools/dice.rb', line 79 def fail( maximum ) @roll.inject(0) do |total, die| if die <= maximum then total + 1 else total end end end |
#matches?(*pattern) ⇒ Boolean
A call to matches?() allows pattern matching for dice rolls. A pattern may contain single-letter Strings and/or Integers.
Strings are used to locate recurring die patterns. For example, one could check for a full house with dice.matches?(*w{x x x y y})
.
Integers can be used to locate sequences of dice. For example, one could check for a small straight with dice.matches?(*(1..4).to_a)
.
97 98 99 100 |
# File 'lib/gambit/tools/dice.rb', line 97 def matches?( *pattern ) digits, letters = pattern.partition { |e| e.is_a?(Integer) } matches_digits?(digits) and matches_letters?(letters) end |
#reroll(*indices) ⇒ Object
A call to this method will reroll the indicated dice. Dice can be selected by block or indices.
:call-seq:
reroll(*indices) -> dice
reroll() { |die| ... } -> dice
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/gambit/tools/dice.rb', line 110 def reroll( *indices ) if block_given? @roll.each_with_index do |die, i| @roll[i] = rand(6) + 1 if yield(die) end elsif indices.empty? @roll = Array.new(@dice) { rand(@sides) + 1 } else indices.each { |i| @roll[i] = rand(6) + 1 } end self end |
#success(minimum) ⇒ Object
Count the number of “success” dice in this roll. A die is a success if it is greater than or equal to the provided minimum.
129 130 131 132 133 |
# File 'lib/gambit/tools/dice.rb', line 129 def success( minimum ) @roll.inject(0) do |total, die| if die >= minimum then total + 1 else total end end end |
#sum(pips = nil) ⇒ Object
Returns a total sum of all dice in the roll matching the provided pips. If pips is nil
(the default), all dice are totaled.
139 140 141 142 143 144 145 146 147 |
# File 'lib/gambit/tools/dice.rb', line 139 def sum( pips = nil ) if pips @roll.inject(0) do |total, die| if die == pips then total + die else total end end else @roll.inject(0) { |total, die| total + die } end end |