-- import "github.com/adamclerk/deck"
Package deck is a library that describe the card game domain
var (
SUITS = []Suit{CLUB, DIAMOND, HEART, SPADE}
FACES = []Face{ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}
)Global Variables representing the default suits and faces in a deck of cards
func Debugf(debug bool, format string, a ...interface{}) (int, error)Debugf statement for use to debug games
func Decks(count int) func(*Options)Decks is a functional option used to create a shoe with multiple decks.
func Empty(o *Options)Empty is a functional option used to create an empty deck.
func Faces(faces ...Face) func(*Options)Faces is a functional option used to provide the faces that should be included in a created deck.
func FromSignature(sig string) func(*Options)FromSignature is a functional option used to create decks from a given hex signature
func Seed()Seed is used to seed rng. It should be called only once before creating any deck. A single invocation is good enough on each executable invocation.
func Suits(suits ...Suit) func(*Options)Suits is a functional option used to provide the suits that should be included in a created deck.
func Unshuffled(o *Options)Unshuffled is a functional option used to stop the default shuffle used when all decks are created. This is mostly used for testing.
func WithCards(cards ...Card) func(*Options)WithCards creates a deck with specific cards. Paired with he Unshuffled option and you can stack a deck for testing. With cards takes precedence over Suits and Faces Options.
type Card struct {
Face Face
Suit Suit
}Card represents a playing card with a Face and a Suit
func (c *Card) GetSignature() stringGetSignature is the hex representation of the Face and Suit of the card
func (c Card) String() stringtype CompareResult intCompareResult is the custom type returned when comparing cards
func DefaultCompare(i, j Card) CompareResultDefaultCompare is the default comparison function Currently not used in any games.
func (i CompareResult) IsEqualTo() boolIsEqualTo is a utility function to make code more readable when comparing cards. This is used in conjunction with DefaultCompare or any comparison function that returns CompareResult
func (i CompareResult) IsGreaterThan() boolIsGreaterThan is a utility function to make code more readable when comparing cards. This is used in conjunction with DefaultCompare or any comparison function that returns CompareResult
func (i CompareResult) IsLessThan() boolIsLessThan is a utility function to make code more readable when comparing cards. This is used in conjunction with DefaultCompare or any comparison function that returns CompareResult
type Deck struct {
Cards []Card
NumberOfDecks int
}Deck is a deck of cards. An array of type Card
func New(options ...func(*Options)) (*Deck, error)New creates a new deck based on Options
func (d *Deck) Deal(cards int, hands ...*Deck)Deal distributes cards to other decks/hands
func (d *Deck) GetSignature() stringGetSignature returns the signature of the deck The signature is a string in which each card is represented as a hex character. Each hex character is in the same order as the deck
func (d *Deck) NumberOfCards() intNumberOfCards is a utility function that tells you how many cards are left in the deck
func (d *Deck) Shuffle()Shuffle uses Knuth shuffle algo to randomize the deck in O(n) time sourced from https://gist.github.com/quux00/8258425
func (d *Deck) ShufflePerm()ShufflePerm uses rand.Perm instead of the many calls to rand.Intn.
When compared to the current implementation:
benchmark old ns/op new ns/op delta
BenchmarkTinyDeckShuffle-8 524 537 +2.48%
BenchmarkSmallDeckShuffle-8 1119 1070 -4.38%
BenchmarkMediumDeckShuffle-8 1611 1626 +0.93%
BenchmarkDeckShuffle-8 2115 2194 +3.74%
BenchmarkLargeDeckShuffle-8 21301 21408 +0.50%
Conclusion: Not Recommended
func (d *Deck) String() stringtype Face intFace represents the face of the card (ace, two...queen, king)
const (
ACE Face = iota
TWO
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
TEN
JACK
QUEEN
KING
)Constants for Face
type Options struct {
Shuffled bool // should the deck be shuffled
Cards []Card // default array of cards to use
Faces []Face
Suits []Suit
Decks int
Signature string
}Options is the struct used to describe now a Deck should be created
type Suit intSuit represents the suit of the card (spade, heart, diamond, club)
const (
CLUB Suit = iota
DIAMOND
HEART
SPADE
)Constants for Suit ♠♥♦♣