dices

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 1 Imported by: 0

README

Contributors Forks Stargazers Issues

GoDoc Go Report Card

Dice package

A GoLang package that allows complete management, autonomous or manual, of the use of dice for games of all kinds.

Installation

First, make sure you have GoLang installed on your machine.
Proceed by downloading the package with the go get -u github.com/MoraGames/dices command.

Examples

1. Roll a standard 6-sides dice: GoPlayground

package main

import (
	"fmt"
	"log"

	"github.com/MoraGames/dices/standardDice"
)

func main() {
	//Create a standard n-sides dice
	sidesNumber := 6
	d1, err := standardDice.NewDice(sidesNumber)
	if err != nil {
		log.Panic(err)
	}
	//d1 is a 6-sided dice with faces valued [1, 2, 3, 4, 5, 6]

	//Roll the dice and print the result
	result := d1.Throw()
	fmt.Println("The result of rolling the dice d1 is:", result)
}

2. Roll a range dice: GoPlayground

package main

import (
	"fmt"
	"log"

	"github.com/MoraGames/dices"
	"github.com/MoraGames/dices/standardDice"
)

func main() {
	//Create a special ranged dice
	lowestSide, highestSide := -1, 1
	r1, err := dices.NewRange(lowestSide, highestSide)
	if err != nil {
		log.Panic(err)
	}
	d2, err := standardDice.NewRangeDice(r1)
	if err != nil {
		log.Panic(err)
	}
	//d2 is a 3-sided dice with faces valued [-1, 0, 1]

	//Roll the dice and print the result
	result := d2.Throw()
	fmt.Println("The result of rolling the dice d2 is:", result)
}

3. Roll a custom dice: GoPlayground

package main

import (
	"fmt"
	"log"

	"github.com/MoraGames/dices"
	"github.com/MoraGames/dices/standardDice"
)

func main() {
	//Create a custom n-sides dice and their respective values
	sidesValue := []dices.Side{
		standardDice.NewStandardSide("Apple"),
		standardDice.NewStandardSide("Banana"),
		standardDice.NewStandardSide("Cherry"),
		standardDice.NewStandardSide("Dates"),
		standardDice.NewStandardSide("Elderberry"),
	}
	d3, err := standardDice.NewCustomDice(sidesValue...)
	if err != nil {
		log.Panic(err)
	}
	//d3 is a 5-sided dice with faces valued ["Apple", "Banana", "Cherry", "Dates", "Elderberry"]

	//Roll the dice and print the result
	result := d3.Throw()
	fmt.Println("The result of rolling the dice d3 is:", result)
}

4. Roll a set of dices: GoPlayground

package main

import (
	"fmt"
	"log"

	"github.com/MoraGames/dices"
	"github.com/MoraGames/dices/set"
	"github.com/MoraGames/dices/standardDice"
)

func main() {
	//Create a standard n-sides dice
	sidesNumber := 6
	d4a, err := standardDice.NewDice(sidesNumber)
	if err != nil {
		log.Panic(err)
	}
	//d4a is a 6-sided dice with faces valued [1, 2, 3, 4, 5, 6]

	//Create a special ranged dice
	lowestSide, highestSide := -1, 1
	r2, err := dices.NewRange(lowestSide, highestSide)
	if err != nil {
		log.Panic(err)
	}
	d4b, err := standardDice.NewRangeDice(r2)
	if err != nil {
		log.Panic(err)
	}
	//d4b is a 3-sided dice with faces valued [-1, 0, 1]

	//Create a custom n-sides dice and their respective values
	sidesValue := []dices.Side{
		standardDice.NewStandardSide("Apple"),
		standardDice.NewStandardSide("Banana"),
		standardDice.NewStandardSide("Cherry"),
		standardDice.NewStandardSide("Dates"),
		standardDice.NewStandardSide("Elderberry"),
	}
	d4c, err := standardDice.NewCustomDice(sidesValue...)
	if err != nil {
		log.Panic(err)
	}
	//d4c is a 5-sided dice with faces valued ["Apple", "Banana", "Cherry", "Dates", "Elderberry"]

	//Create a custom set of n-dices
	s1, err := dices.NewSet(d4a, d4b, d4c)
	if err != nil {
		log.Panic(err)
	}

	//Roll all the dices in the set
	result := s1.Throw()
	fmt.Println("The result of rolling the set s1 is:", result)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMinimumSides

func GetMinimumSides() int

Types

type Dice

type Dice interface {
	Throw() Side
	ThrowN(n int) []Side
}

type Range added in v1.2.0

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

func NewRange added in v1.2.0

func NewRange(lowest, highest int) (Range, error)

func (Range) Highest added in v1.2.0

func (r Range) Highest() int

func (Range) Lowest added in v1.2.0

func (r Range) Lowest() int

type Side

type Side interface {
	Value() any
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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