More refactor and string formatting

This commit is contained in:
Nox Sluijtman 2022-07-29 15:04:24 +02:00
parent 852bd4c906
commit 06c27a2e3e

48
main.go
View file

@ -21,6 +21,7 @@ const (
ColorGreen = "\u001b[32m" ColorGreen = "\u001b[32m"
ColorYellow = "\u001b[33m" ColorYellow = "\u001b[33m"
ColorBlue = "\u001b[34m" ColorBlue = "\u001b[34m"
ColorMagenta = "\u001b[35m"
ColorReset = "\u001b[0m" ColorReset = "\u001b[0m"
) )
@ -31,22 +32,31 @@ func main() {
switch { switch {
case advantage: case advantage:
fmt.Println(string(ColorGreen), "Rolling with advantage...", string(ColorReset)) if modifier != 0 {
fmt.Println(string(ColorGreen), Advantage(SimpleCast(), SimpleCast()), string(ColorReset)) 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))
}
fmt.Printf("\t%s%d%s\n", Color(ColorGreen), Advantage(SimpleCast(), SimpleCast()), Color(ColorReset))
case disadvantage: case disadvantage:
fmt.Println(string(ColorRed), "Rolling with disadvantage...", string(ColorReset)) if modifier != 0 {
fmt.Println(string(ColorRed), Disadvantage(SimpleCast(), SimpleCast()), string(ColorReset)) fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Color(ColorRed), modifier, Color(ColorReset))
} else {
fmt.Printf("%sRolling 1d20 with disadvantage...%s\n", Color(ColorRed), Color(ColorReset))
}
fmt.Printf("\t%s%d%s\n", Color(ColorRed), Disadvantage(SimpleCast(), SimpleCast()), Color(ColorReset))
case attacks < 1: case attacks < 1:
fmt.Println("Attack amount cannot be below 1.") fmt.Println("Attack amount cannot be below 1")
os.Exit(1) os.Exit(1)
case attacks > 1: case attacks > 1:
for i := 0; i < attacks; i++ { for i := 0; i < attacks; i++ {
fmt.Println() fmt.Printf("%sAttack %d:%s\n", Color(ColorBlue), i+1, Color(ColorReset))
fmt.Println(string(ColorYellow), "Attack:", i+1, string(ColorReset))
Cast(surfaces, diceThrows) Cast(surfaces, diceThrows)
} }
default: default:
Cast(surfaces, diceThrows) Cast(surfaces, diceThrows)
} }
} }
@ -82,21 +92,21 @@ func Cast(dieSurfaces, castAmount int) {
total += cast total += cast
} }
if castAmount > 1 {
fmt.Println(string(ColorBlue), "\tIndividual rolls:", casts, string(ColorReset))
}
if modifier != 0 { if 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 { } else {
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 {
fmt.Println(Color(ColorMagenta), "\tIndividual rolls:", casts, Color(ColorReset))
}
if modifier != 0 { if modifier != 0 {
fmt.Println("\tWithout modifier:", total) fmt.Println("\tWithout modifier:", total)
fmt.Println(string(ColorGreen), "\tWith modifier:", total+modifier, string(ColorReset)) fmt.Println(Color(ColorGreen), "\tWith modifier:", total+modifier, Color(ColorReset))
} else { } else {
fmt.Println(string(ColorGreen), "\t", total, string(ColorReset)) fmt.Println(Color(ColorGreen), "\t", total, Color(ColorReset))
} }
} }
@ -104,13 +114,15 @@ func SimpleCast() int {
var cast = rand.Intn(20) + 1 var cast = rand.Intn(20) + 1
if modifier != 0 {
fmt.Println("Without modifier:", cast) fmt.Println("Without modifier:", cast)
}
return cast + modifier return cast + modifier
} }
func Advantage(x, y int) int { func Advantage(x, y int) int {
fmt.Println("x:", x) fmt.Println("\tx:", x)
fmt.Println("Y:", y) fmt.Println("\tY:", y)
if x > y { if x > y {
return x return x
} else { } else {
@ -119,8 +131,8 @@ func Advantage(x, y int) int {
} }
func Disadvantage(x, y int) int { func Disadvantage(x, y int) int {
fmt.Println("x:", x) fmt.Println("\tx:", x)
fmt.Println("Y:", y) fmt.Println("\tY:", y)
if x < y { if x < y {
return x return x
} else { } else {