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
26 27 28 29 30 31 32 |
# File 'lib/games_dice/die.rb', line 26 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.
94 95 96 |
# File 'lib/games_dice/die.rb', line 94 def maps nil end |
#max ⇒ Integer (readonly)
Returns maximum possible result from a call to #roll.
51 52 53 |
# File 'lib/games_dice/die.rb', line 51 def max @sides end |
#min ⇒ Integer (readonly)
Returns minimum possible result from a call to #roll.
45 46 47 |
# File 'lib/games_dice/die.rb', line 45 def min 1 end |
#prng ⇒ Object (readonly)
Returns random number generator as supplied to constructor, may be nil.
41 42 43 |
# File 'lib/games_dice/die.rb', line 41 def prng @prng end |
#rerolls ⇒ nil (readonly)
Rules for when to re-roll this die.
87 88 89 |
# File 'lib/games_dice/die.rb', line 87 def rerolls nil end |
#result ⇒ Integer (readonly)
Returns result of last call to #roll, nil if no call made yet.
38 39 40 |
# File 'lib/games_dice/die.rb', line 38 def result @result end |
#sides ⇒ Integer (readonly)
Returns number of sides on simulated die.
35 36 37 |
# File 'lib/games_dice/die.rb', line 35 def sides @sides end |
Instance Method Details
#all_values ⇒ Array<Integer>
Returns All potential results from the die.
80 81 82 |
# File 'lib/games_dice/die.rb', line 80 def all_values (1..@sides).to_a end |
#each_value {|result| ... } ⇒ GamesDice::Die
Iterates through all possible results on die.
74 75 76 77 |
# File 'lib/games_dice/die.rb', line 74 def each_value (1..@sides).each { |r| yield(r) } self end |
#probabilities ⇒ GamesDice::Probabilities
Calculates probability distribution for this die.
57 58 59 |
# File 'lib/games_dice/die.rb', line 57 def probabilities @probabilities ||= GamesDice::Probabilities.for_fair_die( @sides ) end |
#roll ⇒ Integer
Simulates rolling the die
63 64 65 66 67 68 69 |
# File 'lib/games_dice/die.rb', line 63 def roll if @prng @result = @prng.rand(@sides) + 1 else @result = rand(@sides) + 1 end end |