Added cointoss functionality and some more formatting

This commit is contained in:
Nox Sluijtman 2022-08-07 01:42:35 +02:00
parent 8c04e87930
commit c2442bb207
2 changed files with 58 additions and 9 deletions

View file

@ -13,6 +13,9 @@ dice-roller \- Dice roller utility written in GO
.PP .PP
.B dice-roller .B dice-roller
.OP --disadvantage .OP --disadvantage
.PP
.B dice-roller
.OP --coin
.SH DESCRIPTION .SH DESCRIPTION
.B dice-roller .B dice-roller
is a little dice roller I wrote because I'm hopelessly bad at basic arithmetic equations. is a little dice roller I wrote because I'm hopelessly bad at basic arithmetic equations.
@ -36,3 +39,6 @@ Roll two 2d20 and take the highest number, accepts modifiers
.TP .TP
.B --disadvantage .B --disadvantage
Roll two 2d20 and take the lowest number, accepts modifiers Roll two 2d20 and take the lowest number, accepts modifiers
.TP
.B --coin
Toss a coin, same behaviour as throwing with 2 surfaces, only accepts different amount of casts.

61
main.go
View file

@ -5,12 +5,13 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"strings"
"time" "time"
) )
var ( var (
surfaces, modifier, diceThrows, attacks int surfaces, modifier, diceThrows, attacks int
advantage, disadvantage bool advantage, disadvantage, coin bool
) )
type Color string type Color string
@ -31,8 +32,26 @@ func main() {
ParseFlags() ParseFlags()
switch { switch {
// few basic checks
case attacks < 1:
fmt.Println("Attack amount cannot be below 1")
os.Exit(1)
case surfaces < 2:
fmt.Println("Die surfaces cannot be below 2")
os.Exit(1)
case diceThrows < 1:
fmt.Println("Dice thrown cannot be below 1")
os.Exit(1)
// die with 2 sides might as well be a coin
case surfaces == 2 || coin:
TossCoin(diceThrows)
case advantage: case advantage:
if modifier != 0 { if modifier > 0 {
fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Color(ColorGreen), modifier, Color(ColorReset)) fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Color(ColorGreen), modifier, Color(ColorReset))
} else { } else {
fmt.Printf("%sRolling 1d20 with advantage...%s\n", Color(ColorGreen), Color(ColorReset)) fmt.Printf("%sRolling 1d20 with advantage...%s\n", Color(ColorGreen), Color(ColorReset))
@ -47,10 +66,6 @@ func main() {
} }
fmt.Printf("\t%s%d%s\n", Color(ColorRed), Disadvantage(SimpleCast(), SimpleCast()), Color(ColorReset)) fmt.Printf("\t%s%d%s\n", Color(ColorRed), Disadvantage(SimpleCast(), SimpleCast()), Color(ColorReset))
case attacks < 1:
fmt.Println("Attack amount cannot be below 1")
os.Exit(1)
case attacks > 1: case attacks > 1:
for i := 0; i < attacks; i++ { for i := 0; i < attacks; i++ {
fmt.Printf("%sAttack %d:%s\n", Color(ColorBlue), i+1, Color(ColorReset)) fmt.Printf("%sAttack %d:%s\n", Color(ColorBlue), i+1, Color(ColorReset))
@ -77,9 +92,34 @@ func ParseFlags() {
flag.BoolVar(&advantage, "advantage", false, "Roll with advantage") flag.BoolVar(&advantage, "advantage", false, "Roll with advantage")
flag.BoolVar(&disadvantage, "disadvantage", false, "Roll with disadvantage") flag.BoolVar(&disadvantage, "disadvantage", false, "Roll with disadvantage")
flag.BoolVar(&coin, "coin", false, "Toss a coin, same as '-s 2'")
flag.Parse() flag.Parse()
} }
func TossCoin(castAmount int) {
var (
coins []string
coin string
coinState int
)
for i := 0; i < castAmount; i++ {
coinState = rand.Intn(2)
if coinState == 0 {
coin = ColorYellow + "heads" + ColorReset
} else {
coin = ColorBlue + "tails" + ColorReset
}
coins = append(coins, coin)
}
if castAmount > 1 {
fmt.Printf("%sTossing %d coins...%s\n", ColorYellow, castAmount, ColorReset)
} else {
fmt.Printf("%sTossing coin...%s\n", ColorYellow, ColorReset)
}
fmt.Printf("\t%s\n", strings.Trim(fmt.Sprint(coins), "[]"))
}
func Cast(dieSurfaces, castAmount int) { func Cast(dieSurfaces, castAmount int) {
var ( var (
casts []int casts []int
@ -92,14 +132,17 @@ func Cast(dieSurfaces, castAmount int) {
total += cast total += cast
} }
if modifier != 0 { switch {
case modifier != 0:
fmt.Printf("%sRolling %dd%d + %d...\n%s", ColorYellow, diceThrows, surfaces, modifier, ColorReset) fmt.Printf("%sRolling %dd%d + %d...\n%s", ColorYellow, diceThrows, surfaces, modifier, ColorReset)
} else { default:
fmt.Printf("%sRolling %dd%d...\n%s", ColorYellow, diceThrows, surfaces, ColorReset) fmt.Printf("%sRolling %dd%d...\n%s", ColorYellow, diceThrows, surfaces, ColorReset)
} }
if castAmount > 1 { if castAmount > 1 {
fmt.Println(Color(ColorMagenta), "\tIndividual rolls:", casts, Color(ColorReset)) fmt.Println(Color(ColorMagenta), "\tIndividual rolls:",
strings.Trim(fmt.Sprint(casts), "[]"),
Color(ColorReset))
} }
if modifier != 0 { if modifier != 0 {