Back to godoc.org
git.sr.ht/~tslocum/joker

Package joker

v0.1.2-0...-b012663
Latest Go to latest

The latest major version is .

Published: Jan 16, 2020 | License: MIT | Module: git.sr.ht/~tslocum/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)

Index

Constants

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

Card symbols

const Invalid = "?"

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

Variables

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

AllFaces is a slice of all faces.

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

AllSuits is a slice of all suits.

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},
}

StandardCards is a slice of standard cards.

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

StandardFaces is a slice of all faces except Jokers.

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

StandardSuits is a slice of all card suits except Jokers.

type Card

type Card struct {
	Face CardFace
	Suit CardSuit
}

Card defines a playing card with a face and suit.

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

type CardFace int

CardFace defines a card face.

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
)

Card faces

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

type CardSuit int

CardSuit defines a card suit.

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

Card suits

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

type Cards []Card

Cards is a slice of Cards.

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(i int) Cards

Remove 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) Swap

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

type Deck

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

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

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 deck.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier