Repository navigation
Expand file tree
/
Copy pathroll.go
More file actions
96 lines (77 loc) · 1.37 KB
/
Copy pathroll.go
File metadata and controls
96 lines (77 loc) · 1.37 KB
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
// Copy from game/entropy/models/roll.go
package main
import (
"fmt"
"math/rand"
"strconv"
"strings"
)
type Rolls struct {
dieValue int
nbDice int
rolls []bonus
extra []int
}
func (r Rolls) String() string {
var out string
rs := make([]string, 0, len(r.rolls))
for _, v := range r.rolls {
s := strconv.Itoa(v.Value)
if v.Type != "" {
s += " (" + v.Type + ")"
}
rs = append(rs, s)
}
out += strings.Join(rs, " + ")
out += " = " + strconv.FormatInt(int64(r.Total()), 10)
return out
}
func (r Rolls) Total() int {
var total int
for _, v := range r.rolls {
total += int(v.Value)
}
return total
}
// TODO: refactor/rename/cleanup
type bonus struct {
Value int
Type string
}
func (r *Rolls) Extra(b bonus) *Rolls {
if b.Value == 0 {
return r
}
r.extra = append(r.extra, b.Value)
r.rolls = append(r.rolls, b)
return r
}
func roll(dice, n int) *Rolls {
var rolls []bonus
var all int
for i := 0; i < n; i++ {
roll := 1 + rand.Intn(dice)
all += roll
}
rolls = append(rolls, bonus{Value: all, Type: fmt.Sprintf("%dd%d", n, dice)})
return &Rolls{
dieValue: dice,
nbDice: n,
rolls: rolls,
}
}
func d4(n int) *Rolls {
return roll(4, n)
}
func d6(n int) *Rolls {
return roll(6, n)
}
func d8(n int) *Rolls {
return roll(8, n)
}
func d12(n int) *Rolls {
return roll(12, n)
}
func d20(n int) *Rolls {
return roll(20, n)
}