diff --git a/src/entity/hand.go b/src/entity/hand.go index c59e759..a54adad 100644 --- a/src/entity/hand.go +++ b/src/entity/hand.go @@ -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 diff --git a/src/game/rule/operation/cards.go b/src/game/rule/operation/cards.go index fad4bf1..f0c818f 100644 --- a/src/game/rule/operation/cards.go +++ b/src/game/rule/operation/cards.go @@ -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) diff --git a/src/game/rule/operation/cards_test.go b/src/game/rule/operation/cards_test.go index 7c75cef..266210c 100644 --- a/src/game/rule/operation/cards_test.go +++ b/src/game/rule/operation/cards_test.go @@ -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) + } }) } }