dice-roller/main.go

104 lines
1.8 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
)
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:
fmt.Println("Advantage")
fmt.Println(Advantage(SimpleCast(), SimpleCast()))
case disadvantage:
fmt.Println("Disadvantage")
fmt.Println(Disadvantage(SimpleCast(), SimpleCast()))
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 11:24:46 +02:00
Cast(surfaces, diceThrows)
2021-09-16 11:59:45 +02:00
}
default:
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() {
flag.IntVar(&surfaces, "surfaces", 20, "Specify amount of die surfaces")
flag.IntVar(&diceThrows, "throws", 1, "Specify dice diceThrows")
flag.IntVar(&modifier, "modifier", 0, "Stat modifier")
flag.IntVar(&attacks, "attacks", 1, "Attacks")
flag.BoolVar(&advantage, "advantage", false, "Advantage")
flag.BoolVar(&disadvantage, "disadvantage", false, "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 {
fmt.Println(casts)
}
2021-09-16 11:59:45 +02:00
if modifier != 0 {
fmt.Println("Without modifier:", total)
fmt.Println("With modifier:", total+modifier)
} else {
fmt.Println(total)
}
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
}
}