-
Notifications
You must be signed in to change notification settings - Fork 3
/
dice.go
175 lines (148 loc) · 3.9 KB
/
dice.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"errors"
"fmt"
"math/rand"
"strconv"
"strings"
log "gopkg.in/inconshreveable/log15.v2"
)
// Roll represents a single rolled unit (which may be multiple dice)
type Roll struct {
number int
sides int
operator byte
operand int
}
func (d Roll) calculate() string {
if d.sides == 0 {
return fmt.Sprintf("1 %d-sided die: %d", d.number, rand.Intn(d.number)+1)
}
var result int
for i := 1; i <= d.number; i++ {
result += rand.Intn(d.sides) + 1
}
if d.operand != 0 {
if d.operator == '+' {
result += d.operand
}
if d.operator == '-' {
result -= d.operand
}
return fmt.Sprintf("%d %d-sided dice (%c%d): %d", d.number, d.sides, d.operator, d.operand, result)
}
return fmt.Sprintf("%d %d-sided dice: %d", d.number, d.sides, result)
}
func rollDice(input string) string {
inputDice := diceRegex.FindStringSubmatch(input)
dice, err := validateDice(input, inputDice)
if err != nil {
return err.Error()
}
log.Debug("RollDice", "Input", fmt.Sprintf("%dd%d%c%d", dice.number, dice.sides, dice.operator, dice.operand))
return dice.calculate()
}
func validateDice(input string, dice []string) (Roll, error) {
log.Debug("Validate Dice", "Input", input, "Dice array", dice)
failure := "Try something like '!roll d4', '!roll 3d8', '!roll 2d6+2'"
// Input didn't pass the regex at all so dice is empty
if strings.TrimSpace(input) == "" || len(dice) == 0 {
return Roll{}, errors.New(failure)
}
//cuts out weirdly formed things
failCases := []bool{
dice[2] == "" && len(input) > len(dice[1]),
strings.Contains(input, "d") && dice[2] == "",
strings.Contains(input, "-") && dice[3] == "",
strings.Contains(input, "+") && dice[3] == ""}
for _, failCase := range failCases {
if failCase {
return Roll{}, errors.New(failure)
}
}
var (
sidesDice int
operator byte
operand int
numDice = 1
err error
)
if dice[1] != "" {
numDice, err = strconv.Atoi(dice[1])
if err != nil {
return Roll{}, errors.New("malformed roll")
}
if numDice > 99 {
return Roll{}, errors.New("malformed roll (max dice is 99)")
}
}
if dice[2] != "" {
sidesDice, err = strconv.Atoi(dice[2])
if err != nil {
return Roll{}, errors.New("malformed roll")
}
if sidesDice > 100 {
return Roll{}, errors.New("malformed roll (max sides is 100)")
}
if sidesDice <= 1 {
return Roll{}, errors.New("malformed roll (min sides is 2)")
}
}
if dice[3] != "" {
operator = dice[3][0]
operand, err = strconv.Atoi(dice[3][1:])
if err != nil {
return Roll{}, errors.New("malformed roll")
}
if operand > 1000 {
return Roll{}, errors.New("malformed roll (max operand is 1000)")
}
}
wellFormedDice := Roll{numDice, sidesDice, operator, operand}
return wellFormedDice, nil
}
func flipCoin(input string) string {
coinCount, err := parseCoinCount(input)
if err != nil {
return err.Error()
}
flips := make([]int, coinCount)
for i := 0; i < coinCount; i++ {
flips[i] = rand.Intn(2)
}
return formatFlips(flips, coinCount)
}
func parseCoinCount(input string) (int, error) {
coinMaximum := 50
coins := coinRegex.FindStringSubmatch(input)
if coins[1] == "" {
// no number specified - implicit one flip
return 1, nil
}
numCoins, err := strconv.Atoi(coins[1])
if err != nil {
return 0, errors.New("malformed coin toss")
}
if numCoins > coinMaximum {
return 0, errors.New("malformed coin toss (max count is 50)")
}
if numCoins < 1 {
return 0, errors.New("malformed coin toss (min count is 1)")
}
return numCoins, nil
}
func formatFlips(flips []int, count int) string {
prefix := ""
coinNames := []string{"Heads", "Tails"}
joiner := ", "
if count > 5 {
prefix = fmt.Sprintf("%d coins: ", count)
coinNames = []string{"H", "T"}
joiner = ""
}
formatted := make([]string, len(flips))
for i := 0; i < len(formatted); i++ {
formatted[i] = coinNames[flips[i]]
}
return fmt.Sprintf("%s%s.", prefix, strings.Join(formatted, joiner))
}