-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpokerlib.go
107 lines (95 loc) · 2.42 KB
/
pokerlib.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Created with IntelliJ IDEA.
* User: hcombs
* Date: 8/3/13
* Time: 10:40 AM
* To change this template use File | Settings | File Templates.
*/
package poker
import "fmt"
//
// This routine initializes the deck. A deck of cards is
// simply an integer array of length 52 (no jokers). This
// array is populated with each card, using the following
// scheme:
//
// An integer is made up of four bytes. The high-order
// bytes are used to hold the rank bit pattern, whereas
// the low-order bytes hold the suit/rank/prime value
// of the card.
//
// +--------+--------+--------+--------+
// |xxxbbbbb|bbbbbbbb|cdhsrrrr|xxpppppp|
// +--------+--------+--------+--------+
//
// p = prime number of rank (deuce=2,trey=3,four=5,five=7,...,ace=41)
// r = rank of card (deuce=0,trey=1,four=2,five=3,...,ace=12)
// cdhs = suit of card
// b = bit turned on depending on rank of card
//
func init_deck(deck *Deck) {
suit := CLUB // (0x8000)
n := 0 //index into the deck array
for i := 0; i < NUM_SUITS; i++ {
for j := uint(0); j < uint(NUM_CARDS); j++ {
deck[n] = NewCard(CardRank(j), Suit(suit))
n++
}
suit >>= 1 //go to the 'next' suit
}
}
func NewCard(rank CardRank, suit Suit) Card {
return Card(
primes[rank] |
uint(rank)<<8 |
uint(suit) |
(1 << (16 + rank)))
}
//
// Creates a new Deck initialized to the value of
func NewDeck() *Deck {
var d Deck
init_deck(&d)
return &d
}
// Return the position within the deck for a given rank & suit,
// or (-1, false) if not found.
func (d Deck) find(c CardRank, s Suit) (pos int, found bool) {
for i, card := range d {
if ((card & RANK_MASK) != 0) && ((int(card) & SUIT_MASK) == int(s)) {
return i, true
}
}
return -1, false
}
// return the corresponding HandRank for a given value
func HandRankFromScore(val int) HandRank {
switch {
case val > 6185:
return HIGH_CARD
case val > 3325:
return ONE_PAIR
case val > 2467:
return TWO_PAIR
case val > 1609:
return THREE_OF_A_KIND
case val > 1599:
return STRAIGHT
case val > 322:
return FLUSH
case val > 166:
return FULL_HOUSE
case val > 10:
return FOUR_OF_A_KIND
}
return STRAIGHT_FLUSH
}
// String value for a Hand
func (h Hand) String() string {
return fmt.Sprintf("%s %s %s %s %s", h[0], h[1], h[2], h[3], h[4])
}
// String value for a Card
func (c Card) String() string {
rank := (c >> 8) & 0xF
return fmt.Sprintf("%s%s", cardRankString[rank], Suit(c&SUIT_MASK))
}