more deck util
This commit is contained in:
parent
8d4e875891
commit
26aff0ecbb
21
deck.go
21
deck.go
@ -2,7 +2,6 @@ package tome_lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
@ -23,7 +22,16 @@ func DeckFromCards(c []*Card) *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 {
|
||||
@ -33,17 +41,20 @@ func (d *Deck) Shuffle() {
|
||||
}
|
||||
|
||||
func (d *Deck) Scry(s int) *Deck {
|
||||
seen := make([]*Card, s)
|
||||
if len(d.Cards) < s {
|
||||
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")
|
||||
}
|
||||
log.Println(seen)
|
||||
d.Cards = d.Cards[0 : len(d.Cards)-s]
|
||||
}
|
||||
return DeckFromCards(seen)
|
||||
|
Loading…
Reference in New Issue
Block a user