From c2442bb2077c73ac0de67b60a23cfbea79163078 Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Sun, 7 Aug 2022 01:42:35 +0200 Subject: [PATCH] Added cointoss functionality and some more formatting --- dice-roller.1 | 6 +++++ main.go | 61 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/dice-roller.1 b/dice-roller.1 index caac0f7..33c5d8e 100644 --- a/dice-roller.1 +++ b/dice-roller.1 @@ -13,6 +13,9 @@ dice-roller \- Dice roller utility written in GO .PP .B dice-roller .OP --disadvantage +.PP +.B dice-roller +.OP --coin .SH DESCRIPTION .B dice-roller 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 .B --disadvantage 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. diff --git a/main.go b/main.go index 88e0eab..0f0d9a0 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,13 @@ import ( "fmt" "math/rand" "os" + "strings" "time" ) var ( surfaces, modifier, diceThrows, attacks int - advantage, disadvantage bool + advantage, disadvantage, coin bool ) type Color string @@ -31,8 +32,26 @@ func main() { ParseFlags() 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: - if modifier != 0 { + if modifier > 0 { fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Color(ColorGreen), modifier, Color(ColorReset)) } else { 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)) - case attacks < 1: - fmt.Println("Attack amount cannot be below 1") - os.Exit(1) - case attacks > 1: for i := 0; i < attacks; i++ { 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(&disadvantage, "disadvantage", false, "Roll with disadvantage") + flag.BoolVar(&coin, "coin", false, "Toss a coin, same as '-s 2'") + 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) { var ( casts []int @@ -92,14 +132,17 @@ func Cast(dieSurfaces, castAmount int) { total += cast } - if modifier != 0 { + switch { + case modifier != 0: 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) } 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 {