-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice.go
58 lines (48 loc) · 1.31 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
package main
import (
"math/rand"
"sort"
)
// DoRolls takes a RollSpec and perform all of the random number generation
// and math to fulfill the request.
func DoRolls(spec RollSpec) RollResults {
var localResults RollResults
var eachSet int64
for eachSet = 0; eachSet < spec.Times; eachSet++ {
localResults.Rolls = append(localResults.Rolls, roll(spec))
localResults.Count++
}
return localResults
}
func roll(spec RollSpec) SetResult {
var result SetResult
var eachDie int64
var sidesInt = int(spec.Sides)
for eachDie = 0; eachDie < spec.DieCount; eachDie++ {
die := rand.Intn(sidesInt) + 1
result.Count++
if len(result.Dies) == 0 || die >= result.Dies[0] {
result.Dies = append(result.Dies, die)
} else {
// We always want to have the smallest value at the
// zero index
result.Dies = append([]int{die}, result.Dies...)
}
}
// Calculate the total, accounting for best-of modifiers
countIdx := len(result.Dies) - 1
sortedRolls := make([]int, len(result.Dies))
copy(sortedRolls, result.Dies)
sort.Ints(sortedRolls)
var counted int64
countLimit := int(spec.BestOf)
if countLimit == 0 {
countLimit = int(spec.DieCount)
}
for counted = 0; counted < int64(countLimit); counted++ {
result.Total += sortedRolls[countIdx]
countIdx--
}
result.Total += int(spec.Modifier)
return result
}