deck

package module
v0.0.0-...-4df7676 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 8 Imported by: 2

README

Deck

GoDoc

exercice 9 from Gophercises

# Build
make compile

Documentation

Overview

Package deck contains card actions such as creating new deck of cards, sorting cards functions, ...

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Spade card type
	Spade = Suit{
			// contains filtered or unexported fields
	}
	// Diamond card type
	Diamond = Suit{
			// contains filtered or unexported fields
	}
	// Club card type
	Club = Suit{
			// contains filtered or unexported fields
	}
	// Hearth card type
	Hearth = Suit{
			// contains filtered or unexported fields
	}
	// BlackJoker card type
	BlackJoker = Suit{
				// contains filtered or unexported fields
	}
	// RedJoker card type
	RedJoker = Suit{
				// contains filtered or unexported fields
	}
)

Functions

func FilterOut

func FilterOut(f func(Card) bool) func([]Card) []Card

FilterOut card by given card

Example
cards := NewDeck(FilterOut(func(card Card) bool {
	return card.Rank != Ace
}))
for _, card := range cards {
	fmt.Println(card)
}
Output:

Ace of Spade
Ace of Diamond
Ace of Club
Ace of Hearth

func Print

func Print(cards []Card) string

Print cards in color & ASCII art

func ToASCII

func ToASCII(cards []Card) string

ToASCII renders the cards in ASCII art

Types

type Card

type Card struct {
	Suit   Suit
	Rank   Rank
	Hidden bool
}

Card from a deck

Example
fmt.Println(Card{Suit: Spade, Rank: King})
fmt.Println(Card{Suit: Diamond, Rank: Queen})
fmt.Println(Card{Suit: Hearth, Rank: Eight})
fmt.Println(Card{Suit: RedJoker})
Output:

King of Spade
Queen of Diamond
Eight of Hearth
RedJoker

func AddJokers

func AddJokers(cards []Card) []Card

AddJokers adds jokers to the deck

Example
for _, card := range AddJokers([]Card{
	Card{Suit: Hearth, Rank: Two},
}) {
	fmt.Println(card)
}
Output:

Two of Hearth
BlackJoker
RedJoker

func DefaultSort

func DefaultSort(cards []Card) []Card

DefaultSort cards

Example
for _, card := range DefaultSort([]Card{
	Card{Suit: Hearth, Rank: Two},
	Card{Suit: Spade, Rank: Queen},
	Card{Suit: Spade, Rank: Four},
	Card{Suit: Diamond, Rank: Ace},
	Card{Suit: Club, Rank: Ten},
}) {
	fmt.Println(card)
}
Output:

Four of Spade
Queen of Spade
Ace of Diamond
Ten of Club
Two of Hearth

func FromDecks

func FromDecks(decks ...[]Card) []Card

FromDecks constructs a single deck composed of multiple decks

func NewDeck

func NewDeck(opts ...func([]Card) []Card) []Card

NewDeck given an operation function to perform on them (e.g. sort, shuffle...)

func Shuffle

func Shuffle(cards []Card) []Card

Shuffle cards

Example
for _, card := range Shuffle([]Card{
	Card{Suit: Hearth, Rank: Two},
	Card{Suit: Spade, Rank: Queen},
	Card{Suit: Spade, Rank: Four},
	Card{Suit: Diamond, Rank: Ace},
	Card{Suit: Club, Rank: Ten},
}) {
	fmt.Println(card)
}
Output:

Ten of Club
Queen of Spade
Four of Spade
Two of Hearth
Ace of Diamond

func (Card) Equals

func (c Card) Equals(to Card) bool

Equals checks if the given card is the same or not

func (Card) Print

func (c Card) Print() string

Print card in ASCII with color

func (Card) String

func (c Card) String() string

func (Card) ToASCII

func (c Card) ToASCII() []string

ToASCII renders the card in ASCII art

Example
fmt.Println(strings.Join(Card{Suit: Spade, Rank: Ten}.ToASCII(), "\n"))
fmt.Println(strings.Join(Card{Suit: Diamond, Rank: Eight}.ToASCII(), "\n"))
fmt.Println(strings.Join(Card{Suit: Club, Rank: Jack}.ToASCII(), "\n"))
fmt.Println(strings.Join(Card{Suit: Hearth, Rank: Ace}.ToASCII(), "\n"))
fmt.Println(strings.Join(Card{Suit: BlackJoker}.ToASCII(), "\n"))
fmt.Println(strings.Join(Card{Suit: RedJoker}.ToASCII(), "\n"))
fmt.Println(strings.Join(Card{Suit: Spade, Rank: Five, Hidden: true}.ToASCII(), "\n"))
Output:

┌────────┐
│10 .    │
│  / \   │
│ (_,_)  │
│   I  10│
└────────┘
┌────────┐
│8  /\   │
│  /  \  │
│  \  /  │
│   \/  8│
└────────┘
┌────────┐
│J  _    │
│  ( )   │
│ (_x_)  │
│   Y   J│
└────────┘
┌────────┐
│A _  _  │
│ ( \/ ) │
│  \  /  │
│   \/  A│
└────────┘
┌────────┐
│* \||/ K│
│J /~~\ O│
│O( o o)J│
│K \ v/ *│
└────────┘
┌────────┐
│+ \||/ K│
│J /~~\ O│
│O( o o)J│
│K \ v/ +│
└────────┘
┌────────┐
│████████│
│████████│
│████████│
│████████│
└────────┘

type Rank

type Rank int

Rank of a card

const (

	// Ace of card
	Ace Rank
	// Two of card
	Two
	// Three of card
	Three
	// Four of card
	Four
	// Five of card
	Five
	// Six of card
	Six
	// Seven of card
	Seven
	// Eight of card
	Eight
	// Nine of card
	Nine
	// Ten of card
	Ten
	// Jack of card
	Jack
	// Queen of card
	Queen
	// King of card
	King
)

func (Rank) Single

func (r Rank) Single() string

Single character of the rank

func (Rank) String

func (i Rank) String() string

type Suit

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

Suit represents the type of card Setting all properties to private to make it immutable

func (Suit) ASCIITemplate

func (s Suit) ASCIITemplate() []string

ASCIITemplate of the card suit in ASCII

func (Suit) Color

func (s Suit) Color(arg interface{}) aurora.Value

Color of the suit

func (Suit) Equals

func (s Suit) Equals(to Suit) bool

Equals checks if the given suit is the same or not

func (Suit) HasRank

func (s Suit) HasRank() bool

HasRank specifies if the suit use card rank

func (Suit) String

func (s Suit) String() string

func (Suit) Value

func (s Suit) Value() int

Value of the suit

Jump to

Keyboard shortcuts

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