dice-roller/Dice/Dice.go

86 lines
1.5 KiB
Go

package Dice
import (
"fmt"
"math/rand"
"strings"
"time"
"gitlab.com/EternalWanderer/dice-roller/Colors"
)
var (
surfaces, diceThrows, modidier int
initRandom bool
)
func InitRandom(initialised bool) {
if initialised {
return
}
rand.Seed(time.Now().Unix())
initRandom = true
}
func Cast(surfaces, diceThrows, modifier int) {
InitRandom(initRandom)
var (
casts []int
cast, total int
)
for i := 0; i < diceThrows; i++ {
cast = rand.Intn(surfaces) + 1
casts = append(casts, cast)
total += cast
}
switch {
case modifier != 0:
fmt.Printf("%sRolling %dd%d + %d...\n%s", Colors.ColorYellow, diceThrows, surfaces, modifier, Colors.ColorReset)
default:
fmt.Printf("%sRolling %dd%d...\n%s", Colors.ColorYellow, diceThrows, surfaces, Colors.ColorReset)
}
if diceThrows > 1 {
fmt.Println(Colors.ColorMagenta, "\tIndividual rolls:",
strings.Trim(fmt.Sprint(casts), "[]"),
Colors.ColorReset)
}
if modifier != 0 {
fmt.Println("\tWithout modifier:", total)
fmt.Println(Colors.ColorGreen, "\tWith modifier:", total+modifier, Colors.ColorReset)
} else {
fmt.Printf("%s\t%d%s\n", Colors.ColorGreen, total, Colors.ColorReset)
}
}
func SimpleCast() int {
InitRandom(initRandom)
var cast = rand.Intn(20) + 1
return cast
}
func Advantage() (int, int, int) {
x := SimpleCast()
y := SimpleCast()
if x > y {
return x, x, y
} else {
return y, x, y
}
}
func Disadvantage() (int, int, int) {
x := SimpleCast()
y := SimpleCast()
if x < y {
return x, x, y
} else {
return y, x, y
}
}