From 62f66bd55084a2300196be07b11098dbd1fc3c9e Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 27 Jan 2017 18:19:28 +0100 Subject: change package goffel into logic --- goffel/dices.go | 95 ------------------------------------------- goffel/dices_test.go | 112 --------------------------------------------------- logic/dices.go | 98 ++++++++++++++++++++++++++++++++++++++++++++ logic/dices_test.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 207 deletions(-) delete mode 100644 goffel/dices.go delete mode 100644 goffel/dices_test.go create mode 100644 logic/dices.go create mode 100644 logic/dices_test.go diff --git a/goffel/dices.go b/goffel/dices.go deleted file mode 100644 index 113ccb0..0000000 --- a/goffel/dices.go +++ /dev/null @@ -1,95 +0,0 @@ -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 deleted file mode 100644 index 4593fac..0000000 --- a/goffel/dices_test.go +++ /dev/null @@ -1,112 +0,0 @@ -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) - } - } -} 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) +} diff --git a/logic/dices_test.go b/logic/dices_test.go new file mode 100644 index 0000000..a18ecca --- /dev/null +++ b/logic/dices_test.go @@ -0,0 +1,112 @@ +package logic + +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) + } + } +} -- cgit v1.2.3