Browse Source

better types, more idiomatic go

master
Jeffrey Paul 3 years ago
parent
commit
c5f0c257cb
  1. 52
      pokercore/pokercore.go
  2. 3
      pokercore/pokercore_test.go

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

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

Loading…
Cancel
Save