forked from feyeleanor/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice_pool.go
57 lines (46 loc) · 789 Bytes
/
dice_pool.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
package dice
import "math/rand"
type Filter func(int) bool
type DicePool struct {
sides int
number int
values []int
}
func NewDicePool(sides, number int) *DicePool {
return &DicePool{
sides: sides,
number: number,
}
}
func (d *DicePool) rand() int {
return rand.Intn(d.sides) + 1
}
func (d DicePool) Values() (r []int) {
r = make([]int, len(d.values))
copy(r, d.values)
return
}
func (d DicePool) Sum() (r int) {
for _, v := range d.values {
r += v
}
return
}
func (d DicePool) Select(f Filter) (r int) {
for _, v := range d.values {
if f(v) {
r++
}
}
return
}
func (d *DicePool) Roll() {
if d.number > 0 {
if cap(d.values) == 0 {
d.values = make([]int, d.number)
}
for n := d.number - 1; n > -1; n-- {
d.values[n] = d.rand()
}
}
}