impl cards

This commit is contained in:
Bel LaPointe
2021-03-30 20:29:49 -05:00
parent 9efb30ae3e
commit a26c1a0043
3 changed files with 66 additions and 21 deletions

View File

@@ -5,8 +5,10 @@ import (
)
type Hand struct {
Public []Card
Private []Card
Public []Card
Private []Card
ReversePublic []Card
ReversePrivate []Card
}
func (hand *Hand) Push(card Card) {
@@ -17,12 +19,12 @@ func (hand *Hand) Push(card Card) {
}
func (hand Hand) Len() int {
return len(hand.Public) + len(hand.Private)
return len(hand.Public) + len(hand.Private) + len(hand.ReversePublic) + len(hand.ReversePrivate)
}
func (hand Hand) AllCards() []Card {
allcards := make([]Card, 0, len(hand.Public)+len(hand.Private))
for _, cards := range [][]Card{hand.Public, hand.Private} {
allcards := make([]Card, 0, hand.Len())
for _, cards := range [][]Card{hand.Public, hand.Private, hand.ReversePublic, hand.ReversePrivate} {
allcards = append(allcards, cards...)
}
return allcards

View File

@@ -1,17 +1,23 @@
package operation
import "local/sandbox/cards/src/entity"
import (
"local/sandbox/cards/src/entity"
)
type Cards struct {
AceLow bool
Jokers int
WithoutValue []Value
WithoutSuit []Suit
}
func (cards Cards) NewDeck() entity.Deck {
deck := new54DeckAceHigh()
deck := new52DeckAceHigh()
if cards.AceLow {
deck = new54DeckAceLow()
deck = new52DeckAceLow()
}
for i := 0; i < cards.Jokers; i++ {
deck.Deck = append(deck.Deck, entity.Card{Value: int(Joker)})
}
for i := len(deck.Deck) - 1; i >= 0; i-- {
card := deck.Deck[i]
@@ -26,11 +32,12 @@ func (cards Cards) NewDeck() entity.Deck {
deck.Deck = append(deck.Deck[:i], deck.Deck[i+1:]...)
}
}
deck.Shuffle()
return deck
}
func new54DeckAceHigh() entity.Deck {
cards := make([]entity.Card, 0, 54)
func new52DeckAceHigh() entity.Deck {
cards := make([]entity.Card, 0, 52)
for _, suit := range suitStrings {
for _, value := range valueStrings {
if value != AceLow && value != Joker {
@@ -38,15 +45,13 @@ func new54DeckAceHigh() entity.Deck {
}
}
}
cards = append(cards, entity.Card{Value: int(Joker)})
cards = append(cards, entity.Card{Value: int(Joker)})
deck := entity.Deck{Deck: cards}
deck.Shuffle()
return deck
}
func new54DeckAceLow() entity.Deck {
deck := new54DeckAceHigh()
func new52DeckAceLow() entity.Deck {
deck := new52DeckAceHigh()
for i, card := range deck.Deck {
if card.Value == int(AceHigh) {
deck.Deck[i].Value = int(AceLow)

View File

@@ -1,10 +1,13 @@
package operation
import "testing"
import (
"local/sandbox/cards/src/entity"
"testing"
)
func TestNewDeck54(t *testing.T) {
deck := new54DeckAceLow()
if len(deck.Deck) != 54 {
func TestNewDeck52(t *testing.T) {
deck := new52DeckAceLow()
if len(deck.Deck) != 52 {
t.Fatal(len(deck.Deck))
}
for _, card := range deck.Deck {
@@ -17,13 +20,48 @@ func TestNewDeck54(t *testing.T) {
func TestCardsNewDeck(t *testing.T) {
cases := map[string]struct {
cards Cards
check func(Cards) bool
}{}
check func(entity.Deck) bool
}{
"default": {
cards: Cards{},
check: func(deck entity.Deck) bool {
return len(deck.Deck) == 52
},
},
"two jokers": {
cards: Cards{
Jokers: 2,
},
check: func(deck entity.Deck) bool {
return len(deck.Deck) == 54
},
},
"no fours": {
cards: Cards{
WithoutValue: []Value{Four},
},
check: func(deck entity.Deck) bool {
return len(deck.Deck) == 48
},
},
"no hearts, no clubs, one joker": {
cards: Cards{
Jokers: 1,
WithoutSuit: []Suit{Heart, Club},
},
check: func(deck entity.Deck) bool {
return len(deck.Deck) == 27
},
},
}
t.Fatal("not impl")
for name, d := range cases {
c := d
t.Run(name, func(t *testing.T) {
deck := c.cards.NewDeck()
if !c.check(deck) {
t.Fatal(deck)
}
})
}
}