2021-10-01 12:43:55 -04:00
|
|
|
package tome_lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-11-19 13:53:19 -05:00
|
|
|
"log"
|
2021-10-01 12:43:55 -04:00
|
|
|
"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 (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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 13:53:19 -05:00
|
|
|
func (d *Deck) Scry(s int) *Deck {
|
|
|
|
seen := make([]*Card, s)
|
2021-10-01 12:43:55 -04:00
|
|
|
if len(d.Cards) < s {
|
2021-11-19 13:53:19 -05:00
|
|
|
copy(seen, d.Cards)
|
|
|
|
d.Reset()
|
|
|
|
} else {
|
|
|
|
scrybox := d.Cards[(len(d.Cards) - s):len(d.Cards)]
|
|
|
|
res := copy(seen, scrybox)
|
|
|
|
if res == 0 {
|
|
|
|
panic("Error copy scrybox")
|
|
|
|
}
|
|
|
|
log.Println(seen)
|
|
|
|
d.Cards = d.Cards[0 : len(d.Cards)-s]
|
2021-10-01 12:43:55 -04:00
|
|
|
}
|
2021-11-19 13:53:19 -05:00
|
|
|
return DeckFromCards(seen)
|
2021-10-01 12:43:55 -04:00
|
|
|
}
|
|
|
|
|
2021-11-19 13:53:19 -05:00
|
|
|
func (d *Deck) Bottom(bot *Deck) {
|
|
|
|
d.Cards = append(bot.Cards, d.Cards...) //Should shuffle result first?
|
2021-10-01 12:43:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deck) Size() int {
|
|
|
|
return len(d.Cards)
|
|
|
|
}
|
2021-11-19 13:53:19 -05:00
|
|
|
|
|
|
|
func (d *Deck) Reset() {
|
|
|
|
d.Cards = []*Card{}
|
|
|
|
}
|