cards

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SuitHearts   = "Hearts"
	SuitDiamonds = "Diamonds"
	SuitClubs    = "Clubs"
	SuitSpades   = "Spades"
)

Suit constants represent the four standard suits in a deck of playing cards.

Example:

card := Card{Suit: SuitHearts, Face: FaceAce}
fmt.Println(card) // Output: A of ♥
View Source
const (
	FaceAce   = "A"
	FaceTwo   = "2"
	FaceThree = "3"
	FaceFour  = "4"
	FaceFive  = "5"
	FaceSix   = "6"
	FaceSeven = "7"
	FaceEight = "8"
	FaceNine  = "9"
	FaceTen   = "10"
	FaceJack  = "J"
	FaceQueen = "Q"
	FaceKing  = "K"
	FaceJoker = "Joker"
)

Face constants represent the possible face values of a standard playing card, including numbers, face cards, and the Joker.

Example:

// A standard face card
card := Card{Suit: SuitSpades, Face: FaceKing}

// A joker
card := Card{Suit: "", Face: FaceJoker}
View Source
const (
	RankAce = iota + 14
	RankTwo = iota + 1
	RankThree
	RankFour
	RankFive
	RankSix
	RankSeven
	RankEight
	RankNine
	RankTen
	RankJack
	RankQueen
	RankKing
	RankJoker = iota + 2
)

Rank constants represent the numeric ranking of face values, typically used for sorting or comparisons. Higher numbers correspond to stronger ranks.

Note: Ace is ranked highest by default, and Joker has its own rank value.

Example:

if faceToRank["K"] > faceToRank["Q"] {
	fmt.Println("King outranks Queen")
}

Variables

This section is empty.

Functions

func AdditionalDecks

func AdditionalDecks(num int) func(*Deck)

AdditionalDecks configures the number of extra standard decks to include. Each additional deck duplicates the standard set of cards.

Example:

// Use a single standard deck (no extra decks)
deck := New(AdditionalDecks(0))

// Use two decks combined (1 original + 1 additional)
deck := New(AdditionalDecks(1))

// Use three decks combined (1 original + 2 additional)
deck := New(AdditionalDecks(2))

func CustomSort

func CustomSort(val bool) func(*Deck)

CustomSort sets whether the deck should use a custom sorting function. This option applies a user-defined sort order instead of the default.

Example:

// Apply custom sorting
deck := New(CustomSort(true))

// Skip custom sorting (deck order determined by other options such as shuffle)
deck := New(CustomSort(false))

func DefaultSort

func DefaultSort(val bool) func(*Deck)

DefaultSort sets whether the deck should use the default comparison function for sorting. When enabled, cards are sorted based on their rank in ascending order.

Example:

// Enable default sorting
deck := New(DefaultSort(true))

// Disable default sorting (cards remain unsorted unless another option applies)
deck := New(DefaultSort(false))

func FilterCard

func FilterCard(face []string, suit []string) func(*Deck)

FilterCard removes specific cards by face value, suit, or both. If only face values are provided, all cards with those faces are excluded. If only suits are provided, all cards with those suits are excluded. If both are provided, only matching face+suit pairs are excluded.

Example:

// Remove all "2" cards (across every suit)
deck := New(FilterCard([]string{"2"}, nil))

// Remove all "Hearts"
deck := New(FilterCard(nil, []string{"Hearts"}))

// Remove only the "Ace of Spades"
deck := New(FilterCard([]string{"Ace"}, []string{"Spades"}))

// Remove no cards (pass empty slices)
deck := New(FilterCard(nil, nil))

func Joker

func Joker(num int) func(*Deck)

Joker adds the given number of jokers into the deck. Jokers are represented by the face value "Joker".

Example:

// Add 2 jokers
deck := New(Joker(2))

// Add no jokers
deck := New(Joker(0))

// Add 5 jokers for custom game rules
deck := New(Joker(5))

func Less

func Less(cards []Card) func(i, j int) bool

func Shuffle

func Shuffle(val bool) func(*Deck)

Shuffle sets whether the deck should be shuffled randomly. When enabled, the order of the cards is randomized.

Example:

// Shuffle the deck
deck := New(Shuffle(true))

// Keep cards in order (based on default/custom sort)
deck := New(Shuffle(false))

func Sort

func Sort(less func(cards []Card) func(i, j int) bool) func([]Card) []Card

Types

type Card

type Card struct {
	Suit  string
	Face  string
	Rank  int
	Value int
}

Card represents a single playing card in the deck. Each card has a suit (Hearts, Diamonds, Clubs, Spades), a face value (e.g., "Ace", "2", ..., "King"), a numeric rank used for ordering, and an optional value field for game-specific scoring.

Example:

// Standard card
card := Card{Suit: SuitHearts, Face: "Ace", Rank: 14, Value: 1}

// Joker card
card := Card{Suit: "", Face: FaceJoker, Rank: 0, Value: 0}

func New

func New(options ...func(*Deck)) []Card

func (Card) String

func (c Card) String() string

String returns a human-readable string representation of the card. For standard suits, it includes the face and the Unicode symbol of the suit. For jokers or custom cards without a suit, only the face is returned.

Example:

card := Card{Suit: SuitSpades, Face: "King", Rank: 13, Value: 10}
fmt.Println(card.String()) // Output: King of ♠

card = Card{Suit: "", Face: FaceJoker, Rank: 0, Value: 0}
fmt.Println(card.String()) // Output: Joker

type Deck

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

Deck represents configuration options for generating a new deck of cards. It is not used directly; instead, it is configured through option functions (such as Shuffle, Joker, AdditionalDecks, etc.) and passed into New().

Fields inside Deck are set internally by these option functions and should not be modified directly.

Example:

// Create a basic deck
deck := New()

// Create a shuffled deck with 2 jokers
deck := New(Shuffle(true), Joker(2))

// Create a deck with no Hearts and no Aces
deck := New(FilterCard([]string{"Ace"}, []string{"Hearts"}))

// Create 2 combined decks, sorted in default order
deck := New(AdditionalDecks(1), DefaultSort(true))

Typical usage:

options := []func(*Deck){
	Shuffle(true),
	Joker(3),
	FilterCard([]string{"2"}, nil), // remove all 2s
}
deck := New(options...)

Jump to

Keyboard shortcuts

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