Repository navigation
Expand file tree
/
Copy pathplayer.go
More file actions
99 lines (86 loc) · 1.64 KB
/
Copy pathplayer.go
File metadata and controls
99 lines (86 loc) · 1.64 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
package dominion
import (
"fmt"
"sort"
"github.com/golang/go/src/math/rand"
)
type Player struct {
Actions int
Coin int
Buys int
Deck Cards
Hand Cards
Discard Cards
Play Cards
}
func NewPlayer(deck []*Card) *Player {
return &Player{Deck: deck}
}
func (p *Player) String() string {
var result string
result += "Deck: " + p.Deck.String() + "\n"
result += "Hand: " + p.Hand.String() + "\n"
result += "Play: " + p.Play.String() + "\n"
return result
}
func (p *Player) Shuffle() {
rand.Shuffle(len(p.Deck), func(i, j int) { p.Deck[i], p.Deck[j] = p.Deck[j], p.Deck[i] })
}
func (p *Player) Reshuffle() {
p.Deck = append(p.Deck, p.Discard...)
p.Discard = nil
p.Shuffle()
}
func (p *Player) Draw(n int) {
l := len(p.Deck)
if l < n {
if l > 0 {
p.Draw(l)
}
if len(p.Discard) > 0 {
p.Reshuffle()
p.Draw(n - l)
}
return
}
p.Hand = append(p.Hand, p.Deck[:n]...)
p.Deck = p.Deck[n:]
}
func (p *Player) Cleanup() {
p.Hand.MoveTo(p.Discard)
p.Play.MoveTo(p.Discard)
}
// Play p.Hand[card]
func (p *Player) PlayCards(cards ...int) {
sort.Ints(cards)
var hand Cards
for i, c := range p.Hand {
if len(cards) == 0 {
break
}
if i == cards[0] {
p.Play = append(p.Play, c)
cards = cards[1:]
} else {
hand = append(hand, c)
}
}
if len(cards) != 0 {
panic(fmt.Sprintf("%d is out of range of hand (len %d)", cards[0], len(p.Hand)))
}
p.Hand = hand
}
func (p *Player) PlayTreasures() {
var ts []int
for i, c := range p.Hand {
if c.Type.Is(Treasure) {
ts = append(ts, i)
}
}
p.PlayCards(ts...)
}
type Choice struct {
Make func(*Game)
Description string
}
type Decision []*Choice