-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.go
46 lines (36 loc) · 770 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
package game
import (
"math/rand"
)
type Deck struct {
planes []int
}
func NewDeck() *Deck {
deck := &Deck{}
fillDeck(deck)
return deck
}
func (d *Deck) NoPlanes() bool {
return len(d.planes) == 0
}
func (d *Deck) DrawOne() int {
return d.Draw(1)[0]
}
func (d *Deck) Draw(amount int) []int {
planes := d.planes[0:amount]
d.planes = d.planes[amount:]
return planes
}
func (d *Deck) BottomDrawOne() int {
plane := d.planes[len(d.planes)-1]
d.planes = d.planes[:len(d.planes)-1]
return plane
}
func fillDeck(deck *Deck) {
planes := make([]int, 0, 144)
shuffleCards(planes)
deck.planes = append(deck.planes, planes...)
}
func shuffleCards(planes []int) {
rand.Shuffle(len(planes), func(i, j int) { planes[i], planes[j] = planes[j], planes[i] })
}