/* Copyright (C) 2011, 2013-2014 Luke Shumaker */ package main import ( "fmt" "math/rand" "os" "regexp" "strconv" "time" ) func usage() { fmt.Printf("Arguments are in the format []d[+]\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) } }