-
Notifications
You must be signed in to change notification settings - Fork 1
/
dice.go
67 lines (54 loc) · 1.19 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
package roll
import (
crypto "crypto/rand"
"encoding/binary"
"fmt"
rand "math/rand"
"github.com/go-playground/validator/v10"
)
func init() {
var b [8]byte
_, err := crypto.Read(b[:])
if err != nil {
panic("cannot seed math/rand package with cryptographically secure random number generator")
}
// Generate cryptographically random seed
rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
}
var validate = validator.New()
// Die is the struct for all
// rollable dice types
type Die struct {
Min int `validate:"ltfield=Max"`
Max int `validate:"gtfield=Min"`
}
func NewDie(min, max int) (*Die, error) {
d := &Die{Min: min, Max: max}
err := validate.Struct(d)
if err != nil {
return nil, err
}
return d, nil
}
// String returns the string representation of
// the die
func (d Die) String() string {
return fmt.Sprintf("d%d", d.Max)
}
// Validate ensures that the Dice are in a valid
// configuration
func (d *Die) Validate() error {
return validate.Struct(d)
}
// Roll performs a roll of a single die
func (d *Die) Roll() int {
return rand.Intn(d.Max-d.Min+1) + d.Min
}
// NewD6 creates a new instance
// of the D6 dice type
func NewD6() *Die {
return &Die{
Min: 1,
Max: 6,
}
}