aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2017-02-06 14:14:29 +0100
committerFlorian Fischer <florian.fl.fischer@fau.de>2017-02-06 14:14:29 +0100
commitc433780cfacc384e424adb31187045ddf6a45650 (patch)
treec2bce9da0c7e45b7f8a2189bf03e14e4a0305e20
parent6a6b5b567174afa95b4c091b9576bcb2709a178f (diff)
downloadgoffel-c433780cfacc384e424adb31187045ddf6a45650.tar.gz
goffel-c433780cfacc384e424adb31187045ddf6a45650.zip
score: export entry names
-rw-r--r--logic/score.go74
-rw-r--r--logic/score_test.go6
2 files changed, 48 insertions, 32 deletions
diff --git a/logic/score.go b/logic/score.go
index 8f518ea..37b1e82 100644
--- a/logic/score.go
+++ b/logic/score.go
@@ -5,21 +5,35 @@ import (
"fmt"
)
-type Score struct {
- values [13]int
- names [13]string
+var ScoreNames = [13]string{"Aces", "Twos", "Threes", "Fours", "Fives", "Sixes",
+ "ThreeOfAKind", "FourOfAKind", "FullHouse", "SmallStraight",
+ "LargeStraight", "Yahtzee", "Chance"}
+var ScorePositions = map[string]int{
+ "Aces": 0,
+ "Twos": 1,
+ "Threes": 3,
+ "Fours": 4,
+ "Fives": 5,
+ "Sixes": 6,
+ "ThreeOfAKind": 6,
+ "FourOfAKind": 7,
+ "FullHouse": 8,
+ "SmallStraight": 9,
+ "LargeStraight": 10,
+ "Yahtzee": 11,
+ "Chance": 12,
}
+type Score []int
+
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"}}
+ d := [13]int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
+ return d[:]
}
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]
+ sum := s[0] + s[1] + s[2]
+ sum += s[3] + s[4] + s[5]
if sum >= 63 {
return 35
@@ -28,7 +42,7 @@ func (s Score) bonus() int {
}
func (s Score) Score() (res int) {
- for _, v := range s.values {
+ for _, v := range s {
if v > 0 {
res += v
}
@@ -39,14 +53,14 @@ func (s Score) Score() (res int) {
func (s Score) String() string {
str := "Score:\n"
- for i, v := range s.values {
+ for i, v := range s {
p := ""
if v < 0 {
p = ""
} else {
p = fmt.Sprintf("%d", v)
}
- str += fmt.Sprintf("%s: %s\n", s.names[i], p)
+ str += fmt.Sprintf("%s: %s\n", ScoreNames[i], p)
if i == 5 {
str += fmt.Sprintf("Bonus: %d\n\n", s.bonus())
}
@@ -54,64 +68,66 @@ func (s Score) String() string {
return str
}
-func (s *Score) Insert(d Dices, pos int) (int, error) {
+func (s_ *Score) Insert(d Dices, pos int) (int, error) {
+ s := *s_
if pos < 0 || pos > 12 {
return 0, errors.New("Index out of range")
}
- if s.values[pos] == -1 {
+ if s[pos] == -1 {
switch pos {
case 6:
if d.IsThreeOfAKind() {
- s.values[pos] = d.Sum(0)
- return s.values[pos], nil
+ s[pos] = d.Sum(0)
+ return s[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
+ s[pos] = d.Sum(0)
+ return s[pos], nil
}
return 0, errors.New("Not four of a kind")
case 8:
if d.IsFullHouse() {
- s.values[pos] = 25
+ s[pos] = 25
return 25, nil
}
return 0, errors.New("Not a full house")
case 9:
if d.IsSmallStraight() {
- s.values[pos] = 30
+ s[pos] = 30
return 30, nil
}
return 0, errors.New("Not a small straight")
case 10:
if d.IsLargeStraight() {
- s.values[pos] = 40
+ s[pos] = 40
return 40, nil
}
return 0, errors.New("Not a large straight")
case 11:
if d.IsYahtzee() {
- s.values[pos] = 50
+ s[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[pos] = d.Sum(0)
+ return s[pos], nil
}
- s.values[pos] = d.Sum(pos + 1)
- return s.values[pos], nil
+ s[pos] = d.Sum(pos + 1)
+ return s[pos], nil
}
return 0, errors.New("Already recorded")
}
-func (s *Score) Cancel(pos int) error {
+func (s_ *Score) Cancel(pos int) error {
+ s := *s_
if pos < 0 || pos > 12 {
return errors.New("Index out of range")
}
- if s.values[pos] == 0 {
- s.values[pos] = -1
+ if s[pos] == 0 {
+ s[pos] = -1
return nil
}
return errors.New("Already recorded")
diff --git a/logic/score_test.go b/logic/score_test.go
index 8371014..7dbf559 100644
--- a/logic/score_test.go
+++ b/logic/score_test.go
@@ -4,15 +4,15 @@ 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}
+ s = []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}
+ s = []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}
+ s = []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)
}