cardlib

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2019 License: Apache-2.0 Imports: 4 Imported by: 1

README

cardlib

cardlib is library for playing card.

how to use

install
go get github.com/yasukotelin/cardlib
import
import "github.com/yasukotelin/cardlib"
simple sample code
package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/yasukotelin/cardlib"
)

func main() {
	// set the seed value to global random generator.
	// cardlib uses it for shuffle and cut and so on.
	rand.Seed(time.Now().UnixNano())

	// create the complete deck
	deck := cardlib.NewDeck()

	// shuffle the deck
	deck.Shuffle()

	// draw the card from Top
	card := deck.Draw()

	if card.Suit == cardlib.Joker {
		fmt.Println("I drew joker...")
	}
}

example

If you would like to see this how working, there is the example main.go code in project. You can see when Move to project root and typing go run ./cmd/cardlib/main.go.

ʕ◔ϖ◔ʔ > Make new deck!
----------------------------------------------------
♠ A ♠ 2 ♠ 3 ♠ 4 ♠ 5 ♠ 6 ♠ 7 ♠ 8 ♠ 9 ♠ 10 ♠ J ♠ Q ♠ K
♣ A ♣ 2 ♣ 3 ♣ 4 ♣ 5 ♣ 6 ♣ 7 ♣ 8 ♣ 9 ♣ 10 ♣ J ♣ Q ♣ K
♦ A ♦ 2 ♦ 3 ♦ 4 ♦ 5 ♦ 6 ♦ 7 ♦ 8 ♦ 9 ♦ 10 ♦ J ♦ Q ♦ K
♥ A ♥ 2 ♥ 3 ♥ 4 ♥ 5 ♥ 6 ♥ 7 ♥ 8 ♥ 9 ♥ 10 ♥ J ♥ Q ♥ K
🃏 Joker 🃏 Joker
----------------------------------------------------

ʕ◔ϖ◔ʔ > shuffle shuffle
----------------------------------------------------
♦ 5 ♥ K ♠ 3 ♥ 9 ♠ 5 ♠ J ♥ 8 ♥ 6 ♠ 2 ♠ 9 ♥ 3 ♦ 9 ♠ 7
♣ A ♦ J ♣ 8 ♦ K ♥ Q ♠ 8 ♦ A ♥ 10 ♣ 9 ♣ 5 ♠ Q ♠ 6 ♣ Q
♣ K ♣ J ♥ 7 ♣ 10 ♠ A ♠ 4 ♦ 7 ♥ J ♣ 4 ♥ A ♦ 3 ♣ 6 🃏 Joker
♥ 5 ♣ 3 ♦ 6 ♠ 10 ♦ Q ♣ 2 ♦ 8 ♦ 2 ♦ 4 ♠ K ♣ 7 🃏 Joker ♥ 4
♦ 10 ♥ 2
----------------------------------------------------

ʕ◔ϖ◔ʔ > Draw!!
♦ 5

ʕ◔ϖ◔ʔ > Remove joker
----------------------------------------------------
♥ K ♠ 3 ♥ 9 ♠ 5 ♠ J ♥ 8 ♥ 6 ♠ 2 ♠ 9 ♥ 3 ♦ 9 ♠ 7 ♣ A
♦ J ♣ 8 ♦ K ♥ Q ♠ 8 ♦ A ♥ 10 ♣ 9 ♣ 5 ♠ Q ♠ 6 ♣ Q ♣ K
♣ J ♥ 7 ♣ 10 ♠ A ♠ 4 ♦ 7 ♥ J ♣ 4 ♥ A ♦ 3 ♣ 6 ♥ 5 ♣ 3
♦ 6 ♠ 10 ♦ Q ♣ 2 ♦ 8 ♦ 2 ♦ 4 ♠ K ♣ 7 ♥ 4 ♦ 10 ♥ 2
----------------------------------------------------

ʕ◔ϖ◔ʔ > Draw!!
♥ K

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Card

type Card struct {
	Suit   Suit
	Number int
}

Card is struct

func (*Card) GetStrNumber

func (c *Card) GetStrNumber() string

GetStrNumber return card string number. example 13 -> "K"

func (*Card) String

func (c *Card) String() string

type Deck

type Deck struct {
	Cards []Card
}

Deck is card set struct.

func NewDeck

func NewDeck() *Deck

NewDeck returns complete new deck

func NewDeckWithSuitSet

func NewDeckWithSuitSet(suit Suit) *Deck

NewDeckWithSuitSet returns new deck maked only suit of arg

func (*Deck) Add

func (d *Deck) Add(card *Card)

Add is add the card to top of the deck

func (*Deck) AddAt

func (d *Deck) AddAt(card *Card, index int) error

AddAt is add the card to specified index of the deck

func (*Deck) BottomIndex

func (d *Deck) BottomIndex() int

BottomIndex is return bottom of the deck. The bottom is last index of slice.

func (*Deck) Cut

func (d *Deck) Cut()

Cut is cut the deck at random index This function uses global rand in math/rand packge. So please set any seed before call this function.

func (*Deck) CutAt

func (d *Deck) CutAt(index int) error

CutAt is cut the deck at specified index index is started from 0

func (*Deck) Draw

func (d *Deck) Draw() *Card

Draw top card from deck. Top card is 0 index of cards slice.

func (*Deck) DrawAt

func (d *Deck) DrawAt(index int) (*Card, error)

DrawAt is draw card at index from deck index is started from 0

func (*Deck) Exists

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

Exists is return whether exists the same card in the deck

func (*Deck) RemoveJoker

func (d *Deck) RemoveJoker()

RemoveJoker is remove all joker from deck

func (*Deck) Shuffle

func (d *Deck) Shuffle()

Shuffle the deck. The algorythm using is Fisher–Yates shuffle. This function uses global rand in math/rand packge. So please set any seed before call this function.

func (*Deck) TopIndex

func (d *Deck) TopIndex() int

TopIndex is return top of the deck. The top is 0 index of slice.

type Suit

type Suit int

Suit is card mark Spade, Club, Diamond, Heard and Joker

const (
	Spade Suit = iota
	Club
	Diamond
	Heart
	Joker
)

func (*Suit) Mark

func (s *Suit) Mark() string

Mark return Suit mark by Unicode Emoji

func (*Suit) String

func (s *Suit) String() string

Directories

Path Synopsis
cmd
cardlib command

Jump to

Keyboard shortcuts

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