From 1e57a27bcf70d9695108ebe528c5c104f8c9dace Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Sat, 20 Aug 2022 13:37:07 +0200 Subject: [PATCH] Moved rng back to where needed and rewrote some thing to make it actually work it this time --- Dice/Dice.go | 20 ++++++++++++++++++-- main.go | 25 ++++++++++++++++--------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Dice/Dice.go b/Dice/Dice.go index d9ea822..70e7f52 100644 --- a/Dice/Dice.go +++ b/Dice/Dice.go @@ -4,15 +4,26 @@ import ( "fmt" "math/rand" "strings" + "time" "gitlab.com/EternalWanderer/dice-roller/Colors" ) var ( surfaces, diceThrows, modidier int + initRandom bool ) +func InitRandom(initialised bool) { + if initialised { + return + } + rand.Seed(time.Now().Unix()) + initRandom = true +} + func Cast(surfaces, diceThrows, modifier int) { + InitRandom(initRandom) var ( casts []int cast, total int @@ -46,13 +57,16 @@ func Cast(surfaces, diceThrows, modifier int) { } func SimpleCast() int { + InitRandom(initRandom) var cast = rand.Intn(20) + 1 return cast } -func Advantage(x, y int) (int, int, int) { +func Advantage() (int, int, int) { + x := SimpleCast() + y := SimpleCast() if x > y { return x, x, y } else { @@ -60,7 +74,9 @@ func Advantage(x, y int) (int, int, int) { } } -func Disadvantage(x, y int) (int, int, int) { +func Disadvantage() (int, int, int) { + x := SimpleCast() + y := SimpleCast() if x < y { return x, x, y } else { diff --git a/main.go b/main.go index 34afab7..56baa1a 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,7 @@ package main import ( "flag" "fmt" - "math/rand" "os" - "time" "gitlab.com/EternalWanderer/dice-roller/Coin" "gitlab.com/EternalWanderer/dice-roller/Colors" @@ -19,7 +17,6 @@ var ( func main() { ParseFlags() - rand.Seed(time.Now().Unix()) switch { @@ -41,9 +38,7 @@ func main() { Coin.Toss(diceThrows) case advantage: - x := Dice.SimpleCast() - y := Dice.SimpleCast() - result, x_block, y_block := Dice.Advantage(x, y) + result, x_block, y_block := Dice.Advantage() if modifier != 0 { fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Colors.ColorGreen, modifier, Colors.ColorReset) fmt.Printf("\t%s%s%s\n", Colors.ColorYellow, "Without modifier...", Colors.ColorReset) @@ -54,11 +49,16 @@ func main() { fmt.Printf("%sRolling 1d20 with advantage...%s\n", Colors.ColorGreen, Colors.ColorReset) fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorGreen, result, Colors.ColorReset, "x:", x_block, "y:", y_block) } + switch result { + case 20: + fmt.Printf("\t%sNatural%s 20!%s\n", Colors.ColorMagenta, Colors.ColorGreen, Colors.ColorReset) + case 1: + fmt.Printf("\t%sNatural%s 1!%s\n", Colors.ColorMagenta, Colors.ColorRed, Colors.ColorReset) + default: + } case disadvantage: - x := Dice.SimpleCast() - y := Dice.SimpleCast() - result, x_block, y_block := Dice.Disadvantage(x, y) + result, x_block, y_block := Dice.Disadvantage() if modifier != 0 { fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Colors.ColorRed, modifier, Colors.ColorReset) fmt.Printf("\t%s%s%s\n", Colors.ColorYellow, "Without modifier...", Colors.ColorReset) @@ -69,6 +69,13 @@ func main() { fmt.Printf("%sRolling 1d20 with disadvantage...%s\n", Colors.ColorRed, Colors.ColorReset) fmt.Printf("\t%s%d%s\t%s %d\t%s %d\n", Colors.ColorRed, result, Colors.ColorReset, "x:", x_block, "y:", y_block) } + switch result { + case 20: + fmt.Printf("\t%sNatural%s 20!%s\n", Colors.ColorMagenta, Colors.ColorGreen, Colors.ColorReset) + case 1: + fmt.Printf("\t%sNatural%s 1!%s\n", Colors.ColorMagenta, Colors.ColorRed, Colors.ColorReset) + default: + } case attacks > 1: for i := 0; i < attacks; i++ {