...

Package joker

Overview ▾

Package joker provides playing cards.

Cards

To initialize a card:

card := joker.Card{FaceAce, SuitSpades}

To sort a set of cards:

hand := joker.Cards{
  Card{FaceAce, SuitSpades},
  Card{Face3, SuitSpades},
  Card{Face2, SuitSpades}
}
sort.Sort(hand)

Deck

To initialize a deck, call NewDeck with a seed for the random number generator used when shuffling. A seed value of zero will be replaced with the current unix time in nanoseconds.

deck := joker.NewDeck(StandardCards, 0)

Constants

Card symbols

const (
    SymbolHearts   = "♥"
    SymbolDiamonds = "♦"
    SymbolClubs    = "♣"
    SymbolSpades   = "♠"
    SymbolJoker    = "!"
)

Invalid is the text shown when a valid face or suit is not found.

const Invalid = "?"

Variables

AllFaces is a slice of all faces.

var AllFaces = []CardFace{
    FaceAce,
    Face2,
    Face3,
    Face4,
    Face5,
    Face6,
    Face7,
    Face8,
    Face9,
    Face10,
    FaceJack,
    FaceQueen,
    FaceKing,
    FaceJoker,
}

AllSuits is a slice of all suits.

var AllSuits = []CardSuit{
    SuitHearts,
    SuitDiamonds,
    SuitClubs,
    SuitSpades,
    SuitJoker,
}

StandardCards is a slice of standard cards.

var StandardCards = Cards{
    Card{FaceAce, SuitHearts},
    Card{Face2, SuitHearts},
    Card{Face3, SuitHearts},
    Card{Face4, SuitHearts},
    Card{Face5, SuitHearts},
    Card{Face6, SuitHearts},
    Card{Face7, SuitHearts},
    Card{Face8, SuitHearts},
    Card{Face9, SuitHearts},
    Card{Face10, SuitHearts},
    Card{FaceJack, SuitHearts},
    Card{FaceQueen, SuitHearts},
    Card{FaceKing, SuitHearts},
    Card{FaceAce, SuitDiamonds},
    Card{Face2, SuitDiamonds},
    Card{Face3, SuitDiamonds},
    Card{Face4, SuitDiamonds},
    Card{Face5, SuitDiamonds},
    Card{Face6, SuitDiamonds},
    Card{Face7, SuitDiamonds},
    Card{Face8, SuitDiamonds},
    Card{Face9, SuitDiamonds},
    Card{Face10, SuitDiamonds},
    Card{FaceJack, SuitDiamonds},
    Card{FaceQueen, SuitDiamonds},
    Card{FaceKing, SuitDiamonds},
    Card{FaceAce, SuitClubs},
    Card{Face2, SuitClubs},
    Card{Face3, SuitClubs},
    Card{Face4, SuitClubs},
    Card{Face5, SuitClubs},
    Card{Face6, SuitClubs},
    Card{Face7, SuitClubs},
    Card{Face8, SuitClubs},
    Card{Face9, SuitClubs},
    Card{Face10, SuitClubs},
    Card{FaceJack, SuitClubs},
    Card{FaceQueen, SuitClubs},
    Card{FaceKing, SuitClubs},
    Card{FaceAce, SuitSpades},
    Card{Face2, SuitSpades},
    Card{Face3, SuitSpades},
    Card{Face4, SuitSpades},
    Card{Face5, SuitSpades},
    Card{Face6, SuitSpades},
    Card{Face7, SuitSpades},
    Card{Face8, SuitSpades},
    Card{Face9, SuitSpades},
    Card{Face10, SuitSpades},
    Card{FaceJack, SuitSpades},
    Card{FaceQueen, SuitSpades},
    Card{FaceKing, SuitSpades},
}

StandardFaces is a slice of all faces except Jokers.

var StandardFaces = []CardFace{
    FaceAce,
    Face2,
    Face3,
    Face4,
    Face5,
    Face6,
    Face7,
    Face8,
    Face9,
    Face10,
    FaceJack,
    FaceQueen,
    FaceKing,
}

StandardSuits is a slice of all card suits except Jokers.

var StandardSuits = []CardSuit{
    SuitHearts,
    SuitDiamonds,
    SuitClubs,
    SuitSpades,
}

