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"
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)

View File

@ -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)