better types, more idiomatic go

This commit is contained in:
Jeffrey Paul 2019-03-23 21:12:40 -07:00
parent 6d2c71663c
commit c5f0c257cb
2 changed files with 39 additions and 16 deletions

View File

@ -9,26 +9,48 @@ import rand "math/rand"
import "strings" import "strings"
const CLUB = "\u2663" type Suit rune
const SPADE = "\u2660" type Rank rune
const DIAMOND = "\u2666"
const HEART = "\u2665" const (
CLUB Suit = '\u2663'
SPADE Suit = '\u2660'
DIAMOND Suit = '\u2666'
HEART Suit = '\u2665'
)
/* /*
// emoji are cooler anyway
const CLUB = "C" const CLUB = "C"
const SPADE = "S" const SPADE = "S"
const DIAMOND = "D" const DIAMOND = "D"
const HEART = "H" 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 { type TestGenerationIteration struct {
Deck *Deck Deck *Deck
Seed int64 Seed int64
} }
type Card struct { type Card struct {
Rank string Rank Rank
Suit string Suit Suit
} }
type Cards []*Card type Cards []*Card
@ -54,13 +76,13 @@ func formatCardForTerminal(c Card) (output string) {
var suit string var suit string
color := Red color := Red
switch c.Suit { switch c.Suit {
case DIAMOND: case Suit(DIAMOND):
color = Blue color = Blue
case HEART: case Suit(HEART):
color = Red color = Red
case CLUB: case Suit(CLUB):
color = Green color = Green
case SPADE: case Suit(SPADE):
color = Black color = Black
} }
@ -80,17 +102,19 @@ func cryptoUint64() (v uint64) {
} }
func (self *Card) String() string { 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 { func NewDeck() *Deck {
self := new(Deck) self := new(Deck)
ranks := []string{"A", "2", "3", "4", "5", "6", "7", ranks := []Rank{
"8", "9", "T", "J", "Q", "K"} 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) self.Cards = make([]*Card, 52)

View File

@ -8,8 +8,7 @@ type ShuffleTestResults []struct {
Expected string Expected string
} }
func TestPokerHand(t *testing.T) { func TestPokerDeck(t *testing.T) {
//test stuff here
d := NewDeck() d := NewDeck()
d.ShuffleDeterministically(437) d.ShuffleDeterministically(437)
cards := d.Deal(7) cards := d.Deal(7)