snengame/internal/game/deck.go

61 lines
1.0 KiB
Go
Raw Normal View History

2021-07-22 15:37:25 -04:00
package game
2021-07-15 19:26:57 -04:00
2021-07-16 14:53:45 -04:00
import (
"math/rand"
"time"
2021-07-23 18:33:17 -04:00
"github.com/google/uuid"
2021-07-16 14:53:45 -04:00
)
2021-07-15 19:26:57 -04:00
type Deck struct {
2021-07-23 18:33:17 -04:00
Cards []*Card `json:"cards"`
2021-07-15 19:26:57 -04:00
}
func NewDeck(owner int) *Deck {
cards := []*Card{NewCard(0, owner, -1, uuid.Nil)}
2021-07-15 19:26:57 -04:00
for i := 0; i < 3; i++ {
2021-07-16 14:53:45 -04:00
for j := 1; j < 9; j++ {
cards = append(cards, NewCard(j, owner, -1, uuid.Nil))
2021-07-15 19:26:57 -04:00
}
}
return &Deck{
Cards: cards,
}
}
2021-07-23 18:33:17 -04:00
func DeckFromCards(c []*Card) *Deck {
return &Deck{
Cards: c,
}
}
2021-07-15 19:26:57 -04:00
func (d *Deck) Shuffle() {
cards := d.Cards
2021-07-16 14:53:45 -04:00
r := rand.New(rand.NewSource(time.Now().UnixNano()))
2021-07-15 19:26:57 -04:00
for i := range cards {
2021-07-16 14:53:45 -04:00
j := r.Intn(i + 1)
2021-07-15 19:26:57 -04:00
cards[i], cards[j] = cards[j], cards[i]
}
d.Cards = cards
}
2021-07-23 18:33:17 -04:00
func (d *Deck) Scry(s int) []*Card {
seen := []*Card{}
2021-07-15 19:26:57 -04:00
if len(d.Cards) < s {
seen = d.Cards
2021-07-23 18:33:17 -04:00
d.Cards = []*Card{}
2021-07-15 19:26:57 -04:00
return seen
}
seen = d.Cards[(len(d.Cards) - s):len(d.Cards)]
d.Cards = d.Cards[0 : len(d.Cards)-s]
return seen
}
2021-07-23 18:33:17 -04:00
func (d *Deck) Bottom(result []*Card) {
2021-07-15 19:26:57 -04:00
d.Cards = append(result, d.Cards...) //Should shuffle result first?
}
func (d *Deck) Size() int {
return len(d.Cards)
}