feat: 添加21点游戏

This commit is contained in:
lixiangwuxian
2024-11-01 00:46:26 +08:00
parent dce09c6e1f
commit 53da17802a
12 changed files with 999 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
package model
type Card struct {
Color string
Value []int
Name string
Visible bool
}
func NewCard(color string, name string, value []int, visible bool) *Card {
return &Card{
Color: color,
Value: value,
Name: name,
Visible: visible,
}
}
func (card *Card) Flip() *Card {
card.Visible = !card.Visible
return card
}

View File

@@ -0,0 +1,63 @@
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
}