class Rollable::Die

Overview

Not a front class. It is used to represent a type of dice with faces

A Die is a range of Integer values. It is rollable.

Example:

d = Die.new(1..6)
d.min     # => 1
d.max     # => 6
d.average # => 3.5
d.test    # => a random value included in 1..6

TODO make it a Struct ?

Defined in:

rollable/die.cr

Constant Summary

EXPLODING_ITERATIONS = 4
MAX = 1000

Constructors

Instance Method Summary

Instance methods inherited from class Rollable::IsRollable

average : Float64 average, max : Int32 max, min : Int32 min, test : Int32 test

Constructor Detail

def self.new(nb_faces : Int32, exploding : Bool = false) #

[View source]
def self.new(faces : Range(Int32, Int32), exploding : Bool = false) #

[View source]

Instance Method Detail

def <(right : Die) #

[View source]
def <=(right : Die) #

[View source]
def <=>(right : Die) : Int32 #

[View source]
def ==(right : Die) #

[View source]
def >(right : Die) #

[View source]
def >=(right : Die) #

[View source]
def average : Float64 #

Mathematical expectation.

A d6 will have a expected value of 3.5


[View source]
def clone #

[View source]
def exploding : Bool #

[View source]
def faces : Range(Int32, Int32) #

[View source]
def fixed? #

[View source]
def like?(other : Die) #

[View source]
def max : Int32 #

[View source]
def min : Int32 #

[View source]
def negative? #

[View source]
def reverse : Die #

Reverse the values

Example:

Die.new(1..6).reverse # => Die.new -6..-1

[View source]
def reverse! #

[View source]
def size #

Number of faces of the Die


[View source]
def test : Int32 #

Return a random value in the range of the dice


[View source]
def to_s : String #

Return a string.

  • It may be a fixed value (n..n) => "#{n}"
  • It may be a dice (1..n) => "D#{n}"
  • Else, (a..b) => "D(#{a},#{b})"

[View source]