43 lines
857 B
Go
43 lines
857 B
Go
package cards
|
|
|
|
import (
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
// Deck defines a playing card deck containing any number of cards.
|
|
type Deck struct {
|
|
Cards Cards
|
|
|
|
r *rand.Rand
|
|
}
|
|
|
|
// NewDeck initializes a deck of cards. A seed value of 0 is replaced with the
|
|
// current time in nanoseconds.
|
|
func NewDeck(c Cards, seed int64) *Deck {
|
|
if seed == 0 {
|
|
seed = time.Now().UnixNano()
|
|
}
|
|
|
|
return &Deck{Cards: c.Copy(), r: rand.New(rand.NewSource(seed))}
|
|
}
|
|
|
|
// Shuffle randomizes the deck.
|
|
func (d *Deck) Shuffle() {
|
|
for i := range d.Cards {
|
|
j := d.r.Intn(i + 1)
|
|
d.Cards[i], d.Cards[j] = d.Cards[j], d.Cards[i]
|
|
}
|
|
}
|
|
|
|
// Draw removes cards from the deck and returns them as a slice.
|
|
func (d *Deck) Draw(count int) (cards Cards, ok bool) {
|
|
if count > len(d.Cards) {
|
|
return nil, false
|
|
}
|
|
|
|
cards = d.Cards[0:count].Copy()
|
|
d.Cards = d.Cards[count:]
|
|
return cards, true
|
|
}
|