Moved rng back to where needed and rewrote some thing to make it actually work it this time

This commit is contained in:
Nox Sluijtman 2022-08-20 13:37:07 +02:00
parent 2d560c42a0
commit 1e57a27bcf
2 changed files with 34 additions and 11 deletions

View file

@ -4,15 +4,26 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"strings" "strings"
"time"
"gitlab.com/EternalWanderer/dice-roller/Colors" "gitlab.com/EternalWanderer/dice-roller/Colors"
) )
var ( var (
surfaces, diceThrows, modidier int 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) { func Cast(surfaces, diceThrows, modifier int) {
InitRandom(initRandom)
var ( var (
casts []int casts []int
cast, total int cast, total int
@ -46,13 +57,16 @@ func Cast(surfaces, diceThrows, modifier int) {
} }
func SimpleCast() int { func SimpleCast() int {
InitRandom(initRandom)
var cast = rand.Intn(20) + 1 var cast = rand.Intn(20) + 1
return cast return cast
} }
func Advantage(x, y int) (int, int, int) { func Advantage() (int, int, int) {
x := SimpleCast()
y := SimpleCast()
if x > y { if x > y {
return x, x, y return x, x, y
} else { } 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 { if x < y {
return x, x, y return x, x, y
} else { } else {

25
main.go
View file

@ -3,9 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"math/rand"
"os" "os"
"time"
"gitlab.com/EternalWanderer/dice-roller/Coin" "gitlab.com/EternalWanderer/dice-roller/Coin"
"gitlab.com/EternalWanderer/dice-roller/Colors" "gitlab.com/EternalWanderer/dice-roller/Colors"
@ -19,7 +17,6 @@ var (
func main() { func main() {
ParseFlags() ParseFlags()
rand.Seed(time.Now().Unix())
switch { switch {
@ -41,9 +38,7 @@ func main() {
Coin.Toss(diceThrows) Coin.Toss(diceThrows)
case advantage: case advantage:
x := Dice.SimpleCast() result, x_block, y_block := Dice.Advantage()
y := Dice.SimpleCast()
result, x_block, y_block := Dice.Advantage(x, y)
if modifier != 0 { if modifier != 0 {
fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Colors.ColorGreen, modifier, Colors.ColorReset) 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) 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("%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) 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: case disadvantage:
x := Dice.SimpleCast() result, x_block, y_block := Dice.Disadvantage()
y := Dice.SimpleCast()
result, x_block, y_block := Dice.Disadvantage(x, y)
if modifier != 0 { if modifier != 0 {
fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Colors.ColorRed, modifier, Colors.ColorReset) 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) 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("%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) 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: case attacks > 1:
for i := 0; i < attacks; i++ { for i := 0; i < attacks; i++ {