-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·126 lines (105 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import "os"
import "bytes"
import "fmt"
import "regexp"
import "strconv"
import "time"
import "math/rand"
type myRegexp struct {
*regexp.Regexp
}
var dice_expr_regex = myRegexp{regexp.MustCompile(`((?P<rolls>[0-9]*)d)(?P<die>[0-9]+)((?P<op>\+|-)(?P<additive>[0-9]+))?`)}
func main() {
rand.Seed(time.Now().Unix())
var explain bool
for _, cmd := range os.Args[1:] {
if cmd == "explain" {
explain = true
continue
} else if cmd == "roll" {
explain = false
continue
}
expr, plan := ComputeExpression(cmd, explain)
if explain {
fmt.Printf("%s Rolls {%s} : %d", cmd, plan, expr)
} else {
fmt.Printf("%s - %d", cmd, expr)
}
fmt.Println("")
}
}
func ParseInteger(st string, prm map[string]string, def int) int {
var ret = def
if value, ok := prm[st]; ok {
ret, _ = strconv.Atoi(value)
}
return ret
}
func ComputeExpression(cmd string, explain bool) (int, string) {
diceRoll := dice_expr_regex.FindStringSubmatchMap(cmd)
var buffer bytes.Buffer
var counter = 0
var opAdd = true
rolls := ParseInteger("rolls", diceRoll, 1)
die := ParseInteger("die", diceRoll, 0)
additive := ParseInteger("additive", diceRoll, 0)
if value, ok := diceRoll["op"]; ok {
if value == "+" {
opAdd = true
} else {
opAdd = false
}
}
if explain {
buffer.WriteString("( ")
}
die -= 1
for i := 1; i <= rolls; i++ {
var newRoll = rand.Intn(die) + 1
if explain {
buffer.WriteString(strconv.Itoa(newRoll))
if i != rolls {
buffer.WriteString(" + ")
} else {
buffer.WriteString(" ")
}
}
counter += newRoll
}
if explain {
buffer.WriteString(")")
}
if additive > 0 {
if opAdd {
if explain {
buffer.WriteString(" + ")
buffer.WriteString(strconv.Itoa(additive))
}
counter += additive
} else {
if explain {
buffer.WriteString(" - ")
buffer.WriteString(strconv.Itoa(additive))
}
counter -= additive
}
}
return counter, buffer.String()
}
func (r *myRegexp) FindStringSubmatchMap(s string) map[string]string {
captures := make(map[string]string)
match := r.FindStringSubmatch(s)
if match == nil {
return captures
}
for i, name := range r.SubexpNames() {
// Ignore the whole regexp match and unnamed groups
if i == 0 || name == "" {
continue
}
captures[name] = match[i]
}
return captures //return this. make a change.
}