poker

package module
v0.0.0-...-d50e3f2 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 5 Imported by: 1

README

Poker Engine

Poker engine is a library written in Golang. It can be used to build:

  • A game REST API using the Go HTTP standard library.
  • An AI that learns using Reinforcement Learning.
  • A poker-odds application (CLI example).

Ensure the library is installed:

go get -u github.com/arturo-source/poker-engine/

Example

NewCard reads a string and returns the card, only if string has valid number and suit.

  • Valid numbers [A K Q J T 9 8 7 6 5 4 3 2].
  • Valid suits [s c h d].
package main

import (
    "fmt"

    "github.com/arturo-source/poker-engine"
)

func main(t *testing.T) {
 c := poker.NewCard
 cards := poker.JoinCards(c("7s"), c("Kc"), c("7c"), c("As"), c("Ad"))

 winningCards, found := poker.TwoPair(cards)
 if found {
    fmt.Println("Winning cards:", winningCards)
 } else {
    fmt.Println("Two pair was not found")
 }
}

I will add an example for a full game, when the TODO is finished.

TODO

I have to add more funcs to Game, to be able to control the game with just a game:

  • Dealing cards should shuffle the deck first.
  • Add players.
  • Add default entry coins per player.
  • A func to proceed to the next BoardState.
  • Subtract coins from the players when they bet.
  • Add coins when a player wins.
  • Calculate how much money if to each player if multiple of them win.

Documentation

Index

Constants

View Source
const (
	MAX_CARDS          = 4 * 13 // suits * cards per suit
	MAX_CARDS_IN_BOARD = 5
	MAX_BURNED_CARDS   = 3
	MAX_CARDS_PER_HAND = 2
)
View Source
const (
	CLUBS = FIRST_SUIT << (iota * 13)
	DIAMONDS
	HEARTS
	SPADES
)

Variables

View Source
var (
	TOTAL_CARDS = len(SUIT_VALUES) * len(NUMBER_VALUES)

	SUIT_VALUES = map[Cards]string{

		DIAMONDS: "d",
		CLUBS:    "c",
		HEARTS:   "h",
		SPADES:   "s",
	}

	NUMBER_VALUES = map[Cards]string{
		ACES:   "A",
		KINGS:  "K",
		QUEENS: "Q",
		JACKS:  "J",
		TENS:   "T",
		NINES:  "9",
		EIGHTS: "8",
		SEVENS: "7",
		SIXS:   "6",
		FIVES:  "5",
		FOURS:  "4",
		THREES: "3",
		TWOS:   "2",
	}
)

Functions

func BestHand

func BestHand(p *Player, tableCards Cards) (Cards, HandKind)

BestHand calculates the best combination of cards and what kind of hand it is

Types

type Board

type Board struct {
	TableCards  []Cards
	BurnedCards []Cards
	State       BoardState
	// contains filtered or unexported fields
}

Board represents a table where you can access to the current flipped cards, burned cards, and the board state (preflop, flop, etc.).

func NewBoard

func NewBoard(d *Deck) *Board

NewBoard creates a board with an specific deck, and sets the boards as the initial state.

func (*Board) NextBoardState

func (b *Board) NextBoardState() error

NextBoardState add corresponding cards to TableCards and BurnedCards, depending on the current State. Returns an error if there are no more cards in deck, or if you try to get next state in the SHOWDOWN.

func (*Board) Restart

func (b *Board) Restart()

Restart sets the board empty (cards burned, in table), sets the state to PREFLOP, and shuffles the deck, as the initial state.

type BoardState

type BoardState int
const (
	PREFLOP BoardState = iota
	FLOP
	TURN
	RIVER
	SHOWDOWN
)

type Cards

type Cards uint64

Cards represents a set of cards joined into an uint64 where each bit represent one card from the deck. From the 0 to 12 one suit, 13 to 25 other one, etc.

const (
	NO_CARD    Cards = 0
	FIRST_SUIT Cards = 0b1111111111111
	ALL_CARDS  Cards = 0b1111111111111111111111111111111111111111111111111111
)
const (
	TWOS Cards = 0b0000000000001000000000000100000000000010000000000001 << iota
	THREES
	FOURS
	FIVES
	SIXS
	SEVENS
	EIGHTS
	NINES
	TENS
	JACKS
	QUEENS
	KINGS
	ACES
)

func Flush

func Flush(cards Cards) (winningCards Cards, found bool)

Flush receives a set of cards, if it is possible to find a Flush combination, returns the best combination and true, in another case, random cards, and false.

func FourOfAKind

func FourOfAKind(cards Cards) (winningCards Cards, found bool)

FourOfAKind receives a set of cards, if it is possible to find a FourOfAKind combination, returns the best combination and true, in another case, random cards, and false.

func FullHouse

func FullHouse(cards Cards) (winningCards Cards, found bool)

FullHouse receives a set of cards, if it is possible to find a FullHouse combination, returns the best combination and true, in another case, random cards, and false.

func HighCard

func HighCard(cards Cards) (winningCards Cards, found bool)

HighCard receives a set of cards, if it is possible to find a HighCard combination, returns the best combination and true, in another case, random cards, and false.

func JoinCards

func JoinCards(cards ...Cards) Cards

JoinCards gets a set of cards and joins them into `Cards`.

func NewCard

