card_id/common/game/game.go

46 lines
870 B
Go
Raw Normal View History

2022-04-13 07:48:22 +00:00
package game
import (
2022-04-13 23:09:58 +00:00
"git.tuxpa.in/a/card_id/common/game/card"
2022-04-13 07:48:22 +00:00
"lukechampine.com/frand"
)
type Game struct {
2022-04-13 23:09:58 +00:00
Lives int
Dealer *card.Dealer
2022-04-13 07:48:22 +00:00
}
2022-04-13 23:09:58 +00:00
func (g *Game) CreateTrial(pairs int, extra int) *Trial {
if pairs > 10 {
pairs = 10
2022-04-13 07:48:22 +00:00
}
2022-04-13 23:09:58 +00:00
cards := make([]card.Card, 0, pairs*2+extra)
ids := map[int]struct{}{}
deck := g.Dealer.GetBrand().GetDeck()
2022-04-13 07:51:52 +00:00
2022-04-13 23:09:58 +00:00
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
}
2022-04-13 07:48:22 +00:00
}
2022-04-13 23:09:58 +00:00
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
2022-04-13 07:48:22 +00:00
}
}
2022-04-13 23:09:58 +00:00
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
2022-04-13 07:48:22 +00:00
}
2022-04-13 23:09:58 +00:00
return trial
2022-04-13 07:48:22 +00:00
}