summaryrefslogtreecommitdiff
path: root/roll.go
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2013-09-07 15:03:57 -0400
committerLuke Shumaker <LukeShu@sbcglobal.net>2013-09-07 15:03:57 -0400
commit5fe5ef3d949f01ad68a3d683d6d69ffecc364fac (patch)
tree579a699d6794ad200be01113251cd07c23d6868a /roll.go
parent23ebdbace364ffc2e27403e103594c12c0002b5b (diff)
rewrite roll in Go
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)
+ }
+}