snengame/internal/game/deck.go

68 lines
1.1 KiB
Go
Raw Permalink 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 (
2021-07-23 17:42:12 -04:00
"fmt"
2021-07-16 14:53:45 -04:00
"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
}
2021-07-23 17:42:12 -04:00
func (d *Deck) String() string {
if d == nil {
return "||"
}
return fmt.Sprintf("|%v|", d.Cards)
}
func NewDeck(owner int) *Deck {
cards := []*Card{}
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
}
}
cards = append(cards, NewCard(0, 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() {
2021-07-16 14:53:45 -04:00
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range d.Cards {
2021-07-16 14:53:45 -04:00
j := r.Intn(i + 1)
d.Cards[i], d.Cards[j] = d.Cards[j], d.Cards[i]
2021-07-15 19:26:57 -04:00
}
}
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)
}