summaryrefslogtreecommitdiff
path: root/roll.go
diff options
context:
space:
mode:
Diffstat (limited to 'roll.go')
-rw-r--r--roll.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/roll.go b/roll.go
new file mode 100644
index 0000000..1848754
--- /dev/null
+++ b/roll.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+ "fmt"
+ "math/rand"
+ "os"
+ "regexp"
+ "strconv"
+ "time"
+)
+
+func usage() {
+ fmt.Printf("Arguments are in the format [<COUNT>]d<SIZE>[+MOD]\n")
+}
+
+func roll(input string) {
+ parser := regexp.MustCompile("^([0-9]*)d([0-9]+)([+-][0-9]+)?$")
+ parts := parser.FindStringSubmatch(input)
+ if len(parts) < 2 {
+ usage()
+ return
+ }
+ dice, _ := strconv.Atoi(parts[1])
+ die_size, _ := strconv.Atoi(parts[2])
+ mod := 0
+ if len(parts) > 3 {
+ mod, _ = strconv.Atoi(parts[3])
+ }
+ if dice < 1 {
+ dice = 1
+ }
+
+ total := 0
+ for i := 0; i < dice; i++ {
+ v := rand.Intn(die_size) + 1
+ fmt.Printf("%d+", v)
+ total += v
+ }
+ total += mod
+ fmt.Printf("%d = %d\n", mod, total)
+}
+
+func main() {
+ rand.Seed(time.Now().UTC().UnixNano())
+ if len(os.Args) == 1 {
+ usage()
+ }
+ args := os.Args[1:]
+ for _, arg := range args {
+ roll(arg)
+ }
+}