goodice

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2025 License: MIT Imports: 3 Imported by: 0

README

goodice

Go

A good, minimal, efficient go rpg-like dice library. Straight to the point.

go get github.com/5000k/goodice

Basic Usage

package main

import (
	"github.com/5000K/goodice"
)

func main(){
	result, err := goodice.Generate("D20")
	println(result.Result)
	
	result2, err := goodice.GenerateSeeded("3D20-2D8+12", 1337)
	println(result2.Result) // total
	println(result2.Parts) // contains the result of each dice roll plus all constants
}

Supported dice notation

The parser accepts the standard TTRPG dice notation. It has two main parts: constants and dice rolls

A constant is an integer number (e.g. 7 or 42 - I think you know what an integer is)

Dice rolls are in the form of XdY, where:

  • X is an optional integer, How many dice to roll. If not provided, 1 die is assumed.
  • d simply is "d" or "D"
  • Y is the amount of sides each die has (e.g. 20 for a D20)

So D20, d20 and 1d20 will all do the same.

The parts can be chained together with the operators + and - (add and subtract). The whole string will be evaluated from left to right, just like you would when calculating it by hand.

Reusing parsed scripts

If you are going to use a script many times, it will be more efficient to reuse a Goodice instance. For this, you'll create an instance with goodice.New(script: string). This will parse your script. You can then repeatedly call Generate() or GenerateSeeded(seed:int) on the instance.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goodice

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

func New

func New(expression string) (*Goodice, error)

func (*Goodice) Generate

func (g *Goodice) Generate() (Result, error)

Generate will generate with a random seed

func (*Goodice) GenerateSeeded

func (g *Goodice) GenerateSeeded(seed int64) (Result, error)

GenerateSeeded will generate with a fixed seed

func (*Goodice) GenerateWith added in v1.0.2

func (g *Goodice) GenerateWith(gen *rand.Rand) (Result, error)

GenerateWith will generate with a given rand.Rand

type Result

type Result struct {
	Result int
	Parts  []ResultPart
}

func Generate

func Generate(expression string) (Result, error)

Generate will generate with a random seed

func GenerateSeeded

func GenerateSeeded(expression string, seed int64) (Result, error)

GenerateSeeded will generate with a fixed seed

func GenerateWith added in v1.0.2

func GenerateWith(expression string, gen *rand.Rand) (Result, error)

GenerateWith will generate with a given *rand.Rand

type ResultPart

type ResultPart struct {
	Type ResultType

	// Sides of a die. Will be 0 if ResultType != DiceROll
	Sides int

	// value within expression: will be negative for substractions
	Value int

	// absolute value: will always be positive
	AbsoluteValue int

	// Parts of the result (e.g. every single dice roll)
	ResultParts []int
}

type ResultType

type ResultType int
const (
	DiceRoll ResultType = iota
	Constant
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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