/
board.go
49 lines (45 loc) · 957 Bytes
/
board.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
47
48
49
package main
type Board struct {
cards []string
}
func (b *Board) NewCopy() Board {
db := Board{}
db.cards = make([]string, len(b.cards))
copy(db.cards, b.cards)
return db
}
func (b *Board) Init(players int) {
num := 8
if players > 2 {
num = 12
}
for i := 0; i < num; i++ {
b.cards = append(b.cards, "estate")
b.cards = append(b.cards, "dutchy")
b.cards = append(b.cards, "province")
b.cards = append(b.cards, "smithy")
b.cards = append(b.cards, "chapel")
}
for i := 0; i < 30; i++ {
b.cards = append(b.cards, "copper")
b.cards = append(b.cards, "silver")
b.cards = append(b.cards, "gold")
}
}
func (b *Board) Get(name string) (rcard string, rok bool) {
idx := -1
for i, card := range b.cards {
if card == name {
idx = i
rcard = card
break
}
}
if idx == -1 {
return rcard, false
}
l := len(b.cards) - 1
b.cards[idx], b.cards[l] = b.cards[l], b.cards[idx]
b.cards = b.cards[0:l]
return rcard, true
}