dice-roller/main.go

130 lines
3.1 KiB
Go

package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
)
var (
surfaces, modifier, diceThrows, attacks int
advantage, disadvantage bool
)
type Color string
const (
ColorBlack Color = "\u001b[30m"
ColorRed = "\u001b[31m"
ColorGreen = "\u001b[32m"
ColorYellow = "\u001b[33m"
ColorBlue = "\u001b[34m"
ColorReset = "\u001b[0m"
)
func main() {
rand.Seed(time.Now().Unix())
ParseFlags()
switch {
case advantage:
fmt.Println(string(ColorGreen), "Rolling with advantage...", string(ColorReset))
fmt.Println(string(ColorGreen), Advantage(SimpleCast(), SimpleCast()), string(ColorReset))
case disadvantage:
fmt.Println(string(ColorRed), "Rolling with disadvantage...", string(ColorReset))
fmt.Println(string(ColorRed), Disadvantage(SimpleCast(), SimpleCast()), string(ColorReset))
case attacks < 1:
fmt.Println("Attack amount cannot be below 1.")
os.Exit(1)
case attacks > 1:
for i := 0; i < attacks; i++ {
fmt.Println()
fmt.Println(string(ColorYellow), "Attack:", i+1, string(ColorReset))
Cast(surfaces, diceThrows)
}
default:
Cast(surfaces, diceThrows)
}
}
func ParseFlags() {
flag.IntVar(&surfaces, "surfaces", 20, "Use to specify die surfaces, does not apply to advantage and disadvantage")
flag.IntVar(&surfaces, "s", 20, "Use to specify die surfaces, defaults to 20")
flag.IntVar(&diceThrows, "throws", 1, "Specify amount of dice to cast")
flag.IntVar(&diceThrows, "c", 1, "Specify amount of dice to cast")
flag.IntVar(&modifier, "modifier", 0, "Add modifier to result of rolls")
flag.IntVar(&modifier, "m", 0, "Add modifier to result of rolls")
flag.IntVar(&attacks, "attacks", 1, "Roll a set rules multiple times, does not apply to advantage and disadvantage")
flag.IntVar(&attacks, "a", 1, "Roll a set rules multiple times, does not apply to advantage and disadvantage")
flag.BoolVar(&advantage, "advantage", false, "Roll with advantage")
flag.BoolVar(&disadvantage, "disadvantage", false, "Roll with disadvantage")
flag.Parse()
}
func Cast(dieSurfaces, castAmount int) {
var (
casts []int
cast, total int
)
for i := 0; i < castAmount; i++ {
cast = rand.Intn(dieSurfaces) + 1
casts = append(casts, cast)
total += cast
}
if castAmount > 1 {
fmt.Println(string(ColorBlue), "\tIndividual rolls:", casts, string(ColorReset))
}
if modifier != 0 {
fmt.Printf("%sRolling %dd%d + %d...\n%s", ColorYellow, diceThrows, surfaces, modifier, ColorReset)
} else {
fmt.Printf("%sRolling %dd%d...\n%s", ColorYellow, diceThrows, surfaces, ColorReset)
}
if modifier != 0 {
fmt.Println("\tWithout modifier:", total)
fmt.Println(string(ColorGreen), "\tWith modifier:", total+modifier, string(ColorReset))
} else {
fmt.Println(string(ColorGreen), "\t", total, string(ColorReset))
}
}
func SimpleCast() int {
var cast = rand.Intn(20) + 1
fmt.Println("Without modifier:", cast)
return cast + modifier
}
func Advantage(x, y int) int {
fmt.Println("x:", x)
fmt.Println("Y:", y)
if x > y {
return x
} else {
return y
}
}
func Disadvantage(x, y int) int {
fmt.Println("x:", x)
fmt.Println("Y:", y)
if x < y {
return x
} else {
return y
}
}