aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2017-01-27 18:20:16 +0100
committerFlorian Fischer <florian.fl.fischer@fau.de>2017-01-27 18:20:16 +0100
commita1cd861f29a2805cf86c807d9a460151c18efd2c (patch)
treec131023f88ade1a53bec8e9edb6f22816fc8039b
parent62f66bd55084a2300196be07b11098dbd1fc3c9e (diff)
downloadgoffel-a1cd861f29a2805cf86c807d9a460151c18efd2c.tar.gz
goffel-a1cd861f29a2805cf86c807d9a460151c18efd2c.zip
add Score implementation
-rw-r--r--logic/score.go107
-rw-r--r--logic/score_test.go110
2 files changed, 217 insertions, 0 deletions
diff --git a/logic/score.go b/logic/score.go
new file mode 100644
index 0000000..65eac12
--- /dev/null
+++ b/logic/score.go
@@ -0,0 +1,107 @@
+package logic
+
+import (
+ "errors"
+ "fmt"
+)
+
+type Score struct {
+ values [13]int
+ names [13]string
+}
+
+func NewScore() Score {
+ return Score{values: [13]int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+ names: [13]string{"Aces", "Twos", "Threes", "Fours", "Fives", "Sixes",
+ "ThreeOfAKind", "FourOfAKind", "FullHouse", "SmallStraight",
+ "LargeStraight", "Yahtzee", "Chance"}}
+}
+
+func (s Score) bonus() int {
+ sum := s.values[0] + s.values[1] + s.values[2]
+ sum += s.values[3] + s.values[4] + s.values[5]
+
+ if sum >= 63 {
+ return 35
+ }
+ return 0
+}
+
+func (s Score) Score() (res int) {
+ for _, v := range s.values {
+ if v > 0 {
+ res += v
+ }
+ }
+ res += s.bonus()
+ return
+}
+
+func (s Score) String() string {
+ str := "Score:\n"
+ for i, v := range s.values {
+ p := ""
+ if v < 0 {
+ p = ""
+ } else {
+ p = fmt.Sprintf("%d", v)
+ }
+ str += fmt.Sprintf("%s: %s\n", s.names[i], p)
+ if i == 5 {
+ str += fmt.Sprintf("Bonus: %d\n\n", s.bonus())
+ }
+ }
+ return str
+}
+
+func (s *Score) Insert(d Dices, pos int) (int, error) {
+ if pos < 0 || pos > 12 {
+ return 0, errors.New("Index out of range")
+ }
+ if s.values[pos] == -1 {
+ switch pos {
+ case 6:
+ if d.IsThreeOfAKind() {
+ s.values[pos] = d.Sum(0)
+ return s.values[pos], nil
+ }
+ return 0, errors.New("Not three of a kind")
+ case 7:
+ if d.IsFourOfAKind() {
+ s.values[pos] = d.Sum(0)
+ return s.values[pos], nil
+ }
+ return 0, errors.New("Not four of a kind")
+ case 8:
+ if d.IsFullHouse() {
+ s.values[pos] = 25
+ return 25, nil
+ }
+ return 0, errors.New("Not a full house")
+ case 9:
+ if d.IsSmallStraight() {
+ s.values[pos] = 30
+ return 30, nil
+ }
+ return 0, errors.New("Not a small straight")
+ case 10:
+ if d.IsLargeStraight() {
+ s.values[pos] = 40
+ return 40, nil
+ }
+ return 0, errors.New("Not a large straight")
+ case 11:
+ if d.IsYahtzee() {
+ s.values[pos] = 50
+ return 50, nil
+ }
+ return 0, errors.New("Not a yahtzee")
+ case 12:
+ s.values[pos] = d.Sum(0)
+ return s.values[pos], nil
+ }
+ s.values[pos] = d.Sum(pos + 1)
+ return s.values[pos], nil
+ }
+ return 0, errors.New("Already recorded")
+}
diff --git a/logic/score_test.go b/logic/score_test.go
new file mode 100644
index 0000000..8371014
--- /dev/null
+++ b/logic/score_test.go
@@ -0,0 +1,110 @@
+package logic
+
+import "testing"
+
+func TestScore(t *testing.T) {
+ s := NewScore()
+ s.values = [13]int{1, 2, 3, 4, 5, 6, 23, 27, 25, 30, 40, -1, 26}
+ if s.Score() != 192 {
+ t.Errorf("Score of \n%v should be 192", s)
+ }
+ s.values = [13]int{3, 6, 9, 12, 15, 18, 23, 27, 25, 30, 40, -1, 26}
+ if s.Score() != 269 {
+ t.Errorf("Score of \n%v should be 269", s)
+ }
+ s.values = [13]int{1, 8, 6, 8, 15, 12, 23, 15, -1, 30, 40, -1, 26}
+ if s.Score() != 184 {
+ t.Errorf("Score of \n%v should be 184", s)
+ }
+}
+
+func TestInsert(t *testing.T) {
+ s := NewScore()
+ d := Dices{1, 2, 3, 4, 5}
+ p, err := s.Insert(d, 0)
+ if p != 1 || err != nil {
+ t.Errorf("Inserting %v as aces should return 1", d)
+ }
+
+ d = Dices{1, 2, 2, 3, 4}
+ p, err = s.Insert(d, 1)
+ if p != 4 || err != nil {
+ t.Errorf("Inserting %v as twos should return 4", d)
+ }
+
+ p, err = s.Insert(d, 1)
+ if p != 0 || err == nil {
+ t.Errorf("Inserting %v as twos again should fail", d)
+ }
+
+ d = Dices{1, 2, 2, 4, 4}
+ p, err = s.Insert(d, 2)
+ if p != 0 || err != nil {
+ t.Errorf("Inserting %v as threes should return 0", d)
+ }
+
+ d = Dices{1, 2, 2, 6, 6}
+ p, err = s.Insert(d, 5)
+ if p != 12 || err != nil {
+ t.Errorf("Inserting %v as sixes should return 12", d)
+ }
+
+ d = Dices{1, 2, 6, 6, 6}
+ p, err = s.Insert(d, 6)
+ if p != 21 || err != nil {
+ t.Errorf("Inserting %v as three of a kind should return 21", d)
+ }
+
+ d = Dices{1, 1, 2, 2, 2}
+ p, err = s.Insert(d, 7)
+ if p != 0 || err == nil {
+ t.Errorf("Inserting %v as four of a kind should fail", d)
+ }
+
+ d = Dices{1, 2, 2, 2, 2}
+ p, err = s.Insert(d, 7)
+ if p != 9 || err != nil {
+ t.Errorf("Inserting %v as four of a kind should return 9", d)
+ }
+
+ d = Dices{1, 1, 2, 2, 2}
+ p, err = s.Insert(d, 8)
+ if p != 25 || err != nil {
+ t.Errorf("Inserting %v as full house should return 25", d)
+ }
+
+ d = Dices{1, 2, 3, 4, 5}
+ p, err = s.Insert(d, 9)
+ if p != 30 || err != nil {
+ t.Errorf("Inserting %v as small straight should return 30", d)
+ }
+
+ d = Dices{1, 2, 3, 4, 5}
+ p, err = s.Insert(d, 10)
+ if p != 40 || err != nil {
+ t.Errorf("Inserting %v as large straight should return 40", d)
+ }
+
+ d = Dices{2, 2, 3, 4, 5}
+ p, err = s.Insert(d, 10)
+ if p != 0 || err == nil {
+ t.Errorf("Inserting %v as large straight should fail", d)
+ }
+
+ d = Dices{2, 2, 2, 2, 2}
+ p, err = s.Insert(d, 11)
+ if p != 50 || err != nil {
+ t.Errorf("Inserting %v as yahtzee should return 50", d)
+ }
+
+ d = Dices{1, 3, 3, 5, 6}
+ p, err = s.Insert(d, 12)
+ if p != 18 || err != nil {
+ t.Errorf("Inserting %v as chance should return 18", d)
+ }
+
+ p, err = s.Insert(d, 200)
+ if p != 0 || err == nil {
+ t.Errorf("Inserting %v at index 200 should fail", d)
+ }
+}