type Card

Card defines a playing card with a face and suit.

type Card struct {
    Face CardFace
    Suit CardSuit
}

func Parse

func Parse(identifier string) (Card, bool)

Parse parses a card identifier to construct a card.

func (Card) Equal

func (c Card) Equal(b Card) bool

Equal returns whether both cards have the same face and suit.

func (Card) Identifier

func (c Card) Identifier() string

Identifier returns a machine-readable representation of a card.

func (Card) MarshalJSON

func (c Card) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Card.

func (Card) String

func (c Card) String() string

String returns a human-readable representation of a Card.

func (*Card) UnmarshalJSON

func (c *Card) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals a Card.

func (Card) Value

func (c Card) Value() int

Value returns the numeric value of a card.

type CardFace

CardFace defines a card face.

type CardFace int

Card faces

const (
    FaceAce   CardFace = 1
    Face2     CardFace = 2
    Face3     CardFace = 3
    Face4     CardFace = 4
    Face5     CardFace = 5
    Face6     CardFace = 6
    Face7     CardFace = 7
    Face8     CardFace = 8
    Face9     CardFace = 9
    Face10    CardFace = 10
    FaceJack  CardFace = 11
    FaceQueen CardFace = 12
    FaceKing  CardFace = 13
    FaceJoker CardFace = 14
)

func (CardFace) Name

func (f CardFace) Name() string

Name returns the card face name.

func (CardFace) String

func (f CardFace) String() string

String returns the card face name.

type CardSuit

CardSuit defines a card suit.

type CardSuit int

Card suits

const (
    SuitHearts   CardSuit = 1
    SuitDiamonds CardSuit = 2
    SuitClubs    CardSuit = 3
    SuitSpades   CardSuit = 4
    SuitJoker    CardSuit = 5
)

func (CardSuit) Name

func (s CardSuit) Name() string

Name returns the card suit name.

func (CardSuit) String

func (s CardSuit) String() string

String returns the card suit name.

func (CardSuit) Symbol

func (s CardSuit) Symbol() string

Symbol returns the card suit symbol.

type Cards

Cards is a slice of Cards.

type Cards []Card

func (Cards) Contains

func (c Cards) Contains(card Card) bool

Contains returns whether the supplied cards contain the specified card.

func (Cards) Copy

func (c Cards) Copy() Cards

Copy returns a copy of the supplied cards.

func (Cards) Count

func (c Cards) Count(card Card) int

Count returns the number of occurrences of the specified card.

func (Cards) Equal

func (c Cards) Equal(cards Cards) bool

Equal returns whether the supplied cards are equal to another set of cards.

func (Cards) High

func (c Cards) High() Card

High returns the highest valued card.

func (Cards) Len

func (c Cards) Len() int

func (Cards) Less

func (c Cards) Less(i, j int) bool

func (Cards) Low

func (c Cards) Low() Card

Low returns the lowest valued card.

func (Cards) Permutations

func (c Cards) Permutations() []Cards

Permutations returns all permutations of the supplied cards.

func (Cards) Remove

func (c Cards) Remove(card Card) Cards

Remove returns the supplied cards with the specified card removed one time.

func (Cards) RemoveIndex

func (c Cards) RemoveIndex(i int) Cards

RemoveIndex returns the supplied cards excluding the card at the specified index.

func (Cards) Reversed

func (c Cards) Reversed() Cards

Reversed returns the supplied cards in reverse order.

func (Cards) Sorted

func (c Cards) Sorted() Cards

Sorted returns the supplied cards in order.

func (Cards) String

func (c Cards) String() string

func (Cards) Swap

func (c Cards) Swap(i, j int)

type Deck

Deck defines a playing card deck containing any number of cards.

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

func NewDeck

func NewDeck(c Cards, seed int64) *Deck

NewDeck initializes a deck of cards. A seed value of 0 is replaced with the current unix time in nanoseconds.

func (*Deck) Draw

func (d *Deck) Draw(count int) (cards Cards, ok bool)

Draw removes cards from the deck and returns them as a slice.

func (*Deck) Shuffle

func (d *Deck) Shuffle()

Shuffle randomizes the order of the deck using the Fisher-Yates shuffle algorithm.