2024-11-01 00:46:26 +08:00

64 lines
1003 B
Go

package model
import "math/rand"
type Deck struct {
cards []*Card
}
func NewDeck() *Deck {
deck := &Deck{
cards: []*Card{},
}
return deck
}
func (deck *Deck) Cards() []*Card {
return deck.cards
}
func (deck *Deck) Clear() *Deck {
deck.cards = []*Card{}
return deck;
}
func (deck *Deck) Length() int {
return len(deck.cards)
}
func (deck *Deck) Add(card *Card) *Deck {
deck.cards = append(deck.cards, card)
return deck
}
func (deck *Deck) Shuffle() *Deck {
for i := range deck.cards {
j := i + rand.Intn(len(deck.cards) - i)
deck.cards[i], deck.cards[j] = deck.cards[j], deck.cards[i]
}
return deck
}
func (deck *Deck) Draw() *Card {
if len(deck.cards) == 0 {
return nil
}
card := deck.cards[0]
deck.cards = deck.cards[1:]
return card
}
func (deck *Deck) Contains(color string, num int) bool {
for _, card := range deck.cards {
if card.Color == color {
for _, value := range card.Value {
if value == num {
return true
}
}
}
}
return false
}