Module: GamesDice

Defined in:
lib/games_dice.rb,
lib/games_dice/die.rb,
lib/games_dice/dice.rb,
lib/games_dice/bunch.rb,
lib/games_dice/parser.rb,
lib/games_dice/marshal.rb,
lib/games_dice/version.rb,
lib/games_dice/map_rule.rb,
lib/games_dice/constants.rb,
lib/games_dice/die_result.rb,
lib/games_dice/complex_die.rb,
lib/games_dice/reroll_rule.rb,
lib/games_dice/bunch_helpers.rb,
lib/games_dice/complex_die_helpers.rb,
ext/games_dice/games_dice.c,
ext/games_dice/probabilities.c

Overview

GamesDice is a library for simulating dice combinations used in dice and board games.

Defined Under Namespace

Classes: Bunch, ComplexDie, Dice, Die, DieResult, MapRule, Parser, Probabilities, RerollRule

Constant Summary collapse

VERSION =

Current version of the gem.

'0.4.2'
REROLL_TYPES =

Reasons for making a reroll, and text explanation symbols for them

{
  basic: ',',
  reroll_add: '+',
  reroll_subtract: '-',
  reroll_replace: '|',
  reroll_use_best: '/',
  reroll_use_worst: '\\'
  # These are not yet implemented:
  # :reroll_new_die => '*',
  # :reroll_new_keeper => '*',
}.freeze

Class Method Summary collapse

Class Method Details

.create(dice_description, prng = nil) ⇒ GamesDice::Dice

Creates an instance of GamesDice::Dice from a string description.

Parameters:

  • dice_description (String)

    Uses a variation of common game notation, examples: '1d6', '3d8+1d4+7', '5d10k2'

  • prng (#rand) (defaults to: nil)

    Optional random number generator, default is to use Ruby's built-in #rand()

Returns:



23
24
25
26
27
# File 'lib/games_dice.rb', line 23

def self.create(dice_description, prng = nil)
  parsed = parser.parse(dice_description)
  parsed[:bunches].each { |bunch| bunch.merge!(prng: prng) } if prng
  GamesDice::Dice.new(parsed[:bunches], parsed[:offset])
end

.parserObject



29
30
31
# File 'lib/games_dice.rb', line 29

def self.parser
  @parser ||= GamesDice::Parser.new
end