aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2017-01-28 14:30:33 +0100
committerFlorian Fischer <florian.fl.fischer@fau.de>2017-01-28 14:30:33 +0100
commit7a2b66c2b67e662c5cf20aba79fdabab3a4baae9 (patch)
tree7236eb7193d316a63e7613b5b86845c001a2ded9
parent1ee0ff7f9ef4faf45db11ce3c56ccae633cd77ee (diff)
downloadgoffel-7a2b66c2b67e662c5cf20aba79fdabab3a4baae9.tar.gz
goffel-7a2b66c2b67e662c5cf20aba79fdabab3a4baae9.zip
dices.go: make Roll() more robust
-rw-r--r--logic/dices.go11
1 files changed, 8 insertions, 3 deletions
diff --git a/logic/dices.go b/logic/dices.go
index 07c4a98..43dca22 100644
--- a/logic/dices.go
+++ b/logic/dices.go
@@ -1,6 +1,7 @@
package logic
import (
+ "errors"
"fmt"
"math/rand"
"sort"
@@ -10,14 +11,18 @@ var diceRunes = [7]string{"", "\u2680", "\u2681", "\u2682", "\u2683", "\u2684",
type Dices [5]int
-func (d *Dices) Roll(i []int) {
- if i == nil {
- i = []int{1,2,3,4,5}
+func (d *Dices) Roll(i []int) error {
+ if i == nil || len(i) == 0 {
+ i = []int{1, 2, 3, 4, 5}
}
for _, v := range i {
+ if v > 6 || v < 1 {
+ return errors.New("index out of range")
+ }
d[v-1] = rand.Int()%6 + 1
}
sort.Ints(d[0:])
+ return nil
}
func (d Dices) String() string {