poker

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

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 4 Imported by: 3

README

poker-go

poker-go is a library for playing poker in Go.

Usage

package main

import (
	"fmt"
    "log"
	
    "github.com/whywaita/poker-go"
)

func main() {
	h1 := []poker.Card{
		{Rank: poker.RankAce, Suit: poker.Hearts},
		{Rank: poker.RankAce, Suit: poker.Diamonds},
	}
	p1 := poker.NewPlayer("player1", h1)

	board := []poker.Card{
		{Rank: poker.RankAce, Suit: poker.Clubs},
		{Rank: poker.RankAce, Suit: poker.Spades},
		{Rank: poker.RankKing, Suit: poker.Hearts},
		{Rank: poker.RankQueen, Suit: poker.Hearts},
		{Rank: poker.RankJack, Suit: poker.Hearts},
	}

	handtype, cards, err := poker.Evaluate(append(p1.Hand, board...))
	if err != nil {
		log.Fatalf("poker.Evaluate(append(%s. %s...)): %v", p1.Hand, board, err)
	}

	fmt.Printf("handtype: %s, cards: %s\n", handtype, cards)
}
$ go run eval_hand.go
handtype: Four of a Kind, cards: [{A hearts} {A diamonds} {A clubs} {A spades} {K hearts}]
package main

import (
    "fmt"
    "log"

    "github.com/whywaita/poker-go"
)

func main() {
	h1 := []poker.Card{
		{Rank: poker.RankDeuce, Suit: poker.Hearts},
		{Rank: poker.RankThree, Suit: poker.Diamonds},
	}
	p1 := poker.NewPlayer("player1", h1)

	h2 := []poker.Card{
		{Rank: poker.RankAce, Suit: poker.Hearts},
		{Rank: poker.RankAce, Suit: poker.Diamonds},
	}
	p2 := poker.NewPlayer("player2", h2)

	h3 := []poker.Card{
		{Rank: poker.RankSeven, Suit: poker.Clubs},
		{Rank: poker.RankEight, Suit: poker.Clubs},
	}
	p3 := poker.NewPlayer("player3", h3)

	equities, err := poker.EvaluateEquity([]poker.Player{*p1, *p2, *p3})
	if err != nil {
		log.Fatalf("failed to evaluate equity: %v", err)
	}
	fmt.Printf("%v equity: %f, %v equity: %f, %v equity %f\n", h1, equities[0], h2, equities[1], h3, equities[2])
}
$ go run calc_equities.go
[{2 hearts} {3 diamonds}] equity: 0.085326, [{A hearts} {A diamonds}] equity: 0.663249, [{7 clubs} {8 clubs}] equity 0.251425

LICENSE

MIT License

porting_made_hand.go and porting_precalculated_table.go is porting from axross/poker. Only these files are under Apache License 2.0 from the original works.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllCombinations

func AllCombinations(cards []Card, k int) [][]Card

AllCombinations returns all combinations of k elements from the given slice.

func CompareVSMadeHand

func CompareVSMadeHand(p1 Player) error

CompareVSMadeHand compares the hand of a player against all possible boards.

func Evaluate

func Evaluate(cards []Card) (HandType, []Card, error)

Evaluate returns the best hand type and cards for the given cards.

func EvaluateEquity

func EvaluateEquity(players []Player) ([]float64, error)

EvaluateEquity returns the equity of each player in the game.

func EvaluateEquityByMadeHand

func EvaluateEquityByMadeHand(players []Player) ([]float64, error)

EvaluateEquityByMadeHand returns the equity of each player in the game.

func EvaluateEquityByMadeHandWithCommunity

func EvaluateEquityByMadeHandWithCommunity(players []Player, community []Card) ([]float64, error)

func GetPairs

func GetPairs(cards []Card) [][]Card

func IsStraight

func IsStraight(cards []Card) *[]Card

Types

type Card

type Card struct {
	Rank Rank
	Suit Suit
}

func IsStraightFlush

func IsStraightFlush(cards []Card) []Card

type Deck

type Deck struct {
	Cards []Card
}

func NewDeck

func NewDeck() *Deck

func (*Deck) DrawCard

func (d *Deck) DrawCard() Card

func (*Deck) DrawCards

func (d *Deck) DrawCards(n int) []Card

func (*Deck) Shuffle

func (d *Deck) Shuffle()

type HandType

type HandType int
const (
	HandTypeRoyalFlush    HandType = 10
	HandTypeStraightFlush HandType = 9
	HandTypeFourOfAKind   HandType = 8
	HandTypeFullHouse     HandType = 7
	HandTypeFlush         HandType = 6
	HandTypeStraight      HandType = 5
	HandTypeThreeOfAKind  HandType = 4
	HandTypeTwoPair       HandType = 3
	HandTypePair          HandType = 2
	HandTypeHighCard      HandType = 1
	HandTypeUnknown       HandType = 0
)

func (HandType) String

func (h HandType) String() string

type MadeHand

type MadeHand struct {
	Value int
}

func NewBestMadeHand

func NewBestMadeHand(cards []Card) *MadeHand

func NewMadeHandFromIndex

func NewMadeHandFromIndex(value int) *MadeHand

func (*MadeHand) Power

func (hand *MadeHand) Power() int

func (*MadeHand) String

func (hand *MadeHand) String() string

func (*MadeHand) Type

func (hand *MadeHand) Type() HandType

type Player

type Player struct {
	Name  string
	Hand  []Card
	Score HandType
}

func CompareHands

func CompareHands(players []Player, board []Card) ([]Player, error)

CompareHands returns the winner(s) of the game.

func CompareHandsByMadeHand

func CompareHandsByMadeHand(players []Player, board []Card) ([]Player, error)

CompareHandsByMadeHand returns the winner(s) of the game.

func NewPlayer

func NewPlayer(name string, hand []Card) *Player

func (*Player) Evaluate

func (p *Player) Evaluate(board []Card) (HandType, []Card, error)

type Rank

type Rank int
const (
	RankUnknown Rank = iota
	RankDeuce
	RankThree
	RankFour
	RankFive
	RankSix
	RankSeven
	RankEight
	RankNine
	RankTen
	RankJack
	RankQueen
	RankKing
	RankAce
)

func UnmarshalRank

func UnmarshalRank(r any) Rank

func UnmarshalRankInt

func UnmarshalRankInt(r int) Rank

func UnmarshalRankString

func UnmarshalRankString(r string) Rank

func (Rank) String

func (r Rank) String() string

type Suit

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

func UnmarshalSuitString

func UnmarshalSuitString(s string) Suit

func (Suit) String

func (s Suit) String() string

type Winner

type Winner int
const (
	WinnerUnknown Winner = iota
	WinnerPlayer1
	WinnerPlayer2
	WinnerTie
)

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

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