go-poker/pokercore/pokercore.go

176 lines
3.1 KiB
Go
Raw Permalink Normal View History

2019-03-23 09:43:25 +00:00
package pokercore
import (
"encoding/binary"
"fmt"
"sort"
2019-03-23 09:43:25 +00:00
crand "crypto/rand"
2019-03-23 09:43:25 +00:00
"github.com/logrusorgru/aurora/v4"
2019-03-23 09:43:25 +00:00
log "github.com/sirupsen/logrus"
2019-03-23 09:43:25 +00:00
"strings"
)
2019-03-23 09:43:25 +00:00
2019-03-24 04:12:40 +00:00
type Suit rune
2019-03-23 09:43:25 +00:00
func (s Suit) String() string {
return string(s)
2019-03-23 09:43:25 +00:00
}
2019-03-24 04:12:40 +00:00
type Rank rune
func (r Rank) String() string {
return string(r)
2019-03-23 11:21:03 +00:00
}
2019-03-24 04:12:40 +00:00
const (
CLUB Suit = '\u2663' // ♣
SPADE Suit = '\u2660' // ♠
DIAMOND Suit = '\u2666' // ♦
HEART Suit = '\u2665' // ♥
2019-03-24 04:12:40 +00:00
)
2019-03-23 09:43:25 +00:00
2019-03-24 04:12:40 +00:00
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'
)
2019-03-23 09:43:25 +00:00
type Card struct {
2019-03-24 04:12:40 +00:00
Rank Rank
Suit Suit
2019-03-23 09:43:25 +00:00
}
type Cards []Card
2019-03-23 09:43:25 +00:00
func (r Rank) Int(x AcesHighOrLow) int {
return int(rankToScore(r, x))
2019-03-23 09:43:25 +00:00
}
func (r Rank) HandScore(x AcesHighOrLow) HandScore {
return HandScore(r.Int(x))
}
2019-03-23 09:43:25 +00:00
func (c Cards) SortByRank(x AcesHighOrLow) Cards {
2019-03-23 09:43:25 +00:00
newCards := make(Cards, len(c))
copy(newCards, c)
2019-03-23 09:43:25 +00:00
sort.Slice(newCards, func(i, j int) bool {
return rankToScore(newCards[i].Rank, x) > rankToScore(newCards[j].Rank, x)
})
return newCards
2019-03-23 09:43:25 +00:00
}
type TestGenerationIteration struct {
Deck *Deck
Seed int64
2019-03-23 09:57:58 +00:00
}
func (c Cards) PrintToTerminal() {
fmt.Printf("%s", c.FormatCardsForTerminal())
2019-03-23 09:43:25 +00:00
}
func (c Cards) FormatCardsForTerminal() string {
2019-03-23 11:21:03 +00:00
var cardstrings []string
for i := 0; i < len(c); i++ {
cardstrings = append(cardstrings, c[i].FormatForTerminal())
2019-03-23 09:43:25 +00:00
}
return strings.Join(cardstrings, ",")
2019-03-23 09:43:25 +00:00
}
func (c Card) FormatForTerminal() string {
2019-03-23 09:43:25 +00:00
var rank string
var suit string
color := aurora.Red
2019-03-23 11:21:03 +00:00
switch c.Suit {
2019-03-24 04:12:40 +00:00
case Suit(DIAMOND):
color = aurora.Blue
2019-03-24 04:12:40 +00:00
case Suit(HEART):
color = aurora.Red
2019-03-24 04:12:40 +00:00
case Suit(CLUB):
color = aurora.Green
2019-03-24 04:12:40 +00:00
case Suit(SPADE):
color = aurora.Black
2019-03-23 09:43:25 +00:00
}
rank = fmt.Sprintf("%s", aurora.Bold(color(c.Rank.String())))
suit = fmt.Sprintf("%s", aurora.Bold(color(c.Suit.String())))
return fmt.Sprintf("%s%s", rank, suit)
2019-03-23 11:21:03 +00:00
}
func cryptoUint64() (v uint64) {
err := binary.Read(crand.Reader, binary.BigEndian, &v)
if err != nil {
log.Fatal(err)
}
log.Debugf("crand cryptosource is returning Uint64: %d", v)
return v
}
func (c *Card) String() string {
return fmt.Sprintf("%s%s", string(c.Rank), string(c.Suit))
2019-03-23 09:43:25 +00:00
}
func (c Cards) HighestRank(x AcesHighOrLow) Rank {
c = c.SortByRank(x)
return c[0].Rank
2019-03-23 11:21:03 +00:00
}
2019-03-23 09:43:25 +00:00
func (s Cards) String() (output string) {
var cardstrings []string
for i := 0; i < len(s); i++ {
cardstrings = append(cardstrings, s[i].String())
}
output = strings.Join(cardstrings, ",")
return output
}
2019-03-23 09:57:58 +00:00
func generate() {
2019-03-23 09:43:25 +00:00
log.SetLevel(log.DebugLevel)
2019-03-23 09:57:58 +00:00
}
func proto() {
2019-03-23 09:43:25 +00:00
myDeck := NewDeck()
2019-03-23 11:21:03 +00:00
myDeck.ShuffleDeterministically(42)
2019-03-23 09:43:25 +00:00
myHand := myDeck.Deal(2)
//spew.Dump(myHand)
fmt.Printf("my hand: %s\n", myHand)
cmty := myDeck.Deal(5)
fmt.Printf("community: %s\n", cmty)
}
2019-03-23 11:21:03 +00:00
func contains(ranks []Rank, rank Rank) bool {
for _, r := range ranks {
if r == rank {
return true
}
}
return false
}
func unique(intSlice []int) []int {
keys := make(map[int]bool)
list := []int{}
for _, entry := range intSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
2019-03-23 11:21:03 +00:00
}