card_id/common/game/game.go

46 lines
870 B
Go

package game
import (
"git.tuxpa.in/a/card_id/common/game/card"
"lukechampine.com/frand"
)
type Game struct {
Lives int
Dealer *card.Dealer
}
func (g *Game) CreateTrial(pairs int, extra int) *Trial {
if pairs > 10 {
pairs = 10
}
cards := make([]card.Card, 0, pairs*2+extra)
ids := map[int]struct{}{}
deck := g.Dealer.GetBrand().GetDeck()
for i := 0; i < pairs; {
card := deck.Draw()
if _, ok := ids[card.Id]; !ok {
ids[card.Id] = struct{}{}
cards = append(cards, card, card)
i = i + 1
}
}
for i := 0; i < extra; {
card := deck.Draw()
if _, ok := ids[card.Id]; !ok {
ids[card.Id] = struct{}{}
cards = append(cards, card)
i = i + 1
}
}
frand.Shuffle(len(cards), func(i, j int) { cards[i], cards[j] = cards[j], cards[i] })
trial := NewTrial(g.Lives)
for k, v := range cards {
trial.Cards[k+1] = v
}
return trial
}