Documentation
¶
Index ¶
- Constants
- func AdditionalDecks(num int) func(*Deck)
- func CustomSort(val bool) func(*Deck)
- func DefaultSort(val bool) func(*Deck)
- func FilterCard(face []string, suit []string) func(*Deck)
- func Joker(num int) func(*Deck)
- func Less(cards []Card) func(i, j int) bool
- func Shuffle(val bool) func(*Deck)
- func Sort(less func(cards []Card) func(i, j int) bool) func([]Card) []Card
- type Card
- type Deck
Constants ¶
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 ♥
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}
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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))
Types ¶
type Card ¶
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 (Card) 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...)