-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.go
46 lines (41 loc) · 1.08 KB
/
player.go
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
package rummy
import (
"github.com/timpalpant/rummy/deck"
"github.com/timpalpant/rummy/meld"
"github.com/timpalpant/rummy/scoring"
)
// player holds the state for a single player in a game of Rummy.
type player struct {
name string
// The cards in our hand. If len(hand) == 0, then the Game
// is over.
hand Hand
// Played melds (sets or runs).
melds []meld.Meld
// Played rummies off of other melds.
// The melds may be ones we have played, or ones that another
// player in the Game has played.
rummies []deck.Card
}
// Score returns the current score for this player, the sum of
// all played melds and rummies minus the score of cards still
// in hand.
func (p player) Score() int {
total := p.PublicScore()
for card := range p.hand {
total -= scoring.Value(card)
}
return total
}
// PublicScore returns the publicly-visible score formed by
// taking the total of all played melds and rummies.
func (p player) PublicScore() int {
total := 0
for _, m := range p.melds {
total += m.Value()
}
for _, card := range p.rummies {
total += scoring.Value(card)
}
return total
}