-
Notifications
You must be signed in to change notification settings - Fork 24
/
deck.go
54 lines (43 loc) · 915 Bytes
/
deck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package poker
import (
"math/rand"
"time"
)
var fullDeck *Deck
func init() {
fullDeck = &Deck{initializeFullCards()}
rand.Seed(time.Now().UnixNano())
}
type Deck struct {
cards []Card
}
func NewDeck() *Deck {
deck := &Deck{}
deck.Shuffle()
return deck
}
func (deck *Deck) Shuffle() {
deck.cards = make([]Card, len(fullDeck.cards))
copy(deck.cards, fullDeck.cards)
rand.Shuffle(len(deck.cards), func(i, j int) {
deck.cards[i], deck.cards[j] = deck.cards[j], deck.cards[i]
})
}
func (deck *Deck) Draw(n int) []Card {
cards := make([]Card, n)
copy(cards, deck.cards[:n])
deck.cards = deck.cards[n:]
return cards
}
func (deck *Deck) Empty() bool {
return len(deck.cards) == 0
}
func initializeFullCards() []Card {
var cards []Card
for _, rank := range strRanks {
for suit := range charSuitToIntSuit {
cards = append(cards, NewCard(string(rank)+string(suit)))
}
}
return cards
}