|
@@ -0,0 +1,54 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "math/rand"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+type Dice struct {
|
|
|
+ Number uint // Number of dice to roll
|
|
|
+}
|
|
|
+
|
|
|
+// Perform the rolls without the Pips
|
|
|
+func (d *Dice) Roll() (total, wild int, rolls []int) {
|
|
|
+ var die int
|
|
|
+ for range make([]byte, d.Number-1) {
|
|
|
+ die = rand.Intn(6) + 1
|
|
|
+ total += die
|
|
|
+ rolls = append(rolls, die)
|
|
|
+ }
|
|
|
+ wild = rand.Intn(6) + 1
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// Returns the text form for this Dice
|
|
|
+//
|
|
|
+// Without the Pips
|
|
|
+func (d *Dice) String() string {
|
|
|
+ return fmt.Sprintf("%dD", d.Number)
|
|
|
+}
|
|
|
+
|
|
|
+func NewDice(D uint) *Dice {
|
|
|
+ return &Dice{
|
|
|
+ Number: D,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func NewDiceFromString(text string) (*Dice, error) {
|
|
|
+ var (
|
|
|
+ number int
|
|
|
+ err error
|
|
|
+ )
|
|
|
+ if strings.Contains(text, "D") {
|
|
|
+ text = strings.ReplaceAll(text, "D", "")
|
|
|
+ }
|
|
|
+ number, err = strconv.Atoi(text)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return &Dice{
|
|
|
+ Number: uint(number),
|
|
|
+ }, nil
|
|
|
+}
|