tome_lib/deck.go

74 lines
1.2 KiB
Go

package tome_lib
import (
"fmt"
"math/rand"
"time"
)
type Deck struct {
Cards []*Card `json:"cards"`
}
func (d *Deck) String() string {
if d == nil {
return "||"
}
return fmt.Sprintf("|%v|", d.Cards)
}
func DeckFromCards(c []*Card) *Deck {
return &Deck{
Cards: c,
}
}
func DeckFromCard(c *Card) *Deck {
if c == nil {
return &Deck{
Cards: []*Card{},
}
}
return &Deck{
Cards: []*Card{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) *Deck {
var seen []*Card
if len(d.Cards) == 0 {
return DeckFromCard(nil)
} else if len(d.Cards) < s {
seen := make([]*Card, len(d.Cards))
copy(seen, d.Cards)
d.Reset()
} else {
seen = make([]*Card, s)
scrybox := d.Cards[(len(d.Cards) - s):len(d.Cards)]
res := copy(seen, scrybox)
if res == 0 {
panic("Error copy scrybox")
}
d.Cards = d.Cards[0 : len(d.Cards)-s]
}
return DeckFromCards(seen)
}
func (d *Deck) Bottom(bot *Deck) {
d.Cards = append(bot.Cards, d.Cards...) //Should shuffle result first?
}
func (d *Deck) Size() int {
return len(d.Cards)
}
func (d *Deck) Reset() {
d.Cards = []*Card{}
}