snengame/internal/game/deck.go

68 lines
1.1 KiB
Go

package game
import (
"fmt"
"math/rand"
"time"
"github.com/google/uuid"
)
type Deck struct {
Cards []*Card `json:"cards"`
}
func (d *Deck) String() string {
if d == nil {
return "||"
}
return fmt.Sprintf("|%v|", d.Cards)
}
func NewDeck(owner int) *Deck {
cards := []*Card{}
for i := 0; i < 3; i++ {
for j := 1; j < 9; j++ {
cards = append(cards, NewCard(j, owner, -1, uuid.Nil))
}
}
cards = append(cards, NewCard(0, owner, -1, uuid.Nil))
return &Deck{
Cards: cards,
}
}
func DeckFromCards(c []*Card) *Deck {
return &Deck{
Cards: c,
}
}
func (d *Deck) Shuffle() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range d.Cards {
j := r.Intn(i + 1)
d.Cards[i], d.Cards[j] = d.Cards[j], d.Cards[i]
}
}
func (d *Deck) Scry(s int) []*Card {
seen := []*Card{}
if len(d.Cards) < s {
seen = d.Cards
d.Cards = []*Card{}
return seen
}
seen = d.Cards[(len(d.Cards) - s):len(d.Cards)]
d.Cards = d.Cards[0 : len(d.Cards)-s]
return seen
}
func (d *Deck) Bottom(result []*Card) {
d.Cards = append(result, d.Cards...) //Should shuffle result first?
}
func (d *Deck) Size() int {
return len(d.Cards)
}