diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2017-01-27 17:06:12 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2017-01-27 17:06:12 +0100 |
| commit | 713547fcba74294cef18ad573f1cb9a93e3b6795 (patch) | |
| tree | fcf0bf7f47bc194bf533996fb717f983102308bb | |
| download | goffel-713547fcba74294cef18ad573f1cb9a93e3b6795.tar.gz goffel-713547fcba74294cef18ad573f1cb9a93e3b6795.zip | |
add dices implementation
The type Dices represent the dice set. It also implements the
game logic checks. (IsFullHouse, ...)
| -rw-r--r-- | goffel/dices.go | 95 | ||||
| -rw-r--r-- | goffel/dices_test.go | 112 |
2 files changed, 207 insertions, 0 deletions
diff --git a/goffel/dices.go b/goffel/dices.go new file mode 100644 index 0000000..113ccb0 --- /dev/null +++ b/goffel/dices.go @@ -0,0 +1,95 @@ +package goffel + +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) { + 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) +} diff --git a/goffel/dices_test.go b/goffel/dices_test.go new file mode 100644 index 0000000..4593fac --- /dev/null +++ b/goffel/dices_test.go @@ -0,0 +1,112 @@ +package goffel + +import "testing" + +func TestSum(t *testing.T) { + d := Dices{1, 1, 1, 2, 6} + if d.Sum(1) != 3 { + t.Errorf("Sum(1) of %v should be 3", d) + } + d = Dices{1, 1, 1, 2, 6} + if d.Sum(2) != 2 { + t.Errorf("Sum(2) of %v should be 2", d) + } + d = Dices{1, 1, 1, 2, 6} + if d.Sum(0) != 11 { + t.Errorf("Sum(0) of %v should be 11", d) + } + d = Dices{1, 1, 1, 2, 6} + if d.Sum(3) != 0 { + t.Errorf("Sum(3) of %v should be 0", d) + } +} + +func TestIsThreeOfAKind(t *testing.T) { + dices := []Dices{Dices{1, 2, 3, 3, 3}, Dices{1, 4, 4, 4, 5}, Dices{2, 2, 2, 3, 6}, Dices{1, 1, 1, 1, 1}} + for _, d := range dices { + if !d.IsThreeOfAKind() { + t.Errorf("%v is three of a kind", d) + } + } + dices = []Dices{Dices{1, 2, 3, 3, 4}, Dices{1, 4, 4, 5, 5}, Dices{1, 2, 2, 3, 6}, Dices{1, 1, 2, 5, 6}} + for _, d := range dices { + if d.IsThreeOfAKind() { + t.Errorf("%v is not three of a kind", d) + } + } +} + +func TestIsFourOfAKind(t *testing.T) { + dices := []Dices{Dices{1, 3, 3, 3, 3}, Dices{1, 4, 4, 4, 4}, Dices{2, 2, 2, 2, 6}, Dices{1, 1, 1, 1, 1}} + for _, d := range dices { + if !d.IsFourOfAKind() { + t.Errorf("%v is four of a kind", d) + } + } + dices = []Dices{Dices{1, 2, 3, 3, 4}, Dices{1, 4, 4, 5, 5}, Dices{1, 2, 2, 3, 6}, Dices{1, 1, 2, 5, 6}} + for _, d := range dices { + if d.IsFourOfAKind() { + t.Errorf("%v is not four of a kind", d) + } + } +} + +func TestIsFullHouse(t *testing.T) { + dices := []Dices{Dices{1, 1, 3, 3, 3}, Dices{1, 1, 4, 4, 4}, Dices{2, 2, 2, 6, 6}} + for _, d := range dices { + if !d.IsFullHouse() { + t.Errorf("%v is a full house", d) + } + } + dices = []Dices{Dices{1, 2, 3, 3, 4}, Dices{1, 4, 4, 5, 5}, Dices{1, 2, 2, 3, 6}, Dices{1, 1, 2, 5, 6}} + for _, d := range dices { + if d.IsFullHouse() { + t.Errorf("%v is not a full house", d) + } + } +} + +func TestIsSmallStraight(t *testing.T) { + dices := []Dices{Dices{1, 2, 3, 4, 6}, Dices{1, 1, 2, 3, 4}, Dices{2, 3, 4, 5, 6}, Dices{1, 2, 3, 3, 4}} + for _, d := range dices { + if !d.IsSmallStraight() { + t.Errorf("%v is a small straight", d) + } + } + dices = []Dices{Dices{1, 2, 3, 3, 5}, Dices{1, 4, 4, 5, 5}, Dices{1, 2, 2, 3, 6}, Dices{1, 1, 2, 5, 6}} + for _, d := range dices { + if d.IsSmallStraight() { + t.Errorf("%v is not a small straight", d) + } + } +} + +func TestIsLargeStraight(t *testing.T) { + dices := []Dices{Dices{1, 2, 3, 4, 5}, Dices{2, 3, 4, 5, 6}} + for _, d := range dices { + if !d.IsLargeStraight() { + t.Errorf("%v is a large straight", d) + } + } + dices = []Dices{Dices{1, 2, 3, 3, 4}, Dices{1, 4, 4, 5, 5}, Dices{1, 2, 2, 3, 6}, Dices{1, 1, 2, 5, 6}} + for _, d := range dices { + if d.IsLargeStraight() { + t.Errorf("%v is not a large straight", d) + } + } +} + +func TestIsYahtzee(t *testing.T) { + dices := []Dices{Dices{3, 3, 3, 3, 3}, Dices{1, 1, 1, 1, 1}} + for _, d := range dices { + if !d.IsYahtzee() { + t.Errorf("%v is a yahtzee", d) + } + } + dices = []Dices{Dices{1, 2, 3, 3, 4}, Dices{1, 4, 4, 5, 5}, Dices{1, 2, 2, 3, 6}, Dices{1, 1, 2, 5, 6}} + for _, d := range dices { + if d.IsYahtzee() { + t.Errorf("%v is not a yahtzee", d) + } + } +} |
