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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -23,7 +22,16 @@ func DeckFromCards(c []*Card) *Deck {
|
|||||||
Cards: c,
|
Cards: c,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func DeckFromCard(c *Card) *Deck {
|
||||||
|
if c == nil {
|
||||||
|
return &Deck{
|
||||||
|
Cards: []*Card{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &Deck{
|
||||||
|
Cards: []*Card{c},
|
||||||
|
}
|
||||||
|
}
|
||||||
func (d *Deck) Shuffle() {
|
func (d *Deck) Shuffle() {
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
for i := range d.Cards {
|
for i := range d.Cards {
|
||||||
@ -33,17 +41,20 @@ func (d *Deck) Shuffle() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Deck) Scry(s int) *Deck {
|
func (d *Deck) Scry(s int) *Deck {
|
||||||
seen := make([]*Card, s)
|
var seen []*Card
|
||||||
if len(d.Cards) < s {
|
if len(d.Cards) == 0 {
|
||||||
|
return DeckFromCard(nil)
|
||||||
|
} else if len(d.Cards) < s {
|
||||||
|
seen := make([]*Card, len(d.Cards))
|
||||||
copy(seen, d.Cards)
|
copy(seen, d.Cards)
|
||||||
d.Reset()
|
d.Reset()
|
||||||
} else {
|
} else {
|
||||||
|
seen = make([]*Card, s)
|
||||||
scrybox := d.Cards[(len(d.Cards) - s):len(d.Cards)]
|
scrybox := d.Cards[(len(d.Cards) - s):len(d.Cards)]
|
||||||
res := copy(seen, scrybox)
|
res := copy(seen, scrybox)
|
||||||
if res == 0 {
|
if res == 0 {
|
||||||
panic("Error copy scrybox")
|
panic("Error copy scrybox")
|
||||||
}
|
}
|
||||||
log.Println(seen)
|
|
||||||
d.Cards = d.Cards[0 : len(d.Cards)-s]
|
d.Cards = d.Cards[0 : len(d.Cards)-s]
|
||||||
}
|
}
|
||||||
return DeckFromCards(seen)
|
return DeckFromCards(seen)
|
||||||
|
Loading…
Reference in New Issue
Block a user