diff options
Diffstat (limited to 'logic/dices.go')
| -rw-r--r-- | logic/dices.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/logic/dices.go b/logic/dices.go new file mode 100644 index 0000000..07c4a98 --- /dev/null +++ b/logic/dices.go @@ -0,0 +1,98 @@ +package logic + +import ( + "fmt" + "math/rand" + "sort" +) + +var diceRunes = [7]string{"", "\u2680", "\u2681", "\u2682", "\u2683", "\u2684", "\u2685"} + +type Dices [5]int + +func (d *Dices) Roll(i []int) { + if i == nil { + i = []int{1,2,3,4,5} + } + for _, v := range i { + d[v-1] = rand.Int()%6 + 1 + } + sort.Ints(d[0:]) +} + +func (d Dices) String() string { + s := "" + for _, v := range d { + s += diceRunes[v] + " " + } + return fmt.Sprintf("%s", s) +} + +func (d Dices) Sum(i int) int { + s := 0 + for _, v := range d { + if v == i || i == 0 { + s += v + } + } + return s +} + +func (d Dices) sumSame() []int { + s := []int{1} + idx := 0 + for i := 1; i < 5; i++ { + if d[i] == d[i-1] { + s[idx]++ + } else { + s = append(s, 1) + idx++ + } + } + sort.Sort(sort.Reverse(sort.IntSlice(s))) + return s +} + +func (d Dices) IsThreeOfAKind() bool { + return d.sumSame()[0] >= 3 +} + +func (d Dices) IsFourOfAKind() bool { + return d.sumSame()[0] >= 4 +} + +func (d Dices) IsFullHouse() bool { + s := d.sumSame() + return s[0] == 3 && s[1] == 2 +} + +func (d Dices) IsYahtzee() bool { + return d.sumSame()[0] >= 5 +} + +func (d Dices) isStraight(length int) bool { + s := 1 + max_s := s + for i := 0; i < 4; i++ { + if d[i]+1 == d[i+1] { + s++ + } else if d[i] != d[i+1] { + if s > max_s { + max_s = s + } + s = 1 + } + } + if s > max_s { + max_s = s + } + return max_s >= length +} + +func (d Dices) IsSmallStraight() bool { + return d.isStraight(4) +} + +func (d Dices) IsLargeStraight() bool { + return d.isStraight(5) +} |
