From c4f76699aa1459564792395f272052fce6b433bd Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 18 Feb 2017 19:48:20 +0100 Subject: TIL dice is the plural of die --- logic/dice.go | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ logic/dice_test.go | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ logic/dices.go | 121 ---------------------------------------------------- logic/dices_test.go | 115 ------------------------------------------------- logic/score.go | 2 +- uis/interactive.go | 2 +- uis/server.go | 2 +- 7 files changed, 239 insertions(+), 239 deletions(-) create mode 100644 logic/dice.go create mode 100644 logic/dice_test.go delete mode 100644 logic/dices.go delete mode 100644 logic/dices_test.go diff --git a/logic/dice.go b/logic/dice.go new file mode 100644 index 0000000..a5cbade --- /dev/null +++ b/logic/dice.go @@ -0,0 +1,121 @@ +// Copyright (c) 2016 Florian Fischer. All rights reserved. +// Use of this source code is governed by a MIT license found in the LICENSE file. + +package logic + +import ( + "errors" + "fmt" + "math/rand" + "sort" +) + +var diceRunes = [7]string{"", "\u2680", "\u2681", "\u2682", "\u2683", "\u2684", "\u2685"} +var fancy = false + +func SetFancyPrint(f bool) { + fancy = f +} + +type Dice [5]int + +func NewDice() Dice { + d := Dice{} + d.Roll(nil) + return d +} + +func (d *Dice) 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 Dice) String() string { + s := "" + for _, v := range d { + if fancy { + s += diceRunes[v] + " " + } else { + s += fmt.Sprintf("%d ", v) + } + } + return fmt.Sprintf("%s", s) +} + +func (d Dice) Sum(i int) int { + s := 0 + for _, v := range d { + if v == i || i == 0 { + s += v + } + } + return s +} + +func (d Dice) 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 Dice) IsThreeOfAKind() bool { + return d.sumSame()[0] >= 3 +} + +func (d Dice) IsFourOfAKind() bool { + return d.sumSame()[0] >= 4 +} + +func (d Dice) IsFullHouse() bool { + s := d.sumSame() + return s[0] == 3 && s[1] == 2 +} + +func (d Dice) IsYahtzee() bool { + return d.sumSame()[0] >= 5 +} + +func (d Dice) 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 Dice) IsSmallStraight() bool { + return d.isStraight(4) +} + +func (d Dice) IsLargeStraight() bool { + return d.isStraight(5) +} diff --git a/logic/dice_test.go b/logic/dice_test.go new file mode 100644 index 0000000..2a6c67c --- /dev/null +++ b/logic/dice_test.go @@ -0,0 +1,115 @@ +// Copyright (c) 2016 Florian Fischer. All rights reserved. +// Use of this source code is governed by a MIT license found in the LICENSE file. + +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) + } + } +} diff --git a/logic/dices.go b/logic/dices.go deleted file mode 100644 index d6a8952..0000000 --- a/logic/dices.go +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright (c) 2016 Florian Fischer. All rights reserved. -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package logic - -import ( - "errors" - "fmt" - "math/rand" - "sort" -) - -var diceRunes = [7]string{"", "\u2680", "\u2681", "\u2682", "\u2683", "\u2684", "\u2685"} -var fancy = false - -func SetFancyPrint(f bool) { - fancy = f -} - -type Dices [5]int - -func NewDices() Dices { - d := Dices{} - d.Roll(nil) - return d -} - -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 { - s := "" - for _, v := range d { - if fancy { - s += diceRunes[v] + " " - } else { - s += fmt.Sprintf("%d ", 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 deleted file mode 100644 index 2a6c67c..0000000 --- a/logic/dices_test.go +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright (c) 2016 Florian Fischer. All rights reserved. -// Use of this source code is governed by a MIT license found in the LICENSE file. - -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) - } - } -} diff --git a/logic/score.go b/logic/score.go index 6d864b5..1e51571 100644 --- a/logic/score.go +++ b/logic/score.go @@ -71,7 +71,7 @@ func (s Score) String() string { return str } -func (s_ *Score) Insert(d Dices, pos int) (int, error) { +func (s_ *Score) Insert(d Dice, pos int) (int, error) { s := *s_ if pos < 0 || pos > 12 { return 0, fmt.Errorf("Position %d out of range", pos) diff --git a/uis/interactive.go b/uis/interactive.go index ab23971..e493878 100644 --- a/uis/interactive.go +++ b/uis/interactive.go @@ -20,7 +20,7 @@ type Interactive struct { players []intPlayer } -var dices Dices +var dices Dice var reader = bufio.NewReader(os.Stdin) func (i *Interactive) Run() { diff --git a/uis/server.go b/uis/server.go index 321b507..f4e51e8 100644 --- a/uis/server.go +++ b/uis/server.go @@ -161,7 +161,7 @@ func (c *client) getBest() ([]player, int) { } func (c *client) round(round int, wg *sync.WaitGroup) { - dice := logic.Dices{} + dice := logic.Dice{} var msg sMsg for _, p := range c.players { -- cgit v1.2.3