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.

eval.gno

3.52 Kb · 167 lines
  1// Package holdem implements the chain-independent rules of no-limit Texas
  2// Hold'em: hand evaluation, side pots and the betting state machine of one
  3// hand. Cards are indices 0..51 with rank = c % 13 (0 = Two .. 12 = Ace) and
  4// suit = c / 13.
  5package holdem
  6
  7// Hand categories, the high bits of a score.
  8const (
  9	HighCard = iota
 10	OnePair
 11	TwoPair
 12	Trips
 13	Straight
 14	Flush
 15	FullHouse
 16	Quads
 17	StraightFlush
 18)
 19
 20var categoryNames = []string{
 21	"high card", "pair", "two pair", "three of a kind", "straight",
 22	"flush", "full house", "four of a kind", "straight flush",
 23}
 24
 25// CategoryName names the category of a score.
 26func CategoryName(score int) string {
 27	c := Category(score)
 28	if c < 0 || c >= len(categoryNames) {
 29		return "?"
 30	}
 31	return categoryNames[c]
 32}
 33
 34// Category returns the hand category of a score.
 35func Category(score int) int {
 36	return score >> 20
 37}
 38
 39func makeScore(cat int, ranks ...int) int {
 40	s := cat << 20
 41	for i := 0; i < 5; i++ {
 42		r := 0
 43		if i < len(ranks) {
 44			r = ranks[i]
 45		}
 46		s |= r << uint(16-4*i)
 47	}
 48	return s
 49}
 50
 51// straightHigh returns the top rank of the best straight in a rank bitmask,
 52// 3 for the wheel (A-2-3-4-5), or -1.
 53func straightHigh(mask int) int {
 54	for hi := 12; hi >= 4; hi-- {
 55		m := 0x1f << uint(hi-4)
 56		if mask&m == m {
 57			return hi
 58		}
 59	}
 60	wheel := 0xf | 1<<12
 61	if mask&wheel == wheel {
 62		return 3
 63	}
 64	return -1
 65}
 66
 67// topRanks returns up to n highest ranks set in mask, skipping excluded ranks.
 68func topRanks(mask int, n int, exclude ...int) []int {
 69	out := []int{}
 70	for r := 12; r >= 0 && len(out) < n; r-- {
 71		if mask&(1<<uint(r)) == 0 {
 72			continue
 73		}
 74		skip := false
 75		for _, e := range exclude {
 76			if e == r {
 77				skip = true
 78			}
 79		}
 80		if !skip {
 81			out = append(out, r)
 82		}
 83	}
 84	return out
 85}
 86
 87// Evaluate scores the best five-card hand among 5 to 7 cards. Higher is better;
 88// equal scores tie.
 89func Evaluate(cards []int) int {
 90	if len(cards) < 5 || len(cards) > 7 {
 91		panic("evaluate needs 5 to 7 cards")
 92	}
 93	var counts [13]int
 94	var suitCount [4]int
 95	var suitMask [4]int
 96	rankMask := 0
 97	for _, c := range cards {
 98		if c < 0 || c >= 52 {
 99			panic("invalid card")
100		}
101		r, s := c%13, c/13
102		counts[r]++
103		suitCount[s]++
104		suitMask[s] |= 1 << uint(r)
105		rankMask |= 1 << uint(r)
106	}
107	flushSuit := -1
108	for s := 0; s < 4; s++ {
109		if suitCount[s] >= 5 {
110			flushSuit = s
111		}
112	}
113	if flushSuit >= 0 {
114		if hi := straightHigh(suitMask[flushSuit]); hi >= 0 {
115			return makeScore(StraightFlush, hi)
116		}
117	}
118	quad, trip, trip2 := -1, -1, -1
119	pairs := []int{}
120	for r := 12; r >= 0; r-- {
121		switch counts[r] {
122		case 4:
123			if quad < 0 {
124				quad = r
125			}
126		case 3:
127			if trip < 0 {
128				trip = r
129			} else if trip2 < 0 {
130				trip2 = r
131			}
132		case 2:
133			pairs = append(pairs, r)
134		}
135	}
136	if quad >= 0 {
137		return makeScore(Quads, append([]int{quad}, topRanks(rankMask, 1, quad)...)...)
138	}
139	if trip >= 0 {
140		pairRank := -1
141		if trip2 >= 0 {
142			pairRank = trip2
143		}
144		if len(pairs) > 0 && pairs[0] > pairRank {
145			pairRank = pairs[0]
146		}
147		if pairRank >= 0 {
148			return makeScore(FullHouse, trip, pairRank)
149		}
150	}
151	if flushSuit >= 0 {
152		return makeScore(Flush, topRanks(suitMask[flushSuit], 5)...)
153	}
154	if hi := straightHigh(rankMask); hi >= 0 {
155		return makeScore(Straight, hi)
156	}
157	if trip >= 0 {
158		return makeScore(Trips, append([]int{trip}, topRanks(rankMask, 2, trip)...)...)
159	}
160	if len(pairs) >= 2 {
161		return makeScore(TwoPair, pairs[0], pairs[1], topRanks(rankMask, 1, pairs[0], pairs[1])[0])
162	}
163	if len(pairs) == 1 {
164		return makeScore(OnePair, append([]int{pairs[0]}, topRanks(rankMask, 3, pairs[0])...)...)
165	}
166	return makeScore(HighCard, topRanks(rankMask, 5)...)
167}