diff --git a/Coin/Coin.go b/Coin/Coin.go new file mode 100644 index 0000000..4dc7414 --- /dev/null +++ b/Coin/Coin.go @@ -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), "[]")) +} diff --git a/Colors/Colors.go b/Colors/Colors.go new file mode 100644 index 0000000..6e0e6af --- /dev/null +++ b/Colors/Colors.go @@ -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" +) diff --git a/Dice/Dice.go b/Dice/Dice.go new file mode 100644 index 0000000..c116ed1 --- /dev/null +++ b/Dice/Dice.go @@ -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 + } +} diff --git a/main.go b/main.go index 0f0d9a0..54b5651 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,11 @@ import ( "fmt" "math/rand" "os" - "strings" "time" + + "gitlab.com/EternalWanderer/dice-roller/Coin" + "gitlab.com/EternalWanderer/dice-roller/Colors" + "gitlab.com/EternalWanderer/dice-roller/Dice" ) var ( @@ -14,20 +17,7 @@ var ( 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() { - rand.Seed(time.Now().Unix()) ParseFlags() @@ -48,31 +38,31 @@ func main() { // die with 2 sides might as well be a coin case surfaces == 2 || coin: - TossCoin(diceThrows) + Coin.Toss(diceThrows) case advantage: 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 { - 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: 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 { - 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: for i := 0; i < attacks; i++ { - fmt.Printf("%sAttack %d:%s\n", Color(ColorBlue), i+1, Color(ColorReset)) - Cast(surfaces, diceThrows) + fmt.Printf("%sAttack %d:%s\n", Colors.ColorBlue, i+1, Colors.ColorReset) + Dice.Cast(surfaces, diceThrows, modifier) } default: - Cast(surfaces, diceThrows) + Dice.Cast(surfaces, diceThrows, modifier) } } @@ -96,89 +86,3 @@ func ParseFlags() { 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 - } -}