eval

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: BSD-2-Clause Imports: 10 Imported by: 1

README

Benchmarks

Riverboat is faster than

  • chehsunliu/poker
    • by 1.7x for 5-card evaluation
    • by 1.2x for 6-card evaluation*
    • by 1.4x for 7-card evaluation*
  • notnil/joker
    • by 325x for 5-card evaluation
    • by 150x for 6-card evaluation*
    • by 74x for 7-card evaluation*

*Riverboat and notnil/joker's 6- and 7-card hand evaluation are not directly comparable to chehsunliu/poker's, as Riverboat and Joker return both the best 5 cards, as well as their absolute ranking, whereas Poker only provides the absolute ranking.

All benchmarks were measured over the same set of cards, and the timing did not include the conversion of cards from human-readable string form to native representation.

This benchmark was run on a 2018 Macbook Pro with an Intel i9-8950HK 2.9GHz processor. The precise benchmarks run can be found in benchmarks_test.go

$ go test -bench=. -benchmem -benchtime 5s
goos: darwin
goarch: amd64
pkg: github.com/alexclewontin/riverboat/eval
BenchmarkFiveJoker-12             190854             30169 ns/op           14430 B/op        657 allocs/op
BenchmarkFivePoker-12           36639379               156 ns/op               0 B/op          0 allocs/op
BenchmarkFiveRiverboat-12       59392126                92.6 ns/op             0 B/op          0 allocs/op
BenchmarkSixJoker-12               38364            158847 ns/op           67960 B/op       2923 allocs/op
BenchmarkSixPoker-12             4252222              1298 ns/op             288 B/op          9 allocs/op
BenchmarkSixRiverboat-12         5695171              1055 ns/op             288 B/op          9 allocs/op
BenchmarkSevenJoker-12              9788            564280 ns/op          265363 B/op      10231 allocs/op
BenchmarkSevenPoker-12            555128             10397 ns/op            2304 B/op         72 allocs/op
BenchmarkSevenRiverboat-12        746978              7663 ns/op            2016 B/op         63 allocs/op
PASS
ok      github.com/alexclewontin/riverboat/eval 56.545s
                                                              

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBadCard = errors.New("invalid card string")

ErrBadCard is the error returned by ParseCardBytes if the passed []byte does not match the correct format

Functions

func HandValue

func HandValue(c0, c1, c2, c3, c4 Card) int

HandValue takes five cards, and returns an integer [1, 7462] representing their rank among all possible 5-card poker hands. Lower is better (e.g. a royal flush is 1, a 7-5-4-3-2 offsuit is 7462).

WARNING: Passing an ill-formed combination of 5 cards will result in undefined behavior, and may cause anything from a segmentation fault to completely silent failure. Ill-formed combinations include nonsensical cards, or hands that include duplicate cards. HandValue most often recieves values from internal, not external sources so it intentionally does not perform checks on the values it is passed to optimize performance. If you intend to pass HandValue data directly from an external source it is your responsibility to ensure the integrity of that data.

Types

type Card

type Card int32

Card is the type representing a single laying card. It is 32 bits long, packed according to the following schematic:

+--------+--------+--------+--------+
|xxxbbbbb|bbbbbbbb|cdhsrrrr|xxpppppp|
+--------+--------+--------+--------+

p 	= prime number of rank (deuce=2,trey=3,four=5,...,ace=41)
r 	= rank of card (deuce=0,trey=1,four=2,five=3,...,ace=12)
cdhs	= suit of card (bit turned on based on suit of card)
b 	= bit turned on depending on rank of card

func BestFiveOfSeven

func BestFiveOfSeven(c0, c1, c2, c3, c4, c5, c6 Card) ([]Card, int)

BestFiveOfSeven uses HandValue as an oracle to find the optimal combination of 5 cards from the 7 passed in. BestFiveOfSeven returns a slice of the 5 cards which make up the best hand, and the score associated with that hand (lower is better).

WARNING: See the warning associated with HandValue.

func BestFiveOfSix

func BestFiveOfSix(c0, c1, c2, c3, c4, c5 Card) ([]Card, int)

BestFiveOfSix uses HandValue as an oracle to find the optimal combination of 5 cards from the 6 passed in. BestFiveOfSix returns a slice of the 5 cards which make up the best hand, and the score associated with that hand (lower is better).

WARNING: See the warning associated with HandValue.

func MustParseCardBytes

func MustParseCardBytes(b []byte) Card

MustParseCardBytes is the same as ParseCardBytes, except it panics if b is not in L

func MustParseCardString

func MustParseCardString(s string) Card

MustParseCardString is the same as MustParseCardBytes, except it takes a string as an arg

func ParseCardBytes

func ParseCardBytes(b []byte) (Card, error)

ParseCardBytes is a helper function that will convert a human-readable representation of a card to the equivalent 32 bit layout. It accepts strings composed of a rank concatenated with a suit. The rank may be 2-10,T,J,Q,K, or A, and the suit may be C, D, H, or S. It is not case sensative, and 10 and T are equivalent. It discards whitespace before and after. More precisely, it accepts the language L defined by:

^\s*(10|[2-9]|[TJQKAtjqka])([CDHScdhs])\s*$

Should there be a conflict between the plain english and the regular expression, the regular expression takes precedence. If b is not in L, ParseCardBytes returns an error value. Otherwise, it returns a Card in native format and nil

func (Card) Scan

func (c Card) Scan(state fmt.ScanState, verb rune) error

Scan wraps ParseCardBytes to satisfy the fmt.Scanner interface If ParseCardBytes returns an error, c is guaranteed not to change

func (Card) String

func (c Card) String() string

CardToString outputs a human-readable representation of a Card. Unlike Scan, this may be useful for creating serialized GameViews for the end user. The returned string will be in all uppercase, and the rank 10 will be represented using the letter T. Calling this method on an poorly formed card will result in undefined behavior.

type Deck

type Deck []Card

Deck is the basic type representing a deck of playing cards

var DefaultDeck Deck

DefaultDeck contains all 52 cards. It may be ordered, but that is not guaranteed in the future

func (*Deck) IsEmpty

func (d *Deck) IsEmpty() bool

IsEmpty returns true if d has length 0, and false otherwise

func (Deck) Marshal

func (d Deck) Marshal() (interface{}, error)

Marshal returns an empty interface, the underlying type of which is a string, or []byte. The string is a binary representation of the deck, and the characters within are not guaranteed to be printable. Marshal returns an error if the internal write operation fails.

func (*Deck) Pop

func (d *Deck) Pop() Card

Pop removes and returns the top card. Return 0 if stack is empty.

func (*Deck) Push

func (d *Deck) Push(card Card)

Push places card onto the top of the deck. Useful for constructing known orderings, if you want.

func (*Deck) Shuffle

func (d *Deck) Shuffle()

Shuffle resets the contents of d and performs a Fisher-Yates shuffle. Post-condition: d contains all 52 unique cards, in a normally distributed random order.

func (*Deck) Unmarshal

func (d *Deck) Unmarshal(v interface{}) error

Unmarshal populates a deck from a binary string previously generated by Marshal. If the empty interface v does not have a []byte as its underlying type, d will be set to nil.

Jump to

Keyboard shortcuts

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