betting.gno
6.18 Kb · 261 lines
1package holdem
2
3import "errors"
4
5// Streets.
6const (
7 Preflop = iota
8 Flop
9 Turn
10 River
11)
12
13// Player is one hand player's betting state.
14type Player struct {
15 Stack int64 // chips behind
16 StreetBet int64 // chips put in on the current street
17 Contributed int64 // chips put in during the whole hand
18 Folded bool
19 AllIn bool
20 Acted bool // acted since the bet last increased
21 LastActBet int64 // current bet right after this player's last action this street, -1 if none
22}
23
24// Betting is the no-limit betting state of one hand. Players are indexed from
25// the small blind (heads-up: index 0 is the button and small blind).
26type Betting struct {
27 Players []*Player
28 SmallBlind int64
29 BigBlind int64
30 Street int
31 CurrentBet int64 // highest street bet
32 MinRaise int64 // size of the last full bet or raise on this street
33 Turn int // index to act, -1 when the street needs no more action
34}
35
36// NewBetting posts the blinds and sets the first player to act preflop.
37func NewBetting(stacks []int64, smallBlind, bigBlind int64) *Betting {
38 n := len(stacks)
39 if n < 2 {
40 panic("need at least 2 players")
41 }
42 if smallBlind <= 0 || bigBlind < smallBlind {
43 panic("invalid blinds")
44 }
45 b := &Betting{SmallBlind: smallBlind, BigBlind: bigBlind, Street: Preflop, MinRaise: bigBlind}
46 for _, s := range stacks {
47 if s <= 0 {
48 panic("player without chips")
49 }
50 b.Players = append(b.Players, &Player{Stack: s, LastActBet: -1})
51 }
52 b.post(0, smallBlind)
53 b.post(1, bigBlind)
54 b.CurrentBet = b.Players[0].StreetBet
55 if b.Players[1].StreetBet > b.CurrentBet {
56 b.CurrentBet = b.Players[1].StreetBet
57 }
58 if b.CurrentBet < bigBlind {
59 b.CurrentBet = bigBlind
60 }
61 first := 2 % n
62 if n == 2 {
63 first = 0
64 }
65 b.Turn = b.nextToAct(first)
66 return b
67}
68
69func (b *Betting) post(i int, amount int64) {
70 p := b.Players[i]
71 if amount > p.Stack {
72 amount = p.Stack
73 }
74 p.Stack -= amount
75 p.StreetBet += amount
76 p.Contributed += amount
77 if p.Stack == 0 {
78 p.AllIn = true
79 }
80}
81
82// Live returns the number of players who have not folded.
83func (b *Betting) Live() int {
84 n := 0
85 for _, p := range b.Players {
86 if !p.Folded {
87 n++
88 }
89 }
90 return n
91}
92
93func (b *Betting) canAct(p *Player) bool {
94 return !p.Folded && !p.AllIn
95}
96
97func (b *Betting) needsAction(p *Player) bool {
98 return b.canAct(p) && (!p.Acted || p.StreetBet < b.CurrentBet)
99}
100
101// nextToAct returns the first player from index `from` (inclusive, clockwise)
102// who still has to act, or -1 when the street is complete.
103func (b *Betting) nextToAct(from int) int {
104 if b.Live() <= 1 {
105 return -1
106 }
107 actors := 0
108 for _, p := range b.Players {
109 if b.canAct(p) {
110 actors++
111 }
112 }
113 n := len(b.Players)
114 for k := 0; k < n; k++ {
115 i := (from + k) % n
116 p := b.Players[i]
117 if !b.needsAction(p) {
118 continue
119 }
120 // A lone player who can still act and has matched the bet has nobody
121 // to bet against.
122 if actors == 1 && p.StreetBet >= b.CurrentBet {
123 continue
124 }
125 return i
126 }
127 return -1
128}
129
130// StreetComplete reports whether nobody has to act on this street.
131func (b *Betting) StreetComplete() bool {
132 return b.Turn < 0
133}
134
135// Closed reports whether no further betting is possible in the hand: one live
136// player left, or the street is complete and at most one live player still
137// has chips.
138func (b *Betting) Closed() bool {
139 if b.Live() <= 1 {
140 return true
141 }
142 if b.Turn >= 0 {
143 return false
144 }
145 actors := 0
146 for _, p := range b.Players {
147 if b.canAct(p) {
148 actors++
149 }
150 }
151 return actors <= 1
152}
153
154// CanCheck reports whether player i may check.
155func (b *Betting) CanCheck(i int) bool {
156 return b.Players[i].StreetBet == b.CurrentBet
157}
158
159// CanRaise reports whether player i may bet or raise: they have chips beyond a
160// call, and either have not acted on this street or face at least a full
161// raise since they last acted (a short all-in does not reopen the betting).
162func (b *Betting) CanRaise(i int) bool {
163 p := b.Players[i]
164 if p.StreetBet+p.Stack <= b.CurrentBet {
165 return false
166 }
167 return p.LastActBet < 0 || b.CurrentBet-p.LastActBet >= b.MinRaise
168}
169
170// MinRaiseTo returns the smallest legal total street bet for a non all-in
171// bet or raise.
172func (b *Betting) MinRaiseTo() int64 {
173 return b.CurrentBet + b.MinRaise
174}
175
176// Act applies an action by player i. For "raise", amount is the player's total
177// street bet after the action; it also opens the betting when nobody has bet.
178func (b *Betting) Act(i int, action string, amount int64) error {
179 if b.Turn < 0 || i != b.Turn {
180 return errors.New("not this player's turn")
181 }
182 p := b.Players[i]
183 raised := false
184 switch action {
185 case "fold":
186 p.Folded = true
187 case "check":
188 if !b.CanCheck(i) {
189 return errors.New("cannot check facing a bet")
190 }
191 case "call":
192 if p.StreetBet >= b.CurrentBet {
193 return errors.New("nothing to call")
194 }
195 b.post(i, b.CurrentBet-p.StreetBet)
196 case "raise":
197 if amount <= b.CurrentBet {
198 return errors.New("raise must exceed the current bet")
199 }
200 max := p.StreetBet + p.Stack
201 if amount > max {
202 return errors.New("raise exceeds stack")
203 }
204 if !b.CanRaise(i) {
205 return errors.New("betting is not reopened for this player")
206 }
207 allIn := amount == max
208 if !allIn && amount < b.CurrentBet+b.MinRaise {
209 return errors.New("raise below minimum")
210 }
211 if amount-b.CurrentBet >= b.MinRaise {
212 b.MinRaise = amount - b.CurrentBet
213 }
214 b.post(i, amount-p.StreetBet)
215 b.CurrentBet = amount
216 raised = true
217 default:
218 return errors.New("unknown action")
219 }
220 p.Acted = true
221 p.LastActBet = b.CurrentBet
222 if raised {
223 for k, q := range b.Players {
224 if k != i && b.canAct(q) {
225 q.Acted = false
226 }
227 }
228 }
229 b.Turn = b.nextToAct((i + 1) % len(b.Players))
230 return nil
231}
232
233// NextStreet resets street state and sets the first player to act. It must
234// only be called when the current street is complete and betting not closed.
235func (b *Betting) NextStreet() {
236 if b.Street >= River {
237 panic("no street after the river")
238 }
239 b.Street++
240 b.CurrentBet = 0
241 b.MinRaise = b.BigBlind
242 for _, p := range b.Players {
243 p.StreetBet = 0
244 p.Acted = false
245 p.LastActBet = -1
246 }
247 first := 0
248 if len(b.Players) == 2 {
249 first = 1
250 }
251 b.Turn = b.nextToAct(first)
252}
253
254// Pot returns the total contributed by all players.
255func (b *Betting) Pot() int64 {
256 t := int64(0)
257 for _, p := range b.Players {
258 t += p.Contributed
259 }
260 return t
261}