cards

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: MIT Imports: 4 Imported by: 0

README

cards

A tiny Go package that models a standard 52-card deck. It provides a fast, zero-allocation representation of cards and a simple, deterministic shuffle for reproducible results.

Features

  • 52-card deck with suits and ranks encoded into a compact Card type
  • Deterministic Shuffle(seed int64) for repeatable shuffles
  • Sequential dealing with Next() and exhaustion check with IsExhausted()
  • Human-readable String() representation (e.g. A♥, T♣, Q♦, K♠)

Installation

go get github.com/Heebron/cards

If you are already inside this repository as a module, no additional steps are required.

Quick start

package main

import (
    "fmt"
    "time"

    "github.com/Heebron/cards"
)

func main() {
    d := cards.NewDeck()

    // Shuffle with a deterministic seed (use time.Now().UnixNano() for non-deterministic)
    d.Shuffle(time.Now().UnixNano())

    for {
        c, exhausted := d.Next()
        fmt.Println(c) // e.g., A♥, 2♣, ...
        if exhausted {
            break
        }
    }
}

API overview

  • Types
    • type Card uint8
    • type Deck struct
  • Constructors
    • func NewDeck() Deck
  • Methods
    • func (d *Deck) Shuffle(seed int64) — shuffle the deck using the provided seed
    • func (d *Deck) Next() (Card, bool) — deal the next card; second return value is true on last card
    • func (d *Deck) IsExhausted() bool — returns true after the last card has been returned
    • func (c Card) Suit() int — suit index (0..3) in the order Heart, Club, Diamond, Spade
    • func (c Card) Rank() int — rank index (0..12) in the order Ace..King
    • func (c Card) String() string — short string form like A♥, T♣, Q♦, K♠

Suits and ranks are exposed as integer constants:

  • Suits: Heart, Club, Diamond, Spade
  • Ranks: Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King

Deterministic shuffles

The shuffle uses a local PRNG seeded with seed. Re-using the same seed will produce the same card order — useful for tests. Call Shuffle(time.Now().UnixNano()) for non-deterministic shuffles in production.

Testing

This repository includes unit tests. Run:

go test ./...

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Overview

Package cards provides a compact, zero-allocation representation of a standard 52-card deck with deterministic and cryptographic shuffles, sequential dealing, and convenient helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Card

type Card uint8

Card is a compact value type encoding a card's suit and rank.

func (Card) Rank

func (c Card) Rank() Rank

Rank returns the card's rank as a Rank value.

func (Card) String

func (c Card) String() string

String returns the short human-readable form of the card, such as "A♥", "T♣", "Q♦", or "K♠". For out-of-range values it returns "?".

func (Card) Suit

func (c Card) Suit() Suit

Suit returns the card's suit as a Suit value.

type Deck

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

Deck represents a 52-card deck with an internal cursor used for sequential dealing via Next.

func NewDeck

func NewDeck() Deck

NewDeck returns a new ordered 52-card deck ready for use.

func NewShuffledDeck

func NewShuffledDeck(seed int64) Deck

NewShuffledDeck returns a new deck that has been shuffled using the provided seed. Using the same seed yields the same card order.

func (*Deck) Each

func (d *Deck) Each(f func(Card) bool)

Each draws cards until the deck is exhausted or f returns false.

func (*Deck) IsExhausted

func (d *Deck) IsExhausted() bool

IsExhausted reports whether all 52 cards have been dealt.

func (*Deck) Next

func (d *Deck) Next() (Card, bool)

Next deals the next card from the deck. The second return value is true if a card was returned; it becomes false only after all 52 cards have been dealt.

func (*Deck) Reset

func (d *Deck) Reset()

Reset restores the deck to the ordered state (A♥..K♠), and resets the dealing cursor to the beginning.

func (*Deck) Shuffle

func (d *Deck) Shuffle(seed int64)

Shuffle randomizes the order of cards using the provided seed. Shuffles are deterministic for a given seed. The dealing cursor is reset.

func (*Deck) ShuffleCrypto

func (d *Deck) ShuffleCrypto() error

ShuffleCrypto shuffles the deck using crypto/rand for randomness.

func (*Deck) ShuffleRandom

func (d *Deck) ShuffleRandom()

ShuffleRandom shuffles the deck using a time-based seed.

type Rank

type Rank uint8

Rank represents the face value of a card from Ace (low) through King (high).

const (
	Ace Rank = iota
	Two
	Three
	Four
	Five
	Six
	Seven
	Eight
	Nine
	Ten
	Jack
	Queen
	King
)

Rank constants enumerate the thirteen ranks from Ace through King.

type Suit

type Suit uint8

Suit represents one of the four suits in a standard deck: Heart, Club, Diamond, or Spade.

const (
	// Heart is the heart suit.
	Heart Suit = iota
	// Club is the club suit.
	Club
	// Diamond is the diamond suit.
	Diamond
	// Spade is the spade suit.
	Spade
)

Suit constants enumerate the four suits in order: Heart, Club, Diamond, Spade.

Jump to

Keyboard shortcuts

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