-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.go
More file actions
316 lines (281 loc) · 6.1 KB
/
Copy pathgame.go
File metadata and controls
316 lines (281 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package yahtzee
import (
"errors"
"math/rand/v2"
"sort"
)
// Category represents a Yahtzee scoring category.
type Category int
const (
Ones Category = iota
Twos
Threes
Fours
Fives
Sixes
ThreeOfAKind
FourOfAKind
FullHouse
SmallStraight
LargeStraight
YahtzeeScore
Chance
NumCategories // sentinel = 13
)
var categoryNames = [NumCategories]string{
"Ones", "Twos", "Threes", "Fours", "Fives", "Sixes",
"Three of a Kind", "Four of a Kind", "Full House",
"Small Straight", "Large Straight", "Yahtzee", "Chance",
}
// Name returns the display name of the category.
func (c Category) Name() string {
if c >= 0 && c < NumCategories {
return categoryNames[c]
}
return "Unknown"
}
// IsUpper returns true for the upper section (Ones through Sixes).
func (c Category) IsUpper() bool {
return c >= Ones && c <= Sixes
}
// Dice holds the values and hold state of 5 dice.
type Dice struct {
Values [5]int
Held [5]bool
}
// Scorecard tracks which categories have been scored and their values.
type Scorecard struct {
Scores [NumCategories]int
Used [NumCategories]bool
}
// Game holds the complete state of a Yahtzee game.
type Game struct {
Dice Dice
Scorecard Scorecard
RollsLeft int
Turn int
YahtzeeBonuses int
GameOver bool
}
// NewGame creates a fresh game ready to play.
func NewGame() *Game {
return &Game{
RollsLeft: 3,
Turn: 1,
}
}
// Roll rolls all non-held dice. On the first roll of a turn, held flags are cleared.
func (g *Game) Roll() {
if !g.CanRoll() {
return
}
if g.RollsLeft == 3 {
g.Dice.Held = [5]bool{}
}
for i := 0; i < 5; i++ {
if !g.Dice.Held[i] {
g.Dice.Values[i] = rand.IntN(6) + 1
}
}
g.RollsLeft--
}
// CanRoll returns true if the player has rolls remaining.
func (g *Game) CanRoll() bool {
return g.RollsLeft > 0
}
// ToggleHold flips the held state of the die at the given index.
func (g *Game) ToggleHold(index int) {
if index < 0 || index > 4 || !g.CanHold() {
return
}
g.Dice.Held[index] = !g.Dice.Held[index]
}
// CanHold returns true if at least one roll has been made this turn.
func (g *Game) CanHold() bool {
return g.RollsLeft < 3
}
// CanScore returns true if at least one roll has been made this turn.
func (g *Game) CanScore() bool {
return g.RollsLeft < 3
}
// Score assigns the current dice to the given category.
func (g *Game) Score(cat Category) error {
if cat < 0 || cat >= NumCategories {
return errors.New("invalid category")
}
if g.Scorecard.Used[cat] {
return errors.New("category already used")
}
if !g.CanScore() {
return errors.New("must roll at least once before scoring")
}
// Check Yahtzee bonus: current dice are Yahtzee, YahtzeeScore already used with 50.
if isYahtzee(g.Dice.Values) && g.Scorecard.Used[YahtzeeScore] && g.Scorecard.Scores[YahtzeeScore] == 50 {
g.YahtzeeBonuses++
}
g.Scorecard.Scores[cat] = ScoreCategory(g.Dice.Values, cat)
g.Scorecard.Used[cat] = true
g.Turn++
g.RollsLeft = 3
g.Dice.Held = [5]bool{}
g.Dice.Values = [5]int{}
if g.Turn > 13 {
g.GameOver = true
}
return nil
}
// ScoreCategory calculates the score for a set of dice in a given category.
func ScoreCategory(dice [5]int, cat Category) int {
switch cat {
case Ones:
return scoreUpper(dice, 1)
case Twos:
return scoreUpper(dice, 2)
case Threes:
return scoreUpper(dice, 3)
case Fours:
return scoreUpper(dice, 4)
case Fives:
return scoreUpper(dice, 5)
case Sixes:
return scoreUpper(dice, 6)
case ThreeOfAKind:
return scoreNOfAKind(dice, 3)
case FourOfAKind:
return scoreNOfAKind(dice, 4)
case FullHouse:
return scoreFullHouse(dice)
case SmallStraight:
return scoreSmallStraight(dice)
case LargeStraight:
return scoreLargeStraight(dice)
case YahtzeeScore:
return scoreYahtzee(dice)
case Chance:
return scoreChance(dice)
case NumCategories:
// Sentinel value; not a real category.
return 0
}
return 0
}
func scoreUpper(dice [5]int, target int) int {
total := 0
for _, v := range dice {
if v == target {
total += v
}
}
return total
}
func scoreNOfAKind(dice [5]int, n int) int {
c := counts(dice)
for v := 1; v <= 6; v++ {
if c[v] >= n {
return scoreChance(dice)
}
}
return 0
}
func scoreFullHouse(dice [5]int) int {
c := counts(dice)
hasTwo, hasThree := false, false
for v := 1; v <= 6; v++ {
if c[v] == 2 {
hasTwo = true
}
if c[v] == 3 {
hasThree = true
}
}
if hasTwo && hasThree {
return 25
}
return 0
}
func scoreSmallStraight(dice [5]int) int {
has := [7]bool{}
for _, v := range dice {
has[v] = true
}
for start := 1; start <= 3; start++ {
if has[start] && has[start+1] && has[start+2] && has[start+3] {
return 30
}
}
return 0
}
func scoreLargeStraight(dice [5]int) int {
s := sortedDice(dice)
if (s == [5]int{1, 2, 3, 4, 5}) || (s == [5]int{2, 3, 4, 5, 6}) {
return 40
}
return 0
}
func scoreYahtzee(dice [5]int) int {
if isYahtzee(dice) {
return 50
}
return 0
}
func scoreChance(dice [5]int) int {
total := 0
for _, v := range dice {
total += v
}
return total
}
// UpperTotal returns the sum of scored upper section categories.
func (s *Scorecard) UpperTotal() int {
total := 0
for cat := Ones; cat <= Sixes; cat++ {
if s.Used[cat] {
total += s.Scores[cat]
}
}
return total
}
// UpperBonus returns 35 if the upper total is 63 or more, else 0.
func (s *Scorecard) UpperBonus() int {
if s.UpperTotal() >= 63 {
return 35
}
return 0
}
// LowerTotal returns the sum of scored lower section categories.
func (s *Scorecard) LowerTotal() int {
total := 0
for cat := ThreeOfAKind; cat <= Chance; cat++ {
if s.Used[cat] {
total += s.Scores[cat]
}
}
return total
}
// GrandTotal returns the complete game score.
func (g *Game) GrandTotal() int {
return g.Scorecard.UpperTotal() + g.Scorecard.UpperBonus() +
g.Scorecard.LowerTotal() + g.YahtzeeBonuses*100
}
func counts(dice [5]int) [7]int {
var c [7]int
for _, v := range dice {
if v >= 1 && v <= 6 {
c[v]++
}
}
return c
}
func sortedDice(dice [5]int) [5]int {
s := dice
sort.Ints(s[:])
return s
}
func isYahtzee(dice [5]int) bool {
for i := 1; i < 5; i++ {
if dice[i] != dice[0] {
return false
}
}
return dice[0] >= 1
}