Class: CARPS::Dice::D
- Inherits:
-
Object
- Object
- CARPS::Dice::D
- Defined in:
- lib/carps/mod/dice.rb
Overview
A dice
Note that the methods here - including +, *, etc - update the dice’s internal state, and do not return a new dice.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiply by a number or another dice.
-
#+(other) ⇒ Object
Add a number or another dice.
-
#-(other) ⇒ Object
Subtract a number or another dice.
-
#/(other) ⇒ Object
Divide by a number or another dice.
-
#in_range(range, output) ⇒ Object
Directly inspect the range and supply a number or a dice as output, if the result is in that range.
-
#initialize(i) ⇒ D
constructor
Create the dice from an integer, the number of sides.
-
#is(compare, other, output) ⇒ Object
If the result matches a binary comparison, then return this result.
-
#odds ⇒ Object
Output the odds.
-
#roll ⇒ Object
Roll the dice.
-
#rolls ⇒ Object
Output the roll table.
-
#weights ⇒ Object
Output the weights table.
Constructor Details
#initialize(i) ⇒ D
Create the dice from an integer, the number of sides
The dice will have sides numbered from 1 to i.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/carps/mod/dice.rb', line 47 def initialize i @rolls = {} @weights = {} fair_weight = 1.0 / i i.times do |n| n = n + 1 @rolls[n] = n @weights[n] = 1.0 / i end end |
Instance Method Details
#*(other) ⇒ Object
Multiply by a number or another dice
134 135 136 137 138 139 140 |
# File 'lib/carps/mod/dice.rb', line 134 def * other if is_num other mul_num other else mul_dice other end end |
#+(other) ⇒ Object
Add a number or another dice
116 117 118 119 120 121 122 |
# File 'lib/carps/mod/dice.rb', line 116 def + other if is_num other add_num other else add_dice other end end |
#-(other) ⇒ Object
Subtract a number or another dice
125 126 127 128 129 130 131 |
# File 'lib/carps/mod/dice.rb', line 125 def - other if is_num other add_num (other * -1) else subtract_dice other end end |
#/(other) ⇒ Object
Divide by a number or another dice
143 144 145 146 147 148 149 |
# File 'lib/carps/mod/dice.rb', line 143 def / other if is_num other div_num other else div_dice other end end |
#in_range(range, output) ⇒ Object
Directly inspect the range and supply a number or a dice as output, if the result is in that range
152 153 154 155 156 157 158 |
# File 'lib/carps/mod/dice.rb', line 152 def in_range range, output if is_num output in_range_int range, output else in_range_dice range, output end end |
#is(compare, other, output) ⇒ Object
If the result matches a binary comparison, then return this result
compare is one of :<, :<=, :==, :>, :>=
other is an integer to compare with
165 166 167 168 169 170 171 172 |
# File 'lib/carps/mod/dice.rb', line 165 def is compare, other, output cmp = Dice::comparison_to_proc compare, other rng = find_range cmp if rng in_range rng, output end end |
#odds ⇒ Object
Output the odds
That is, a hash of results to the probabalistic weights of the possible results.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/carps/mod/dice.rb', line 92 def odds od = {} @weights.each do |roll, weight| result = @rolls[roll] if od.include? result od[result] = od[result] + weight else od[result] = weight end end od end |
#roll ⇒ Object
Roll the dice
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/carps/mod/dice.rb', line 59 def roll od = [] results = odds.to_a current = 0 # Build up range arrays corresponding to probabalistic weights until results.empty? # Manually insert a 1 at the end to make this numerically stable last = false if results.length == 1 last = true end result, weight = results.shift if last od.push [current..1, result] else new_top = current + weight od.push [current..new_top, result] current = new_top end end # Find the appropriate action r = rand od.each do |range, result| if range.include? r return result end end raise StandardError, "BUG: Dice has no result." end |
#rolls ⇒ Object
Output the roll table
111 112 113 |
# File 'lib/carps/mod/dice.rb', line 111 def rolls @rolls end |
#weights ⇒ Object
Output the weights table
106 107 108 |
# File 'lib/carps/mod/dice.rb', line 106 def weights @weights end |