dice-roller/Dice/Dice.go

84 lines
1.3 KiB
Go
Raw Normal View History

2022-08-19 10:15:31 +02:00
package Dice
import (
"fmt"
"math/rand"
"strings"
"time"
2022-08-19 10:15:31 +02:00
2022-08-22 17:44:16 +02:00
"github.com/fatih/color"
2022-08-19 10:15:31 +02:00
)
var (
surfaces, diceThrows, modidier int
initRandom bool
2022-08-19 10:15:31 +02:00
)
func InitRandom(initialised bool) {
if initialised {
return
}
rand.Seed(time.Now().Unix())
initRandom = true
}
func Cast(surfaces, diceThrows, modifier int) {
InitRandom(initRandom)
2022-08-19 10:15:31 +02:00
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:
2022-08-22 17:44:16 +02:00
color.Yellow("Rolling %dd%d + %d...\n", diceThrows, surfaces, modifier)
2022-08-19 10:15:31 +02:00
default:
2022-08-22 17:44:16 +02:00
color.Yellow("Rolling %dd%d...\n", diceThrows, surfaces)
2022-08-19 10:15:31 +02:00
}
if diceThrows > 1 {
2022-08-22 17:44:16 +02:00
color.Magenta("\tIndividual rolls: %s", strings.Trim(fmt.Sprint(casts), "[]"))
2022-08-19 10:15:31 +02:00
}
if modifier != 0 {
2022-08-22 17:44:16 +02:00
fmt.Printf("\tWithout modifier: %d\n", total)
color.Green("\tWith modifier: %d", total+modifier)
2022-08-19 10:15:31 +02:00
} else {
2022-08-22 17:44:16 +02:00
color.Green("%d", total)
2022-08-19 10:15:31 +02:00
}
}
2022-08-19 15:19:53 +02:00
func SimpleCast() int {
InitRandom(initRandom)
2022-08-19 10:15:31 +02:00
var cast = rand.Intn(20) + 1
2022-08-19 15:19:53 +02:00
return cast
2022-08-19 10:15:31 +02:00
}
func Advantage() (int, int, int) {
x := SimpleCast()
y := SimpleCast()
2022-08-19 10:15:31 +02:00
if x > y {
2022-08-19 15:19:53 +02:00
return x, x, y
2022-08-19 10:15:31 +02:00
} else {
2022-08-19 15:19:53 +02:00
return y, x, y
2022-08-19 10:15:31 +02:00
}
}
func Disadvantage() (int, int, int) {
x := SimpleCast()
y := SimpleCast()
2022-08-19 10:15:31 +02:00
if x < y {
2022-08-19 15:19:53 +02:00
return x, x, y
2022-08-19 10:15:31 +02:00
} else {
2022-08-19 15:19:53 +02:00
return y, x, y
2022-08-19 10:15:31 +02:00
}
}