decks

package module
v0.0.0-...-9e34f47 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2019 License: BSD-2-Clause Imports: 3 Imported by: 0

README

decks

Package decks provides a simple API for creating and customizing decks of cards in Go.

Installation

go get github.com/preslavmihaylov/decks

Quickstart

// get a default, ordered deck of 52 cards
d, err := decks.New()
if err != nil {
  // handle error
}

// Make sure you specify a random seed before shuffling the deck
rand.Seed(time.Now().UnixNano())	

// Shuffle deck of cards
d.Shuffle()

// draw cards
myHand := []decks.Card{}
myHand = append(myHand, d.Draw())
myHand = append(myHand, d.Draw())

// print hand
for _, c := range myHand {
  fmt.Println(c)
}

// discard hand
d.InsertBottom(myHand)

Try it on The Go Playground

Check out the full documentation

Contributing

Pull requests and feature requests are welcome. Feel free to submit a new issue and/or a PR.
Make sure your Pull Request is well documented and reasoned.

Documentation

Index

Constants

View Source
const (
	// DeckSize of a normal deck of cards.
	DeckSize = int(SuitsCnt) * int(RanksCnt)
)

Variables

This section is empty.

Functions

func DefaultComparator

func DefaultComparator(deck *Deck) func(i, j int) bool

DefaultComparator is the default comparator used for sorting a deck of cards.

Types

type Card

type Card struct {
	Rank
	Suit
	IsJoker bool
}

Card represents a playing card from a deck of cards. It contains a rank (e.g. Ace) and a suit (e.g. Clovers). It also contains a boolean flag denoting this card as a joker.

func (Card) String

func (c Card) String() string

type Comparator

type Comparator func(deck *Deck) func(i, j int) bool

Comparator is used to sort a deck of cards based on a user-defined comparator.

type Deck

type Deck struct {
	Cards []Card
}

Deck encapsulates a normal deck of cards and defines some operations on them.

func New

func New(opts ...Option) (*Deck, error)

New deck of cards, based on the options provided. In case none are passed, a sorted default deck of cards (without jokers) will be returned.

func (*Deck) Draw

func (d *Deck) Draw() Card

Draw a card from the deck. The drawn card is returned and removed from the deck.

func (*Deck) InsertBottom

func (d *Deck) InsertBottom(cs []Card)

InsertBottom puts the given cards at the bottom of the deck.

func (*Deck) Shuffle

func (d *Deck) Shuffle() error

Shuffle the deck randomly.

type Option

type Option func(*Deck) error

Option is a function alias used for constructing a functional-options constructor for a deck.

func Filter

func Filter(filterFunc func(fc Card) bool) Option

Filter is a functional option for filtering given cards from the deck.

func Shuffle

func Shuffle() Option

Shuffle is a functional option for shuffling the deck in a random way.

func ShuffleWithSeed

func ShuffleWithSeed(s int64) Option

ShuffleWithSeed is a functional option for shuffling the deck based on a random seed.

func Sort

func Sort(comp Comparator) Option

Sort is a functional option for sorting the deck based on a user-defined comparator.

func WithDecks

func WithDecks(cnt int) Option

WithDecks is a functional option for specifying the amount of decks to include.

func WithJokers

func WithJokers(cnt int) Option

WithJokers is a functional option for specifying how many jokers to include.

type Rank

type Rank uint8

Rank from a normal deck of cards. i.e. Ace, Two, Jack, etc...

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

The ranks from a normal deck of cards.

func (Rank) String

func (i Rank) String() string

type Suit

type Suit uint8

Suit from a normal deck of cards. i.e. Clovers, Diamonds, etc...

const (
	Clovers Suit = iota
	Diamonds
	Hearts
	Spades
	SuitsCnt
)

Suits from a normal deck of cards

func (Suit) String

func (i Suit) String() string

Jump to

Keyboard shortcuts

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