-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtable.go
More file actions
90 lines (77 loc) · 1.59 KB
/
table.go
File metadata and controls
90 lines (77 loc) · 1.59 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
package main
import "fmt"
type Table []*Player
func (t *Table) addPlayer(p guid) (err error) {
var newPlayer *Player = &Player{state: active, guid: p, wealth: 10000}
if len(*t) >= 10 {
err = fmt.Errorf("Table full!")
return err
}
*t = append(*t, newPlayer)
return err
}
func (t Table) AdvanceButton() {
tmp := t[len(t)-1]
for i := len(t) - 1; i > 0; i-- {
t[i] = t[i-1]
}
t[0] = tmp
}
func (t Table) makeCalledPlayersActive() {
for _, p := range t {
if p.state == called {
p.state = active
}
}
}
func (t Table) makeAllPlayersActive() {
for _, p := range t {
p.state = active
}
}
// TODO: need map[guid]player at table level to avoid
// messy n^2 lookup time
func (t Table) getPlayers(guids []guid) []*Player {
players := make([]*Player, 0)
for _, p := range t {
for _, g := range guids {
if p.guid == g {
players = append(players, p)
}
}
}
return players
}
// assignBestHands assigns to each player
// her best hand from the current deal.
func (t Table) assignBestHands(deck Deck) {
for _, p := range t {
allHands := generateAllHands(deck, p.guid)
p.bestHand = bestHand(allHands)
}
}
// bestHand returns the best Hand from a slice of Hands.
func bestHand(hands []Hand) Hand {
return findWinningHands(hands)[0]
}
func (t Table) contains(id guid) bool {
for _, player := range t {
if player.guid == id {
return true
}
}
return false
}
func (t Table) remove(id guid) {
var index int
for i, player := range t {
if player.guid == id {
index = i
}
}
if index == len(t)-1 {
t = t[:index]
} else {
t = append(t[:index], t[index+1:]...)
}
}