dice-roller/main.go

129 lines
3.1 KiB
Go
Raw Normal View History

2021-08-23 18:53:20 +02:00
package main
import (
"flag"
"fmt"
"math/rand"
"os"
2021-08-23 18:53:20 +02:00
"time"
)
var (
surfaces, modifier, diceThrows, attacks int
advantage, disadvantage bool
)
2022-07-29 12:26:50 +02:00
type Color string
const (
ColorBlack Color = "\u001b[30m"
ColorRed = "\u001b[31m"
ColorGreen = "\u001b[32m"
ColorYellow = "\u001b[33m"
ColorBlue = "\u001b[34m"
ColorReset = "\u001b[0m"
)
2021-08-23 18:53:20 +02:00
func main() {
2021-09-16 11:59:45 +02:00
2021-08-23 18:53:20 +02:00
rand.Seed(time.Now().Unix())
ParseFlags()
2021-09-16 11:59:45 +02:00
switch {
case advantage:
2022-07-29 12:26:50 +02:00
fmt.Println(string(ColorGreen), "Rolling with advantage...", string(ColorReset))
fmt.Println(string(ColorGreen), Advantage(SimpleCast(), SimpleCast()), string(ColorReset))
case disadvantage:
2022-07-29 12:26:50 +02:00
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++ {
2021-09-16 11:59:45 +02:00
fmt.Println()
2022-07-29 12:26:50 +02:00
fmt.Println(string(ColorYellow), "Attack:", i+1, string(ColorReset))
2022-07-29 11:24:46 +02:00
Cast(surfaces, diceThrows)
2021-09-16 11:59:45 +02:00
}
default:
2022-07-29 12:30:53 +02:00
2022-07-29 12:57:56 +02:00
if diceThrows <= 1 {
fmt.Println(string(ColorYellow), "Rolling die...:", string(ColorReset))
} else{
fmt.Println(string(ColorYellow), "Rolling",diceThrows,"dice...:", string(ColorReset))
}
2022-07-29 11:24:46 +02:00
Cast(surfaces, diceThrows)
2021-08-28 12:36:16 +02:00
}
2021-08-23 18:53:20 +02:00
}
func ParseFlags() {
2022-07-29 12:26:50 +02:00
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")
2022-07-29 12:26:50 +02:00
flag.BoolVar(&advantage, "advantage", false, "Roll with advantage")
flag.BoolVar(&disadvantage, "disadvantage", false, "Roll with disadvantage")
flag.Parse()
}
2022-07-29 11:24:46 +02:00
func Cast(dieSurfaces, castAmount int) {
2021-08-23 18:53:20 +02:00
var (
casts []int
cast, total int
)
for i := 0; i < castAmount; i++ {
cast = rand.Intn(dieSurfaces) + 1
casts = append(casts, cast)
total += cast
}
2021-08-23 23:19:01 +02:00
if castAmount > 1 {
2022-07-29 12:26:50 +02:00
fmt.Println(string(ColorBlue), "\tIndividual rolls:", casts, string(ColorReset))
2021-08-23 23:19:01 +02:00
}
2021-09-16 11:59:45 +02:00
if modifier != 0 {
2022-07-29 12:26:50 +02:00
fmt.Println("\tWithout modifier:", total)
fmt.Println(string(ColorGreen), "\tWith modifier:", total+modifier, string(ColorReset))
2021-09-16 11:59:45 +02:00
} else {
2022-07-29 12:26:50 +02:00
fmt.Println(string(ColorGreen), "\t", total, string(ColorReset))
2021-09-16 11:59:45 +02:00
}
2021-08-23 18:53:20 +02:00
}
func SimpleCast() int {
2022-07-29 11:24:46 +02:00
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
}
}