cleaning up now that i actually know this language
This commit is contained in:
parent
5289841027
commit
c2dfd09e9f
8
Makefile
8
Makefile
|
@ -1,4 +1,8 @@
|
||||||
default: test
|
default: test
|
||||||
|
|
||||||
test:
|
test: *.go
|
||||||
cd pokercore && make test
|
go get -t -v
|
||||||
|
go test -v
|
||||||
|
|
||||||
|
run:
|
||||||
|
cd cmd/* && go get -v && go build .
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
package poker
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
import "fmt"
|
||||||
|
import crand "crypto/rand"
|
||||||
|
import . "github.com/logrusorgru/aurora"
|
||||||
|
import log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
type Suit rune
|
||||||
|
type Rank rune
|
||||||
|
|
||||||
|
const (
|
||||||
|
CLUB Suit = '\u2663'
|
||||||
|
SPADE Suit = '\u2660'
|
||||||
|
DIAMOND Suit = '\u2666'
|
||||||
|
HEART Suit = '\u2665'
|
||||||
|
)
|
||||||
|
|
||||||
|
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 Card struct {
|
||||||
|
Rank Rank
|
||||||
|
Suit Suit
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cards []*Card
|
||||||
|
|
||||||
|
func (c *Cards) formatForTerminal() (output string) {
|
||||||
|
var cardstrings []string
|
||||||
|
for _, card := range *c {
|
||||||
|
cardstrings = append(cardstrings, card.formatForTerminal())
|
||||||
|
}
|
||||||
|
output = strings.Join(cardstrings, ",")
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Card) formatForTerminal() (output string) {
|
||||||
|
var rank string
|
||||||
|
var suit string
|
||||||
|
color := Red
|
||||||
|
switch c.Suit {
|
||||||
|
case Suit(DIAMOND):
|
||||||
|
color = Blue
|
||||||
|
case Suit(HEART):
|
||||||
|
color = Red
|
||||||
|
case Suit(CLUB):
|
||||||
|
color = Green
|
||||||
|
case Suit(SPADE):
|
||||||
|
color = Black
|
||||||
|
}
|
||||||
|
|
||||||
|
rank = fmt.Sprintf("%s", BgGray(Bold(color(*c.Rank))))
|
||||||
|
suit = fmt.Sprintf("%s", BgGray(Bold(color(*c.Suit))))
|
||||||
|
output = fmt.Sprintf("%s%s", rank, suit)
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (self *Card) String() string {
|
||||||
|
return fmt.Sprintf("%s%s", string(self.Rank), string(self.Suit))
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/sneak/gopoker/pokercore"
|
"github.com/sneak/poker"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/rpc"
|
"net/rpc"
|
|
@ -0,0 +1,92 @@
|
||||||
|
package poker
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
import "fmt"
|
||||||
|
import crand "crypto/rand"
|
||||||
|
import . "github.com/logrusorgru/aurora"
|
||||||
|
import log "github.com/sirupsen/logrus"
|
||||||
|
import rand "math/rand"
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
type Deck struct {
|
||||||
|
Cards Cards
|
||||||
|
DealIndex int
|
||||||
|
ShuffleSeedVal int64
|
||||||
|
}
|
||||||
|
|
||||||
|
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 NewShuffledDeck() *Deck {
|
||||||
|
d := newDeck()
|
||||||
|
d.ShuffleSeedVal = int64(cryptoUint64())
|
||||||
|
d.Shuffle()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDeckFromSeed(seed int64) *Deck {
|
||||||
|
d := newDeck()
|
||||||
|
d.ShuffleSeedVal = seed
|
||||||
|
d.Shuffle()
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDeck() *Deck {
|
||||||
|
|
||||||
|
self := new(Deck)
|
||||||
|
|
||||||
|
ranks := []Rank{
|
||||||
|
ACE, DEUCE, THREE, FOUR, FIVE,
|
||||||
|
SIX, SEVEN, EIGHT, NINE, TEN, JACK,
|
||||||
|
QUEEN, KING}
|
||||||
|
|
||||||
|
suits := []Suit{HEART, DIAMOND, CLUB, SPADE}
|
||||||
|
|
||||||
|
self.Cards = make([]*Card, 52)
|
||||||
|
|
||||||
|
tot := 0
|
||||||
|
for i := 0; i < len(ranks); i++ {
|
||||||
|
for n := 0; n < len(suits); n++ {
|
||||||
|
self.Cards[tot] = &Card{
|
||||||
|
Rank: ranks[i],
|
||||||
|
Suit: suits[n],
|
||||||
|
}
|
||||||
|
tot++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.DealIndex = 0
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Deck) Shuffle() {
|
||||||
|
//FIXME(sneak) not sure if this is constant time or not
|
||||||
|
rnd := rand.New(rand.NewSource(self.ShuffleSeedVal)))
|
||||||
|
rnd.Shuffle(len(self.Cards), func(i, j int) { self.Cards[i], self.Cards[j] = self.Cards[j], self.Cards[i] })
|
||||||
|
self.DealIndex = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Deck) Deal(n int) (output Cards) {
|
||||||
|
|
||||||
|
if (self.DealIndex + n) > len(self.Cards) {
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
output = append(output, self.Cards[self.DealIndex+1])
|
||||||
|
self.DealIndex++
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Deck) Dealt() int {
|
||||||
|
return self.DealIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Deck) Remaining() int {
|
||||||
|
return (len(self.Cards) - self.DealIndex)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package pokercore
|
package poker
|
||||||
|
|
||||||
import "github.com/stretchr/testify/assert"
|
import "github.com/stretchr/testify/assert"
|
||||||
import "testing"
|
import "testing"
|
|
@ -1,4 +1,4 @@
|
||||||
package pokercore
|
package poker
|
||||||
|
|
||||||
type HEPokerHand struct {
|
type HEPokerHand struct {
|
||||||
Cards Cards
|
Cards Cards
|
||||||
|
@ -28,7 +28,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (self *HandScore) BeatsHand(v *HandScore) bool {
|
func (self *HandScore) BeatsHand(v *HandScore) bool {
|
||||||
|
panic("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *HEPokerHand) IsReducedYet() bool {
|
func (self *HEPokerHand) IsReducedYet() bool {
|
||||||
|
@ -53,7 +53,7 @@ func (self *HEPokerHand) IsStraight() bool {
|
||||||
func (self *HEPokerHand) IsFlush() bool {
|
func (self *HEPokerHand) IsFlush() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *HEPokerHand) IsBoat() bool {
|
func (self *HEPokerHand) IsFullHouse() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *HEPokerHand) ScoreHand() *HandScore {
|
func (self *HEPokerHand) ScoreHand() *HandScore {
|
|
@ -1,10 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
import "github.com/sneak/gopoker/pokercore"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
myDeck := pokercore.NewDeck()
|
|
||||||
myDeck.ShuffleRandomly()
|
|
||||||
fmt.Println("%s", myDeck.Cards)
|
|
||||||
}
|
|
|
@ -1,10 +0,0 @@
|
||||||
default: test
|
|
||||||
|
|
||||||
.PHONY: pkgs test
|
|
||||||
|
|
||||||
fetch:
|
|
||||||
go get -t
|
|
||||||
|
|
||||||
test: *.go
|
|
||||||
go test -v
|
|
||||||
|
|
|
@ -1,191 +0,0 @@
|
||||||
package pokercore
|
|
||||||
|
|
||||||
import "encoding/binary"
|
|
||||||
import "fmt"
|
|
||||||
import crand "crypto/rand"
|
|
||||||
import . "github.com/logrusorgru/aurora"
|
|
||||||
import log "github.com/sirupsen/logrus"
|
|
||||||
import rand "math/rand"
|
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
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 Rank
|
|
||||||
Suit Suit
|
|
||||||
}
|
|
||||||
|
|
||||||
type Cards []*Card
|
|
||||||
|
|
||||||
type Deck struct {
|
|
||||||
Cards Cards
|
|
||||||
DealIndex int
|
|
||||||
ShuffleSeedVal int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func formatCardsForTerminal(c Cards) (output string) {
|
|
||||||
var cardstrings []string
|
|
||||||
for i := 0; i < len(c); i++ {
|
|
||||||
cardstrings = append(cardstrings, formatCardForTerminal(*c[i]))
|
|
||||||
}
|
|
||||||
output = strings.Join(cardstrings, ",")
|
|
||||||
return output
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func formatCardForTerminal(c Card) (output string) {
|
|
||||||
var rank string
|
|
||||||
var suit string
|
|
||||||
color := Red
|
|
||||||
switch c.Suit {
|
|
||||||
case Suit(DIAMOND):
|
|
||||||
color = Blue
|
|
||||||
case Suit(HEART):
|
|
||||||
color = Red
|
|
||||||
case Suit(CLUB):
|
|
||||||
color = Green
|
|
||||||
case Suit(SPADE):
|
|
||||||
color = Black
|
|
||||||
}
|
|
||||||
|
|
||||||
rank = fmt.Sprintf("%s", BgGray(Bold(color(c.Rank))))
|
|
||||||
suit = fmt.Sprintf("%s", BgGray(Bold(color(c.Suit))))
|
|
||||||
output = fmt.Sprintf("%s%s", rank, suit)
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (self *Card) String() string {
|
|
||||||
return fmt.Sprintf("%s%s", string(self.Rank), string(self.Suit))
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDeck() *Deck {
|
|
||||||
|
|
||||||
self := new(Deck)
|
|
||||||
|
|
||||||
ranks := []Rank{
|
|
||||||
ACE, DEUCE, THREE, FOUR, FIVE,
|
|
||||||
SIX, SEVEN, EIGHT, NINE, TEN, JACK,
|
|
||||||
QUEEN, KING}
|
|
||||||
|
|
||||||
suits := []Suit{HEART, DIAMOND, CLUB, SPADE}
|
|
||||||
|
|
||||||
self.Cards = make([]*Card, 52)
|
|
||||||
|
|
||||||
tot := 0
|
|
||||||
for i := 0; i < len(ranks); i++ {
|
|
||||||
for n := 0; n < len(suits); n++ {
|
|
||||||
self.Cards[tot] = &Card{
|
|
||||||
Rank: ranks[i],
|
|
||||||
Suit: suits[n],
|
|
||||||
}
|
|
||||||
tot++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.DealIndex = 0
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Deck) ShuffleRandomly() {
|
|
||||||
rnd := rand.New(rand.NewSource(int64(cryptoUint64())))
|
|
||||||
//FIXME(sneak) not sure if this is constant time or not
|
|
||||||
rnd.Shuffle(len(self.Cards), func(i, j int) { self.Cards[i], self.Cards[j] = self.Cards[j], self.Cards[i] })
|
|
||||||
self.DealIndex = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Deck) ShuffleDeterministically(seed int64) {
|
|
||||||
r := rand.New(rand.NewSource(seed))
|
|
||||||
//FIXME(sneak) not sure if this is constant time or not
|
|
||||||
r.Shuffle(len(self.Cards), func(i, j int) { self.Cards[i], self.Cards[j] = self.Cards[j], self.Cards[i] })
|
|
||||||
self.DealIndex = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Deck) Deal(n int) (output Cards) {
|
|
||||||
|
|
||||||
if (self.DealIndex + n) > len(self.Cards) {
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
output = append(output, self.Cards[self.DealIndex+1])
|
|
||||||
self.DealIndex++
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Deck) Dealt() int {
|
|
||||||
return self.DealIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Deck) Remaining() int {
|
|
||||||
return (len(self.Cards) - self.DealIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func generate() {
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
}
|
|
||||||
|
|
||||||
func proto() {
|
|
||||||
myDeck := NewDeck()
|
|
||||||
myDeck.ShuffleDeterministically(42)
|
|
||||||
myHand := myDeck.Deal(2)
|
|
||||||
//spew.Dump(myHand)
|
|
||||||
fmt.Printf("my hand: %s\n", myHand)
|
|
||||||
cmty := myDeck.Deal(5)
|
|
||||||
fmt.Printf("community: %s\n", cmty)
|
|
||||||
}
|
|
Loading…
Reference in New Issue