func NewCard(cardStr string) Cards

NewCard reads the string and returns the card, only if string has valid number and suit.

Valid numbers are A K Q J T 9 8 7 6 5 4 3 2.

Valid suits are s c h d.

An example of card in string is "Ah".

func Pair

func Pair(cards Cards) (winningCards Cards, found bool)

Pair receives a set of cards, if it is possible to find a Pair combination, returns the best combination and true, in another case, random cards, and false.

func RoyalFlush

func RoyalFlush(cards Cards) (winningCards Cards, found bool)

RoyalFlush receives a set of cards, if it is possible to find a RoyalFlush combination, returns the best combination and true, in another case, random cards, and false.

func Straight

func Straight(cards Cards) (winningCards Cards, found bool)

Straight receives a set of cards, if it is possible to find a Straight combination, returns the best combination and true, in another case, random cards, and false.

func StraightFlush

func StraightFlush(cards Cards) (winningCards Cards, found bool)

StraightFlush receives a set of cards, if it is possible to find a StraightFlush combination, returns the best combination and true, in another case, random cards, and false.

func ThreeOfAKind

func ThreeOfAKind(cards Cards) (winningCards Cards, found bool)

ThreeOfAKind receives a set of cards, if it is possible to find a ThreeOfAKind combination, returns the best combination and true, in another case, random cards, and false.

func TwoPair

func TwoPair(cards Cards) (winningCards Cards, found bool)

TwoPair receives a set of cards, if it is possible to find a TwoPair combination, returns the best combination and true, in another case, random cards, and false.

func (Cards) AddCards

func (c Cards) AddCards(cardsToAdd Cards) Cards

AddCards receives cardsToAdd, and returns the original card with cardsToAdd.

func (Cards) BitToggle

func (c Cards) BitToggle(pos int) Cards

BitToggle toggles the bit in that position.

Warning!! Use only if you know how the `Cards uint64` is built.

func (Cards) CardsArePresent

func (c Cards) CardsArePresent(cards Cards) bool

CardsArePresent returns true if any of cards passed is present.

func (Cards) ClearBit

func (c Cards) ClearBit(pos int) Cards

ClearBit clears the bit in that position.

Warning!! Use only if you know how the `Cards uint64` is built.

func (Cards) Count

func (c Cards) Count() int

Count returns the number of cards in c.

func (Cards) HasBit

func (c Cards) HasBit(pos int) bool

HasBit returns true if there is a bit marked as 1 in that position.

Warning!! Use only if you know how the `Cards uint64` is built.

func (Cards) QuitCards

func (c Cards) QuitCards(cardsToQuit Cards) Cards

QuitCards receives cardsToQuit, and returns the original card without cardsToQuit.

func (Cards) SetBit

func (c Cards) SetBit(pos int) Cards

SetBit sets the bit in that position.

Warning!! Use only if you know how the `Cards uint64` is built.

func (Cards) Split

func (c Cards) Split() []Cards

Split divides the set of cards in `Cards`, and returns an array with each one separated.

func (Cards) String

func (c Cards) String() string

String transforms the set of cards in `Cards` into a readable string.

type Deck

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

Deck represents a deck with the 52 cards, you should always call NewDeck to build a deck.

func NewDeck

func NewDeck() *Deck

NewDeck fills the deck with the 52 cards, and returns the reference to this deck.

func (*Deck) GetNextCard

func (d *Deck) GetNextCard() Cards

GetNextCard returns the next card in the deck, and moves the pointer to the next card.

func (*Deck) Shuffle

func (d *Deck) Shuffle()

Shuffle resets the pointer to 0, to start using the deck again, and shuffles the cards to get in a random order.

type Game

type Game struct {
	Players []*Player
	Board   *Board
	Deck    *Deck
}

Game represents a game state which has: many players, a board, and a deck.

func NewGame

func NewGame() *Game

NewGame is an easy way to init a Game with default values.

func (*Game) DealCards

func (g *Game) DealCards() error

DealCards deals one card per each player, and deals another one for each one again.

type HandKind

type HandKind int
const (
	HIGHCARD HandKind = iota
	PAIR
	TWOPAIR
	THREEOFAKIND
	STRAIGHT
	FLUSH
	FULLHOUSE
	FOUROFAKIND
	STRAIGHTFLUSH
	ROYALFLUSH
)

func (HandKind) String

func (hk HandKind) String() string

type Player

type Player struct {
	Name       string
	Hand       Cards
	Coins      uint
	BetCoins   uint
	HasFolded  bool
	HasChecked bool
}

Player stores all the player information.

func NewPlayer

func NewPlayer(name string) *Player

NewPlayer returns a player with that name.

func (*Player) AddCard

func (p *Player) AddCard(card Cards) error

AddCard returns an error if the user has reached max cards in hand, in other case adds the card to the player hand.

type PlayerHandValue

type PlayerHandValue struct {
	Player   *Player
	BestHand Cards
	HandKind HandKind
}

PlayerHandValue is a structure to represent what is the best hand the player can have, and which kind of hand is (HIGHCARD, PAIR, etc.).

func GetWinners

func GetWinners(tableCards Cards, players []*Player) []PlayerHandValue

GetWinners returns an array with the players with the best hand (it can be one or more than one)

Jump to

Keyboard shortcuts

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