Proper refactor

This commit is contained in:
Nox Sluijtman 2022-08-19 10:15:31 +02:00
parent 6fb022aa7d
commit b4e0356ef5
4 changed files with 135 additions and 110 deletions

32
Coin/Coin.go Normal file
View file

@ -0,0 +1,32 @@
package Coin
import (
"fmt"
"math/rand"
"strings"
"gitlab.com/EternalWanderer/dice-roller/Colors"
)
func Toss(castAmount int) {
var (
coins []string
coin string
coinState int
)
for i := 0; i < castAmount; i++ {
coinState = rand.Intn(2)
if coinState == 0 {
coin = Colors.ColorYellow + "heads" + Colors.ColorReset
} else {
coin = Colors.ColorBlue + "tails" + Colors.ColorReset
}
coins = append(coins, coin)
}
if castAmount > 1 {
fmt.Printf("%sTossing %d coins...%s\n", Colors.ColorYellow, castAmount, Colors.ColorReset)
} else {
fmt.Printf("%sTossing coin...%s\n", Colors.ColorYellow, Colors.ColorReset)
}
fmt.Printf("\t%s\n", strings.Trim(fmt.Sprint(coins), "[]"))
}

13
Colors/Colors.go Normal file
View file

@ -0,0 +1,13 @@
package Colors
type Color string
const (
ColorBlack Color = "\u001b[30m"
ColorRed = "\u001b[31m"
ColorGreen = "\u001b[32m"
ColorYellow = "\u001b[33m"
ColorBlue = "\u001b[34m"
ColorMagenta = "\u001b[35m"
ColorReset = "\u001b[0m"
)

76
Dice/Dice.go Normal file
View file

@ -0,0 +1,76 @@
package Dice
import (
"fmt"
"math/rand"
"strings"
"gitlab.com/EternalWanderer/dice-roller/Colors"
)
var (
surfaces, diceThrows, modidier int
)
func Cast(surfaces, diceThrows, modifier int) {
var (
casts []int
cast, total int
)
for i := 0; i < diceThrows; i++ {
cast = rand.Intn(surfaces) + 1
casts = append(casts, cast)
total += cast
}
switch {
case modifier != 0:
fmt.Printf("%sRolling %dd%d + %d...\n%s", Colors.ColorYellow, diceThrows, surfaces, modifier, Colors.ColorReset)
default:
fmt.Printf("%sRolling %dd%d...\n%s", Colors.ColorYellow, diceThrows, surfaces, Colors.ColorReset)
}
if diceThrows > 1 {
fmt.Println(Colors.ColorMagenta, "\tIndividual rolls:",
strings.Trim(fmt.Sprint(casts), "[]"),
Colors.ColorReset)
}
if modifier != 0 {
fmt.Println("\tWithout modifier:", total)
fmt.Println(Colors.ColorGreen, "\tWith modifier:", total+modifier, Colors.ColorReset)
} else {
fmt.Printf("%s\t%d%s\n", Colors.ColorGreen, total, Colors.ColorReset)
}
}
func SimpleCast(modifier int) int {
var cast = rand.Intn(20) + 1
if modifier != 0 {
fmt.Println("Without modifier:", cast)
}
return cast + modifier
}
func Advantage(x, y int) int {
fmt.Println("\tx:", x)
fmt.Println("\tY:", y)
if x > y {
return x
} else {
return y
}
}
func Disadvantage(x, y int) int {
fmt.Println("\tx:", x)
fmt.Println("\tY:", y)
if x < y {
return x
} else {
return y
}
}

124
main.go
View file

@ -5,8 +5,11 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"strings"
"time" "time"
"gitlab.com/EternalWanderer/dice-roller/Coin"
"gitlab.com/EternalWanderer/dice-roller/Colors"
"gitlab.com/EternalWanderer/dice-roller/Dice"
) )
var ( var (
@ -14,20 +17,7 @@ var (
advantage, disadvantage, coin bool advantage, disadvantage, coin bool
) )
type Color string
const (
ColorBlack Color = "\u001b[30m"
ColorRed = "\u001b[31m"
ColorGreen = "\u001b[32m"
ColorYellow = "\u001b[33m"
ColorBlue = "\u001b[34m"
ColorMagenta = "\u001b[35m"
ColorReset = "\u001b[0m"
)
func main() { func main() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
ParseFlags() ParseFlags()
@ -48,31 +38,31 @@ func main() {
// die with 2 sides might as well be a coin // die with 2 sides might as well be a coin
case surfaces == 2 || coin: case surfaces == 2 || coin:
TossCoin(diceThrows) Coin.Toss(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", Colors.ColorGreen, modifier, Colors.ColorReset)
} else { } else {
fmt.Printf("%sRolling 1d20 with advantage...%s\n", Color(ColorGreen), Color(ColorReset)) fmt.Printf("%sRolling 1d20 with advantage...%s\n", Colors.ColorGreen, Colors.ColorReset)
} }
fmt.Printf("\t%s%d%s\n", Color(ColorGreen), Advantage(SimpleCast(), SimpleCast()), Color(ColorReset)) fmt.Printf("\t%s%d%s\n", Colors.ColorGreen, Dice.Advantage(Dice.SimpleCast(modifier), Dice.SimpleCast(modifier)), Colors.ColorReset)
case disadvantage: case disadvantage:
if modifier != 0 { if modifier != 0 {
fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Color(ColorRed), modifier, Color(ColorReset)) fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Colors.ColorRed, modifier, Colors.ColorReset)
} else { } else {
fmt.Printf("%sRolling 1d20 with disadvantage...%s\n", Color(ColorRed), Color(ColorReset)) fmt.Printf("%sRolling 1d20 with disadvantage...%s\n", Colors.ColorRed, Colors.ColorReset)
} }
fmt.Printf("\t%s%d%s\n", Color(ColorRed), Disadvantage(SimpleCast(), SimpleCast()), Color(ColorReset)) fmt.Printf("\t%s%d%s\n", Colors.ColorRed, Dice.Disadvantage(Dice.SimpleCast(modifier), Dice.SimpleCast(modifier)), Colors.ColorReset)
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", Colors.ColorBlue, i+1, Colors.ColorReset)
Cast(surfaces, diceThrows) Dice.Cast(surfaces, diceThrows, modifier)
} }
default: default:
Cast(surfaces, diceThrows) Dice.Cast(surfaces, diceThrows, modifier)
} }
} }
@ -96,89 +86,3 @@ func ParseFlags() {
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) {
var (
casts []int
cast, total int
)
for i := 0; i < castAmount; i++ {
cast = rand.Intn(dieSurfaces) + 1
casts = append(casts, cast)
total += cast
}
switch {
case modifier != 0:
fmt.Printf("%sRolling %dd%d + %d...\n%s", ColorYellow, diceThrows, surfaces, modifier, ColorReset)
default:
fmt.Printf("%sRolling %dd%d...\n%s", ColorYellow, diceThrows, surfaces, ColorReset)
}
if castAmount > 1 {
fmt.Println(Color(ColorMagenta), "\tIndividual rolls:",
strings.Trim(fmt.Sprint(casts), "[]"),
Color(ColorReset))
}
if modifier != 0 {
fmt.Println("\tWithout modifier:", total)
fmt.Println(Color(ColorGreen), "\tWith modifier:", total+modifier, Color(ColorReset))
} else {
fmt.Printf("%s\t%d%s\n", Color(ColorGreen), total, Color(ColorReset))
}
}
func SimpleCast() int {
var cast = rand.Intn(20) + 1
if modifier != 0 {
fmt.Println("Without modifier:", cast)
}
return cast + modifier
}
func Advantage(x, y int) int {
fmt.Println("\tx:", x)
fmt.Println("\tY:", y)
if x > y {
return x
} else {
return y
}
}
func Disadvantage(x, y int) int {
fmt.Println("\tx:", x)
fmt.Println("\tY:", y)
if x < y {
return x
} else {
return y
}
}