Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

pots.gno

3.43 Kb · 161 lines
  1package holdem
  2
  3// MaxPlayers is the largest number of players in one hand.
  4const MaxPlayers = 6
  5
  6// Pot is one main or side pot.
  7type Pot struct {
  8	Amount   int64
  9	Eligible []int // player indices that can win this pot
 10}
 11
 12// BuildPots splits contributions into a main pot and side pots. Folded
 13// players' chips go into the pots but they are never eligible. Chips above the
 14// largest contribution of any live player form a last pot that only the live
 15// players with that contribution can win (an uncalled bet returns to its
 16// owner this way).
 17func BuildPots(contrib []int64, folded []bool) []Pot {
 18	levels := []int64{}
 19	for i, c := range contrib {
 20		if folded[i] {
 21			continue
 22		}
 23		pos := len(levels)
 24		dup := false
 25		for k, l := range levels {
 26			if l == c {
 27				dup = true
 28				break
 29			}
 30			if l > c {
 31				pos = k
 32				break
 33			}
 34		}
 35		if dup {
 36			continue
 37		}
 38		levels = append(levels, 0)
 39		copy(levels[pos+1:], levels[pos:])
 40		levels[pos] = c
 41	}
 42	if len(levels) == 0 {
 43		panic("no live players")
 44	}
 45	pots := []Pot{}
 46	prev := int64(0)
 47	for li, lvl := range levels {
 48		last := li == len(levels)-1
 49		amount := int64(0)
 50		for _, c := range contrib {
 51			hi := c
 52			if !last && hi > lvl {
 53				hi = lvl
 54			}
 55			if hi > prev {
 56				amount += hi - prev
 57			}
 58		}
 59		eligible := []int{}
 60		for i, c := range contrib {
 61			if !folded[i] && c >= lvl {
 62				eligible = append(eligible, i)
 63			}
 64		}
 65		prev = lvl
 66		if amount == 0 {
 67			continue
 68		}
 69		if n := len(pots); n > 0 && sameInts(pots[n-1].Eligible, eligible) {
 70			pots[n-1].Amount += amount
 71			continue
 72		}
 73		pots = append(pots, Pot{Amount: amount, Eligible: eligible})
 74	}
 75	return pots
 76}
 77
 78func sameInts(a, b []int) bool {
 79	if len(a) != len(b) {
 80		return false
 81	}
 82	for i := range a {
 83		if a[i] != b[i] {
 84			return false
 85		}
 86	}
 87	return true
 88}
 89
 90// Distribute awards each pot to its best eligible scores, splitting ties
 91// equally. The odd chips of a split pot all go to the winner that comes first
 92// in oddOrder (player indices clockwise from the button). It returns the
 93// amount won per player; the sum always equals the sum of the pots. It panics
 94// for more than MaxPlayers scores.
 95func Distribute(pots []Pot, scores []int, oddOrder []int) []int64 {
 96	if len(scores) > MaxPlayers {
 97		panic("too many players")
 98	}
 99	var buf [MaxPlayers]int64
100	won := buf[:len(scores):len(scores)]
101	for _, p := range pots {
102		best := -1
103		for _, i := range p.Eligible {
104			if scores[i] > best {
105				best = scores[i]
106			}
107		}
108		winners := []int{}
109		for _, i := range p.Eligible {
110			if scores[i] == best {
111				winners = append(winners, i)
112			}
113		}
114		share := p.Amount / int64(len(winners))
115		for _, i := range winners {
116			won[i] += share
117		}
118		rem := p.Amount - share*int64(len(winners))
119		if rem > 0 {
120			for _, i := range oddOrder {
121				if scores[i] == best && contains(winners, i) {
122					won[i] += rem
123					rem = 0
124					break
125				}
126			}
127			if rem > 0 {
128				panic("odd chip order misses a winner")
129			}
130		}
131	}
132	return won
133}
134
135func contains(xs []int, x int) bool {
136	for _, v := range xs {
137		if v == x {
138			return true
139		}
140	}
141	return false
142}
143
144// OddChipOrder returns player indices clockwise starting left of the button,
145// for hand players indexed from the small blind (heads-up: index 0 is the
146// button). It panics unless 0 <= n <= MaxPlayers.
147func OddChipOrder(n int) []int {
148	if n < 0 || n > MaxPlayers {
149		panic("bad player count")
150	}
151	var buf [MaxPlayers]int
152	out := buf[:n:n]
153	if n == 2 {
154		out[0], out[1] = 1, 0
155		return out
156	}
157	for i := range out {
158		out[i] = i
159	}
160	return out
161}