pokerlib

package module
v0.0.41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2021 License: MIT Imports: 15 Imported by: 0

README

pokerlib

A library of poker primitives in go

Features:

  • Foundation data types for Card, Hand and Deck
  • Hand ranking. Calculates best 5 card hand out of any set of cards.
  • Hand rank encoding
  • English language hand descriptions Ex: Full house, Kings full of 8's, An Ace high flush, etc.]]
  • Hand evaluation logging
  • Odds calculations

Rank representation:

Each 5 card hand has a unique 32-bit representation (called the Hand Rank). The hand is encoded such that the ordering of the integer interpretations of the 32 bits is the same as the hand ordering. In other words a higher ranked hand is gauranteed to have a higher integer value than a lower ranked hand....

The 32 bits contains info about the type of hand (Flush, straight, etc), as well as the index of each of the 5 cards.

32 bit encoding:

  bits 27-20 contains the hand kind 
  bits 19-16 card
  bits 15-12 card
  bits 11-8  card
  bits 7-4   card
  bits 3-0   card

Each card in the hand is represented as a 4 bit value. The least signaficant 20 bits of the rank represent the 5 cards in order of hand evaluation signficance. I.e. for a pair, the 2 cards in the pair are the left most 2 cards, no matter if they are Kings or 2's...

Examples:   

            hand:   A♥ A♣ K♦ K♣ Q♥   (2 Pair, Ace's and King's)

            (2 pair)     (Ace)  (Ace)  (King)  (King) (Queen)
    rank:   00000011     1110   1110   1101    1101   1100       = 4124124


            hand:   2♥ 2♣ 2♦ 7♣ 7♥   (2 Pair, Ace's and King's)

            (Full House) (Two)  (Two)  (Two)  (Seven) (Seven)
    rank:   00000111     0010   0010   0010   0111    0111      = 7479927

Hand odds

Win and Tie probabilities can be calculated given a set of hand hole cards, and additionally 0-5 common cards and a deck.

The Monte Carlo method is used.

Documentation

Overview

all rights reserved

: Rick Badertscher (rick.badertscher@gmail.com)

Index

Constants

View Source
const CARD_MASK = 0x0000000F
View Source
const HAND_KIND_MASK = 0xFFF00000
View Source
const HAND_KIND_SHIFT = 20

Variables

This section is empty.

Functions

func CalculateOdds

func CalculateOdds(deck *Deck, hands Hands, commonCards []Card, depth int) map[int]*Odds

*

Computes win & ties odds given a set of hand cards and a set of common cards

@samples is the number of sample hands evaulated to compute the odds estimate

func DoRank

func DoRank(cards []Card, checkedSets map[HandRank][]Card) ([]Card, HandRank)

cards is in sorted order low to high

func PrintHand

func PrintHand(cards []Card) string

func Rank

func Rank(cards []Card) ([]Card, HandRank)

*

Takes a set of cards of any size and returns the highest ranked 5 card hand

func SortCards

func SortCards(cards []Card)

func SortCardsByIndex

func SortCardsByIndex(cards []Card)

Types

type ByCardIndex

type ByCardIndex []Card

func (ByCardIndex) Len

func (a ByCardIndex) Len() int

func (ByCardIndex) Less

func (a ByCardIndex) Less(i, j int) bool

func (ByCardIndex) Swap

func (a ByCardIndex) Swap(i, j int)

type ByCount

type ByCount []Histogram

func (ByCount) Len

func (a ByCount) Len() int

func (ByCount) Less

func (a ByCount) Less(i, j int) bool

func (ByCount) Swap

func (a ByCount) Swap(i, j int)

type ByValue

type ByValue []Card

func (ByValue) Len

func (a ByValue) Len() int

func (ByValue) Less

func (a ByValue) Less(i, j int) bool

func (ByValue) Swap

func (a ByValue) Swap(i, j int)

type Card

type Card struct {
	Index Index `json:"index"`
	Suit  Suit  `json:"suit"`
}

func CardAbsoluteValueToCard added in v0.0.40

func CardAbsoluteValueToCard(v CardAbsoluteValue) Card

func (Card) Equals

func (this Card) Equals(c Card) bool

func (Card) GetAbsoluteValue added in v0.0.40

func (this Card) GetAbsoluteValue() CardAbsoluteValue

func (Card) GetCardCode added in v0.0.40

func (this Card) GetCardCode() CardCode

func (Card) GetIndexValue

func (this Card) GetIndexValue() CardCode

func (Card) String

func (this Card) String() string

type CardAbsoluteValue added in v0.0.40

type CardAbsoluteValue uint16

func CardToCardAbsoluteValue added in v0.0.40

func CardToCardAbsoluteValue(c Card) CardAbsoluteValue

type CardCode added in v0.0.40

type CardCode int

func (CardCode) String added in v0.0.40

func (this CardCode) String() string

type CardSet

type CardSet []Card

CardSet is a consistantly sorted set of cards

func NewCardSet

func NewCardSet(cards []Card) CardSet

func (CardSet) ContainsCard

func (this CardSet) ContainsCard(card Card) bool

func (CardSet) Equals

func (this CardSet) Equals(h CardSet) bool

func (CardSet) Hash

func (this CardSet) Hash() uint

type Deck

type Deck struct {
	// contains filtered or unexported fields
}

func NewDeck

func NewDeck() *Deck

func ReadDeck

func ReadDeck(filename string) *Deck

func (*Deck) BorrowRandom

func (this *Deck) BorrowRandom() Card

func (*Deck) BurnCard

func (this *Deck) BurnCard()

func (*Deck) Copy

func (this *Deck) Copy() *Deck

func (*Deck) DrawCard

func (this *Deck) DrawCard() Card

func (*Deck) ReturnCard

func (this *Deck) ReturnCard(card Card) error

func (*Deck) Shuffle

func (this *Deck) Shuffle()

func (Deck) String

func (this Deck) String() string

type HandKind

type HandKind int8 // Pair, Straight, etc.
const (
	HighCard      HandKind = 0b0000
	Pair          HandKind = 0b0001
	TwoPair       HandKind = 0b0010
	ThreeOfAKind  HandKind = 0b0011
	Straight      HandKind = 0b0100
	Flush         HandKind = 0b0101
	FullHouse     HandKind = 0b0111
	FourOfAKind   HandKind = 0b1000
	StraightFlush HandKind = 0b1001
)

func GetHandKind

func GetHandKind(rank HandRank) HandKind

func (HandKind) String

func (this HandKind) String() string

type HandRank

type HandRank uint

func RankHand

func RankHand(cards CardSet) HandRank

Called only for 5 cards, cards are in sorted order low to high

func (HandRank) Describe

func (this HandRank) Describe() string

func (HandRank) DescribeBasic

func (this HandRank) DescribeBasic() string

func (HandRank) DescribeWithColor

func (this HandRank) DescribeWithColor() string

func (HandRank) GetCard

func (this HandRank) GetCard(pos int) Index

type Hands

type Hands map[int][2]Card

type Histogram

type Histogram struct {
	Count int
	Index Index
}

type Index

type Index int
const (
	Two Index = iota + 2
	Three
	Four
	Five
	Six
	Seven
	Eight
	Nine
	Ten
	Jack
	Queen
	King
	Ace
)

func (Index) String

func (this Index) String() string

type Odds

type Odds struct {
	Wins float32 `json:"wins"`
	Ties float32 `json:"ties"`
}

type Suit

type Suit int
const (
	Spades Suit = iota + 1
	Hearts
	Diamonds
	Clubs
)

func (Suit) String

func (this Suit) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL