From c5f0c257cbc679fdc5e53ae5509827467aa4778c Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Sat, 23 Mar 2019 21:12:40 -0700 Subject: [PATCH] better types, more idiomatic go --- pokercore/pokercore.go | 52 +++++++++++++++++++++++++++---------- pokercore/pokercore_test.go | 3 +-- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/pokercore/pokercore.go b/pokercore/pokercore.go index 303d45f..6512924 100644 --- a/pokercore/pokercore.go +++ b/pokercore/pokercore.go @@ -9,26 +9,48 @@ import rand "math/rand" import "strings" -const CLUB = "\u2663" -const SPADE = "\u2660" -const DIAMOND = "\u2666" -const HEART = "\u2665" +type Suit rune +type Rank rune + +const ( + CLUB Suit = '\u2663' + SPADE Suit = '\u2660' + DIAMOND Suit = '\u2666' + HEART Suit = '\u2665' +) /* +// emoji are cooler anyway const CLUB = "C" const SPADE = "S" const DIAMOND = "D" const HEART = "H" */ +const ( + ACE Rank = 'A' + DEUCE Rank = '2' + THREE Rank = '3' + FOUR Rank = '4' + FIVE Rank = '5' + SIX Rank = '6' + SEVEN Rank = '7' + EIGHT Rank = '8' + NINE Rank = '9' + TEN Rank = 'T' + JACK Rank = 'J' + QUEEN Rank = 'Q' + KING Rank = 'K' +) + type TestGenerationIteration struct { Deck *Deck Seed int64 } type Card struct { - Rank string - Suit string + Rank Rank + Suit Suit } type Cards []*Card @@ -54,13 +76,13 @@ func formatCardForTerminal(c Card) (output string) { var suit string color := Red switch c.Suit { - case DIAMOND: + case Suit(DIAMOND): color = Blue - case HEART: + case Suit(HEART): color = Red - case CLUB: + case Suit(CLUB): color = Green - case SPADE: + case Suit(SPADE): color = Black } @@ -80,17 +102,19 @@ func cryptoUint64() (v uint64) { } func (self *Card) String() string { - return fmt.Sprintf("%s%s", self.Rank, self.Suit) + return fmt.Sprintf("%s%s", string(self.Rank), string(self.Suit)) } func NewDeck() *Deck { self := new(Deck) - ranks := []string{"A", "2", "3", "4", "5", "6", "7", - "8", "9", "T", "J", "Q", "K"} + ranks := []Rank{ + ACE, DEUCE, THREE, FOUR, FIVE, + SIX, SEVEN, EIGHT, NINE, TEN, JACK, + QUEEN, KING} - suits := []string{HEART, DIAMOND, CLUB, SPADE} + suits := []Suit{HEART, DIAMOND, CLUB, SPADE} self.Cards = make([]*Card, 52) diff --git a/pokercore/pokercore_test.go b/pokercore/pokercore_test.go index 24077ba..e525155 100644 --- a/pokercore/pokercore_test.go +++ b/pokercore/pokercore_test.go @@ -8,8 +8,7 @@ type ShuffleTestResults []struct { Expected string } -func TestPokerHand(t *testing.T) { - //test stuff here +func TestPokerDeck(t *testing.T) { d := NewDeck() d.ShuffleDeterministically(437) cards := d.Deal(7)