cards

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 8 Imported by: 0

README

cards

Go Report Card PkgGoDev

Go library for applications that use playing cards. Contains basic types for:

  • Suit - SPADES, HEARTS, DIAMONDS, and CLUBS.
  • Rank - 2 through Ace, with ordering functions to handle regular and pinochle decks.
  • Card - A combination of Rank and Suit. The library contains SVG images for each card.
  • RegularDeck - A collection of Cards for a regular 52-card deck.
  • PinochleDeck - A collection of Cards for a 48-card Pinochle deck.

References

Documentation

Overview

Cards is a Go library of types and functions for operating on playing cards.

Index

Constants

This section is empty.

Variables

View Source
var (
	ISWINDOWS        = DEFAULTISWINDOWS
	DEFAULTISWINDOWS = func() bool {
		return runtime.GOOS == "windows"
	}
)

Function to test whether the operating system is Windows. Declared as a variable so that it can be overridden in unit tests.

View Source
var Characters = map[Suit]string{
	SPADES:   "S",
	HEARTS:   "H",
	DIAMONDS: "D",
	CLUBS:    "C",
}
View Source
var Glyphs = map[Suit]string{
	SPADES:   string('\u2660'),
	HEARTS:   string('\u2661'),
	DIAMONDS: string('\u2662'),
	CLUBS:    string('\u2663'),
}
View Source
var PinochleRanks = []Rank{
	NINE, JACK, QUEEN, KING, TEN, ACE,
}

PinochleRanks lists the acceptable ranks in a Pinochle deck in reverse order of their precedence

View Source
var RegularRanks = []Rank{
	TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE,
}

A slice of the four suits

Functions

This section is empty.

Types

type Card

type Card struct {
	Rank Rank
	Suit Suit
}

A Card is a combination of a Rank and a Suit

func NewCard

func NewCard(rank Rank, suit Suit) Card

Creates a new Card from a rank and suit

func (Card) GetSVG

func (c Card) GetSVG() (string, error)

Returns the SVG image of this card

func (Card) String

func (c Card) String() string

Returns a representation of the card as a string

func (Card) Unicode

func (c Card) Unicode() string

Returns a representation of the card as a Unicode string

type Deck

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

Deck is the abstract base type for RegularDeck and PinochleDeck

func (*Deck) Len

func (d *Deck) Len() int

Len returns the number of cards in the deck

func (*Deck) Remove added in v1.2.0

func (d *Deck) Remove(card Card) bool

Remove removes the specified card from the deck, if it exists. Returns true if the card was removed.

func (*Deck) Shuffle

func (d *Deck) Shuffle()

Shuffle randomizes the order of cards in a Deck

func (*Deck) Sort added in v1.3.0

func (d *Deck) Sort(order []Rank)

Sort sorts the deck in place, using the specified ordering

type PinochleDeck

type PinochleDeck struct {
	Deck
}

PinochleDeck is a subtype of Deck for the Pinochle game

func NewPinochleDeck

func NewPinochleDeck() PinochleDeck

NewPinochleDeck constructs a 48-card Pinochle deck.

func (*PinochleDeck) Sort added in v1.3.0

func (pd *PinochleDeck) Sort()

Sort sorts the deck in place using the Pinochle ordering of ranks.

type Rank

type Rank int

A rank is one of the 13 levels of cards in a deck.

const (
	TWO Rank = iota + 2
	THREE
	FOUR
	FIVE
	SIX
	SEVEN
	EIGHT
	NINE
	TEN
	JACK
	QUEEN
	KING
	ACE
)

func (Rank) Offset

func (r Rank) Offset() int

Returns the offset in the Unicode system to the beginning of cards for this rank

func (Rank) String

func (r Rank) String() string

Returns a representation of the type as a string

type RegularDeck added in v1.1.1

type RegularDeck struct {
	Deck
}

RegularDeck is a subtype of the abstract type Deck

func NewRegularDeck added in v1.1.1

func NewRegularDeck() RegularDeck

NewRegularDeck constructs a 52-card regular deck.

func (*RegularDeck) Sort added in v1.3.0

func (rd *RegularDeck) Sort()

Sort sorts the deck in place using the regular ordering.

type Suit

type Suit int
const (
	SPADES Suit = iota
	HEARTS
	DIAMONDS
	CLUBS
)

func (Suit) Character

func (suit Suit) Character() string

Returns the character that represents this suit, i. e. S, H, D, C

func (Suit) Glyph

func (suit Suit) Glyph() string

Returns the Unicode glyph that represents this suit, i. e. ♠, ♡, ♢, ♣

func (Suit) Offset

func (suit Suit) Offset() int

Returns the offset in the Unicode system to the beginning of cards for this suit

func (Suit) String

func (suit Suit) String() string

Returns the suit as a string

Jump to

Keyboard shortcuts

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