impl cards
This commit is contained in:
@@ -5,8 +5,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Hand struct {
|
type Hand struct {
|
||||||
Public []Card
|
Public []Card
|
||||||
Private []Card
|
Private []Card
|
||||||
|
ReversePublic []Card
|
||||||
|
ReversePrivate []Card
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hand *Hand) Push(card Card) {
|
func (hand *Hand) Push(card Card) {
|
||||||
@@ -17,12 +19,12 @@ func (hand *Hand) Push(card Card) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hand Hand) Len() int {
|
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 {
|
func (hand Hand) AllCards() []Card {
|
||||||
allcards := make([]Card, 0, len(hand.Public)+len(hand.Private))
|
allcards := make([]Card, 0, hand.Len())
|
||||||
for _, cards := range [][]Card{hand.Public, hand.Private} {
|
for _, cards := range [][]Card{hand.Public, hand.Private, hand.ReversePublic, hand.ReversePrivate} {
|
||||||
allcards = append(allcards, cards...)
|
allcards = append(allcards, cards...)
|
||||||
}
|
}
|
||||||
return allcards
|
return allcards
|
||||||
|
|||||||
@@ -1,17 +1,23 @@
|
|||||||
package operation
|
package operation
|
||||||
|
|
||||||
import "local/sandbox/cards/src/entity"
|
import (
|
||||||
|
"local/sandbox/cards/src/entity"
|
||||||
|
)
|
||||||
|
|
||||||
type Cards struct {
|
type Cards struct {
|
||||||
AceLow bool
|
AceLow bool
|
||||||
|
Jokers int
|
||||||
WithoutValue []Value
|
WithoutValue []Value
|
||||||
WithoutSuit []Suit
|
WithoutSuit []Suit
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cards Cards) NewDeck() entity.Deck {
|
func (cards Cards) NewDeck() entity.Deck {
|
||||||
deck := new54DeckAceHigh()
|
deck := new52DeckAceHigh()
|
||||||
if cards.AceLow {
|
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-- {
|
for i := len(deck.Deck) - 1; i >= 0; i-- {
|
||||||
card := deck.Deck[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.Deck = append(deck.Deck[:i], deck.Deck[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
deck.Shuffle()
|
||||||
return deck
|
return deck
|
||||||
}
|
}
|
||||||
|
|
||||||
func new54DeckAceHigh() entity.Deck {
|
func new52DeckAceHigh() entity.Deck {
|
||||||
cards := make([]entity.Card, 0, 54)
|
cards := make([]entity.Card, 0, 52)
|
||||||
for _, suit := range suitStrings {
|
for _, suit := range suitStrings {
|
||||||
for _, value := range valueStrings {
|
for _, value := range valueStrings {
|
||||||
if value != AceLow && value != Joker {
|
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 := entity.Deck{Deck: cards}
|
||||||
deck.Shuffle()
|
deck.Shuffle()
|
||||||
return deck
|
return deck
|
||||||
}
|
}
|
||||||
|
|
||||||
func new54DeckAceLow() entity.Deck {
|
func new52DeckAceLow() entity.Deck {
|
||||||
deck := new54DeckAceHigh()
|
deck := new52DeckAceHigh()
|
||||||
for i, card := range deck.Deck {
|
for i, card := range deck.Deck {
|
||||||
if card.Value == int(AceHigh) {
|
if card.Value == int(AceHigh) {
|
||||||
deck.Deck[i].Value = int(AceLow)
|
deck.Deck[i].Value = int(AceLow)
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package operation
|
package operation
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"local/sandbox/cards/src/entity"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestNewDeck54(t *testing.T) {
|
func TestNewDeck52(t *testing.T) {
|
||||||
deck := new54DeckAceLow()
|
deck := new52DeckAceLow()
|
||||||
if len(deck.Deck) != 54 {
|
if len(deck.Deck) != 52 {
|
||||||
t.Fatal(len(deck.Deck))
|
t.Fatal(len(deck.Deck))
|
||||||
}
|
}
|
||||||
for _, card := range deck.Deck {
|
for _, card := range deck.Deck {
|
||||||
@@ -17,13 +20,48 @@ func TestNewDeck54(t *testing.T) {
|
|||||||
func TestCardsNewDeck(t *testing.T) {
|
func TestCardsNewDeck(t *testing.T) {
|
||||||
cases := map[string]struct {
|
cases := map[string]struct {
|
||||||
cards Cards
|
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 {
|
for name, d := range cases {
|
||||||
c := d
|
c := d
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
|
deck := c.cards.NewDeck()
|
||||||
|
if !c.check(deck) {
|
||||||
|
t.Fatal(deck)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user