Class: GamesDice::Die
- Inherits:
-
Object
- Object
- GamesDice::Die
- Defined in:
- lib/games_dice/die.rb
Overview
This class models the simplest, most-familiar kind of die.
An object of the class represents a basic die that rolls 1..#sides, with equal weighting for each value.
Instance Attribute Summary collapse
-
#maps ⇒ nil
readonly
Rules for when to map return value of this die.
-
#max ⇒ Integer
readonly
Maximum possible result from a call to #roll.
-
#min ⇒ Integer
readonly
Minimum possible result from a call to #roll.
-
#prng ⇒ Object
readonly
Random number generator as supplied to constructor, may be nil.
-
#rerolls ⇒ nil
readonly
Rules for when to re-roll this die.
-
#result ⇒ Integer
readonly
Result of last call to #roll, nil if no call made yet.
-
#sides ⇒ Integer
readonly
Number of sides on simulated die.
Instance Method Summary collapse
-
#all_values ⇒ Array<Integer>
All potential results from the die.
-
#each_value {|result| ... } ⇒ GamesDice::Die
Iterates through all possible results on die.
-
#initialize(sides, prng = nil) ⇒ GamesDice::Die
constructor
Creates new instance of GamesDice::Die.
-
#probabilities ⇒ GamesDice::Probabilities
Calculates probability distribution for this die.
-
#roll ⇒ Integer
Simulates rolling the die.
Constructor Details
#initialize(sides, prng = nil) ⇒ GamesDice::Die
Creates new instance of GamesDice::Die
28 29 30 31 32 33 34 35 |
# File 'lib/games_dice/die.rb', line 28 def initialize(sides, prng = nil) @sides = Integer(sides) raise ArgumentError, "sides value #{sides} is too low, it must be 1 or greater" if @sides < 1 raise ArgumentError, 'prng does not support the rand() method' if prng && !prng.respond_to?(:rand) @prng = prng @result = nil end |
Instance Attribute Details
#maps ⇒ nil (readonly)
Rules for when to map return value of this die.
97 98 99 |
# File 'lib/games_dice/die.rb', line 97 def maps nil end |
#max ⇒ Integer (readonly)
Returns maximum possible result from a call to #roll.
54 55 56 |
# File 'lib/games_dice/die.rb', line 54 def max @sides end |
#min ⇒ Integer (readonly)
Returns minimum possible result from a call to #roll.
48 49 50 |
# File 'lib/games_dice/die.rb', line 48 def min 1 end |
#prng ⇒ Object (readonly)
Returns random number generator as supplied to constructor, may be nil.
44 45 46 |
# File 'lib/games_dice/die.rb', line 44 def prng @prng end |
#rerolls ⇒ nil (readonly)
Rules for when to re-roll this die.
90 91 92 |
# File 'lib/games_dice/die.rb', line 90 def rerolls nil end |
#result ⇒ Integer (readonly)
Returns result of last call to #roll, nil if no call made yet.
41 42 43 |
# File 'lib/games_dice/die.rb', line 41 def result @result end |
#sides ⇒ Integer (readonly)
Returns number of sides on simulated die.
38 39 40 |
# File 'lib/games_dice/die.rb', line 38 def sides @sides end |
Instance Method Details
#all_values ⇒ Array<Integer>
Returns All potential results from the die.
83 84 85 |
# File 'lib/games_dice/die.rb', line 83 def all_values (1..@sides).to_a end |
#each_value {|result| ... } ⇒ GamesDice::Die
Iterates through all possible results on die.
77 78 79 80 |
# File 'lib/games_dice/die.rb', line 77 def each_value(&) (1..@sides).each(&) self end |
#probabilities ⇒ GamesDice::Probabilities
Calculates probability distribution for this die.
60 61 62 |
# File 'lib/games_dice/die.rb', line 60 def probabilities @probabilities ||= GamesDice::Probabilities.for_fair_die(@sides) end |
#roll ⇒ Integer
Simulates rolling the die
66 67 68 69 70 71 72 |
# File 'lib/games_dice/die.rb', line 66 def roll @result = if @prng @prng.rand(@sides) + 1 else rand(@sides) + 1 end end |