class Rollable::Dice

Overview

Not a front class. It is used to represent a tuple of die type and die amount

A Dice is a amount of Die. It is rollable exactly like a classic Die

It is also possible to get the details of a roll, using the methods .min_details, .max_details, .average_details, .test_details

Example:

d = Dice.parse "2d6"
d.min         # => 2
d.max         # => 12
d.average     # => 7
d.min_details # => [1, 1]
d.test        # => the sum of 2 random values between 1..6

Defined in:

rollable/dice.cr
rollable/dice/parse_wrap.cr

Constant Summary

MAX = 1000

Constructors

Class Method Summary

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(count : Int32, die_type : Int32, exploding : Bool = false, drop : Int32 = 0) #

Create a Dice with "die_type" faces.


[View source]
def self.new(count : Int32, die : Rollable::Die, drop : Int32 = 0) #

[View source]
def self.parse(str : String, strict = true) : Dice #

Returns the Dice parsed. (see #parse_string)


[View source]

Class Method Detail

def self.consume(str : String, &) : String | Nil #

Returns the unconsumed string.

Parse str, and yield a Dice parsed. It does not requires to be a full valid string (see #parse when strict is false).

rest = Dice.consume("1d6+2") do |dice|
  # dice = Dice.new(1, Die.new(1..6))
end
# rest = "+2"

[View source]
def self.parse(str : String, strict = true, &) : String #

Return a valid string parsed from str. (see #parse_string)

Yields the Dice parsed from str. Then, it returns the string read. If strict is false, only the valid string is returned.

Dice.parse("1d6") {|dice| dice.roll } => "1d6"

[View source]

Instance Method Detail

def <(right : Dice) #

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

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

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

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

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

[View source]
def average : Float64 #

[View source]
def average_details : Array(Float64) #

[View source]
def clone #

[View source]
def count : Int32 #

[View source]
def count=(count : Int32) #

[View source]
def count_after_drop #

[View source]
def die : Rollable::Die #

[View source]
def drop : Int32 #

[View source]
def fixed?(*args, **options) #

[View source]
def fixed?(*args, **options, &) #

[View source]
def max : Int32 #

[View source]
def max_details : Array(Int32) #

[View source]
def min : Int32 #

[View source]
def min_details : Array(Int32) #

[View source]
def negative?(*args, **options) #

[View source]
def negative?(*args, **options, &) #

[View source]
def reverse : Dice #

Reverse the Die of the Dice.

Example:

Dice.parse("1d6").reverse # => -1d6

[View source]
def reverse! #

[View source]
def test : Int32 #

Roll an amount of Dice as specified, and return the sum


[View source]
def test_details : Array(Int32) #

Roll an amount of Dice as specified, and return the values


[View source]
def test_details_with_drop : Tuple(Array(Int32), Array(Int32)) #

Roll an amount of Dice as specified, and return the values along with the values that would be dropped


[View source]
def to_s : String #

Return a string which represents the Dice

  • If the value is fixed (n..n), then it return the @count * value
  • Else, it just add the count before the Dice like "{count}{dice.to_s}"

[View source]