From 11c58869511d0bb8b92964b9d71a73a7c53e366e Mon Sep 17 00:00:00 2001 From: sneak Date: Sat, 18 May 2024 21:17:38 -0700 Subject: [PATCH] got a failing test for the two pair bigger pair bug --- Makefile | 16 +++++++- examples/simgame/main.go | 80 ++++++++++++++++++++++++++++++++++++++++ examples/sixhand/main.go | 35 ------------------ go.mod | 3 ++ go.sum | 7 ++++ scoring_test.go | 61 ++++++++++++++++++++++++++++++ 6 files changed, 166 insertions(+), 36 deletions(-) create mode 100644 examples/simgame/main.go delete mode 100644 examples/sixhand/main.go diff --git a/Makefile b/Makefile index 4825ebc..d2cf72a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,18 @@ -default: test +.PHONY: examples + +default: run + +run: simgame + +simgame: examples + ./bin/simgame + +clean: + rm -rf bin test: go test -v -count=1 ./... + +examples: + test -d bin || mkdir bin + go build -o bin/ ./examples/... diff --git a/examples/simgame/main.go b/examples/simgame/main.go new file mode 100644 index 0000000..5396913 --- /dev/null +++ b/examples/simgame/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "time" + + "git.eeqj.de/sneak/pokercore" +) + +func main() { + + sleepTime := 2 * time.Second + playerCount := 8 + d := pokercore.NewDeck() + fmt.Printf("deck before shuffling: %s\n", d.FormatForTerminal()) + // this "randomly chosen" seed somehow deals pocket queens, pocket kings, and pocket aces to three players. + // what are the odds? lol + d.ShuffleDeterministically(1337) + fmt.Printf("deck after shuffling: %s\n", d.FormatForTerminal()) + var players []pokercore.Cards + for i := 0; i < playerCount; i++ { + players = append(players, d.Deal(2)) + } + for i, p := range players { + fmt.Printf("player %d: %s\n", i+1, p.FormatForTerminal()) + } + time.Sleep(2 * time.Second) + + fmt.Printf("##############################################\n") + // deal the flop + var community pokercore.Cards + community = d.Deal(3) + fmt.Printf("flop: %s\n", community.FormatForTerminal()) + time.Sleep(sleepTime) + + fmt.Printf("##############################################\n") + turn := d.Deal(1) + fmt.Printf("turn: %s\n", turn.FormatForTerminal()) + community = append(community, turn...) + fmt.Printf("board is now: %s\n", community.FormatForTerminal()) + + time.Sleep(sleepTime) + + fmt.Printf("##############################################\n") + river := d.Deal(1) + fmt.Printf("river: %s\n", river.FormatForTerminal()) + community = append(community, river...) + fmt.Printf("board is now: %s\n", community.FormatForTerminal()) + + fmt.Printf("##############################################\n") + var winningPlayer int + var winningHand *pokercore.PokerHand + for i, p := range players { + fmt.Printf("player %d: %s\n", i+1, p.FormatForTerminal()) + playerCards, err := p.Append(community).IdentifyBestFiveCardPokerHand() + if err != nil { + panic("error evaluating hand: " + err.Error()) + } + ph, err := playerCards.PokerHand() + if err != nil { + panic("error evaluating hand: " + err.Error()) + } + fmt.Printf("player %d has a %s\n", i+1, ph.Description()) + if winningHand == nil { + winningPlayer = i + winningHand = ph + continue + } + if ph.Score > winningHand.Score { + winningPlayer = i + winningHand = ph + } + } + fmt.Printf("##############################################\n") + fmt.Printf("player %d wins with %s\n", winningPlayer+1, winningHand.Description()) + fmt.Printf("##############################################\n") + fmt.Printf("What a strange game. The only winning move is to bet really big.\n") + fmt.Printf("Insert coin to play again.\n") + +} diff --git a/examples/sixhand/main.go b/examples/sixhand/main.go deleted file mode 100644 index 6a88afa..0000000 --- a/examples/sixhand/main.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "fmt" - - "git.eeqj.de/sneak/pokercore" -) - -func main() { - d := pokercore.NewDeck() - fmt.Printf("deck before shuffling: %s\n", d.FormatForTerminal()) - d.ShuffleDeterministically(1337 + 1) // 1337 gives too weird a result - fmt.Printf("deck after shuffling: %s\n", d.FormatForTerminal()) - var players []pokercore.Cards - for i := 0; i < 6; i++ { - players = append(players, d.Deal(2)) - } - for i, p := range players { - fmt.Printf("player %d: %s\n", i, p.FormatForTerminal()) - } - // deal the flop - var community pokercore.Cards - community = d.Deal(3) - fmt.Printf("flop: %s\n", community.FormatForTerminal()) - // evaluate the hands so far - for i, p := range players { - fmt.Printf("player %d: %s\n", i, p.FormatForTerminal()) - thisPlayersHand, err := p.Append(community).PokerHand() - if err != nil { - fmt.Printf("error evaluating hand: %s\n", err) - continue - } - fmt.Printf("player %d: %s\n", i, thisPlayersHand.String()) - } -} diff --git a/go.mod b/go.mod index a92ac2a..b0fa8e4 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,9 @@ require ( github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/google/go-cmp v0.6.0 // indirect + github.com/k0kubun/pp/v3 v3.2.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/onsi/ginkgo v1.16.5 // indirect diff --git a/go.sum b/go.sum index ac46b71..895bb7d 100644 --- a/go.sum +++ b/go.sum @@ -22,10 +22,16 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= +github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs= +github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA= github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/logrusorgru/aurora/v4 v4.0.0 h1:sRjfPpun/63iADiSvGGjgA1cAYegEWMPCJdUpJYn9JA= github.com/logrusorgru/aurora/v4 v4.0.0/go.mod h1:lP0iIa2nrnT/qoFXcOZSrZQpJ1o6n2CUf/hyHi2Q4ZQ= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= @@ -83,6 +89,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= diff --git a/scoring_test.go b/scoring_test.go index 5488368..011625b 100644 --- a/scoring_test.go +++ b/scoring_test.go @@ -6,6 +6,67 @@ import ( "github.com/stretchr/testify/assert" ) +func TestHandDescripionBug(t *testing.T) { + playerCount := 8 + d := NewDeck() + d.ShuffleDeterministically(1337) + var players []*Cards + players = make([]*Cards, playerCount) + for i := 1; i-1 < playerCount; i++ { + c := d.Deal(2) + players[i-1] = &c + t.Logf("Player %d dealt: %+v\n", i, c) + } + t.Logf("Players: %+v\n", players) + + community := d.Deal(5) + t.Logf("Community: %+v\n", community) + + var playerResults []*PokerHand + + for i := 1; i-1 < playerCount; i++ { + t.Logf("Player %d hole cards: %+v\n", i, *players[i-1]) + pc := append(*players[i-1], community...) + t.Logf("Player %d cards available: %+v\n", i, pc) + hand, err := pc.IdentifyBestFiveCardPokerHand() + assert.Nil(t, err, "Expected no error") + ph, err := hand.PokerHand() + assert.Nil(t, err, "Expected no error") + t.Logf("Player %d five cards used: %+v\n", i, hand) + t.Logf("Player %d poker hand: %+v\n", i, ph) + t.Logf("Player %d best hand description: %s\n", i, ph.Description()) + playerResults = append(playerResults, ph) + } + + weirdOne := playerResults[7] + + t.Logf("Weird one: %v\n", weirdOne) + t.Logf("Weird one description: %s\n", weirdOne.Description()) + + // T♠,7♠,9♦,7♣,T♥ + assert.Equal(t, weirdOne.Description(), "two pair, tens and sevens with a nine") + + scoreShouldBe := ScoreTwoPair + scoreShouldBe += 1000 * TEN.Score() + scoreShouldBe += 100 * SEVEN.Score() + scoreShouldBe += 10 * NINE.Score() + assert.Equal(t, weirdOne.Score, scoreShouldBe) + + cards := weirdOne.Hand + + assert.True(t, cards.containsTwoPair(), "Expected hand to be two pair") + + bp := cards.twoPairBiggestPair() // returns Rank, because describing a pair + assert.Equal(t, bp, TEN, "Expected biggest pair to be a ten") + + sp := cards.twoPairSmallestPair() // returns Rank, because describing a pair + assert.Equal(t, sp, SEVEN, "Expected smallest pair to be a seven") + + k := cards.twoPairKicker() // returns Card, because describing a single card + assert.Equal(t, k.Rank, NINE, "Expected kicker to be a nine") + +} + func TestAceLowStraight(t *testing.T) { hand := Cards{ AceOfSpades(),