dicelang

package
v0.0.0-...-6b9ac5a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2019 License: AGPL-3.0, MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiceProbability

func DiceProbability(numberOfDice, sides, H, L int64) map[int64]float64

DiceProbability returns a map of results to probabilities (in percent) for a given roll of dice numberOfDice (int64): the number of dice in the throw. sides (int64): the sides each die has H: The number of high dice to drop (set to 0 to not drop any) L: The number of low dice to drop (set to 0 to not drop any) credit to https://stackoverflow.com/questions/50690348/calculate-probability-of-a-fair-dice-roll-in-non-exponential-time

func FacesSliceString

func FacesSliceString(faces []int64) string

func MergeDiceTotalMaps

func MergeDiceTotalMaps(mapsToMerge ...map[string]float64) map[string]float64

func PrintAST

func PrintAST(t *AST, identation int)

PrintAST prints a formatted version of the ast to StdOut

func ReStringAST

func ReStringAST(t *AST) string

ReStringAST is a modified inverse shunting yard which converts an AST back to an infix expression.

func TotalsMapString

func TotalsMapString(m map[string]float64) string

Types

type AST

type AST struct {
	Sym   string
	Value string

	BindingPower int

	Children []*AST
	// contains filtered or unexported fields
}

AST represents a node in an abstract syntax tree

func (*AST) GetDiceSet

func (t *AST) GetDiceSet() (float64, DiceSet, error)

GetDiceSet returns the sum of an AST, a DiceSet, and an error

func (*AST) String

func (token *AST) String() (string, error)

Convert AST to Infix expression

type Dice

type Dice struct {
	Count       int64
	Sides       int64
	Total       int64
	Faces       []int64
	Max         int64
	Min         int64
	DropHighest int64
	DropLowest  int64
	Color       string
}

Dice represents a a throw of a single type of die

func GetDiceSets

func GetDiceSets(stmts ...*AST) (map[string]float64, []Dice, error)

GetDiceSets merges all statements in ...*AST and returns a merged diceTotalMap and all rolled dice.

func (*Dice) Roll

func (d *Dice) Roll() (int64, error)

Roll rolls the dice, sets Min, Max, and Faces. Returns the total. Can be called multiple times and returns the same value each time.

type DiceSet

type DiceSet struct {
	Dice          []Dice
	TotalsByColor map[string]float64
	// contains filtered or unexported fields
}

DiceSet represents a collection of Dice and their totals by type

func (*DiceSet) AddToColor

func (d *DiceSet) AddToColor(color string, value float64)

AddToColor increments the total result for a given color

func (*DiceSet) PeekColor

func (d *DiceSet) PeekColor() string

PeekColor returns the most recently added color from the "stack"

func (*DiceSet) PopColor

func (d *DiceSet) PopColor() string

PopColor pops a color from the "stack"

func (*DiceSet) PushAndRoll

func (d *DiceSet) PushAndRoll(dice Dice) (int64, error)

PushAndRoll adds a dice roll to the "stack" applying any values from the set

func (*DiceSet) PushColor

func (d *DiceSet) PushColor(color string)

PushColor pushes a color to the "stack"

func (*DiceSet) Top

func (d *DiceSet) Top(loc int) *Dice

Top returns a pointer to the most recently added dice roll

type Element

type Element struct {
	// contains filtered or unexported fields
}

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer steps through a source string and returns tokens

func NewLexer

func NewLexer(source string) *Lexer

NewLexer creates a new Lexer, initializes the word2number converter and token registry.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser holds a Lexer and implements a top down operator precedence parser (https://tdop.github.io/) credit to: https://github.com/cristiandima/tdop for most of this code.

func NewParser

func NewParser(source string) *Parser

NewParser creates a new Parser from an input string

func (*Parser) Statement

func (parse *Parser) Statement() (*AST, error)

Statement returns the next statement from the parser as *AST

func (*Parser) Statements

func (parse *Parser) Statements() (*AST, error)

Statements returns all statements from the parser as []*AST

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

func (*Stack) Empty

func (s *Stack) Empty() bool

func (*Stack) Pop

func (s *Stack) Pop() (value interface{})

func (*Stack) Push

func (s *Stack) Push(value interface{})

func (*Stack) String

func (s *Stack) String() string

func (*Stack) Top

func (s *Stack) Top() interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL