added more tests, cleaned up example app

This commit is contained in:
Jeffrey Paul 2024-05-18 23:00:19 -07:00
parent 0757f12dfa
commit f135f480be
4 changed files with 90 additions and 28 deletions

View File

@ -5,13 +5,13 @@ default: run
run: simgame
simgame: examples
./bin/simgame
RANDOM_SHUFFLE=1 ./bin/simgame
clean:
rm -rf bin *.test
test:
go test -v -count=1 ./...
go test -count=1 .
examples:
test -d bin || mkdir bin

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"os"
"time"
"git.eeqj.de/sneak/pokercore"
@ -15,7 +16,6 @@ type Player struct {
Hand pokercore.Cards
ScoredHand *pokercore.PokerHand
Position int
CommunityCardsAvailable pokercore.Cards
}
type Game struct {
@ -25,15 +25,38 @@ type Game struct {
Street int
}
func NewGame(seed int) *Game {
func suspense() {
fmt.Printf("########################################\n")
if os.Getenv("NO_LAG") == "" {
time.Sleep(delayTime)
}
}
func NewGame() *Game {
// this "randomly chosen" seed somehow deals pocket queens, pocket kings, and pocket aces to three players.
// what are the odds? lol
//g := NewGame(1337))
// nothing up my sleeve:
seed := 3141592653
g := &Game{}
g.Street = 0
g.Deck = pokercore.NewDeck()
g.SpreadCards()
fmt.Printf("Shuffle up and deal!\n")
time.Sleep(delayTime)
suspense()
fmt.Printf("Shuffling...\n")
g.Deck.ShuffleDeterministically(3141592653)
if os.Getenv("RANDOM_SHUFFLE") != "" {
g.Deck.ShuffleRandomly()
} else {
fmt.Printf("Using deterministic shuffle seed: %d\n", seed)
g.Deck.ShuffleDeterministically(int64(seed))
}
return g
}
@ -121,32 +144,25 @@ func (g *Game) ShowWinner() {
}
func main() {
// this "randomly chosen" seed somehow deals pocket queens, pocket kings, and pocket aces to three players.
// what are the odds? lol
//g := NewGame(1337))
// nothing up my sleeve:
g := NewGame(3141592653)
g := NewGame()
g.ShowGameStatus()
g.DealPlayersIn()
suspense()
g.ShowGameStatus()
g.DealFlop()
suspense()
g.ShowGameStatus()
g.DealTurn()
suspense()
g.ShowGameStatus()
g.DealRiver()
suspense()
g.ShowGameStatus()
g.ShowWinner()
fmt.Printf("What a strange game. The only winning move is to bet really big.\n")

View File

@ -56,8 +56,13 @@ func measureHandFinding(t *testing.T, iterations int, cardCount int) {
if err != nil {
t.Fatalf("Error measuring function: %v", err)
}
t.Logf("Searching %d random cards for a hand takes on average %s", cardCount, result.Mean)
t.Logf("Over %d iterations the min was %s and the max was %s", iterations, result.Min, result.Max)
t.Logf("The standard deviation was %s", result.StdDev)
t.Logf("The median was %s", result.Median)
// Print the timing results.
t.Logf(result.String())
//t.Logf(result.String())
}
func findHandInRandomCards(t *testing.T, shuffleSeed int, cardCount int) {

View File

@ -83,12 +83,13 @@ func TestAceLowStraight(t *testing.T) {
assert.Equal(t, ph.Score, ScoreStraight+FIVE.Score())
assert.Equal(t, ph.Description(), "a five high straight")
assert.True(t, hand.HighestRank() == ACE, "Expected highest rank to be an ace")
assert.True(t, hand.SortByRankAscending().First().Rank == DEUCE, "Expected first card to be a deuce")
assert.True(t, hand.SortByRankAscending().Last().Rank == ACE, "Expected last card in sorted to be a ace")
assert.True(t, hand.SortByRankAscending().Second().Rank == THREE, "Expected second card to be a three")
assert.True(t, hand.SortByRankAscending().Third().Rank == FOUR, "Expected third card to be a four")
assert.True(t, hand.SortByRankAscending().Fourth().Rank == FIVE, "Expected fourth card to be a five")
assert.True(t, hand.SortByRankAscending().Fifth().Rank == ACE, "Expected fifth card to be an ace")
s := hand.SortByRankAscending()
assert.True(t, s.First().Rank == DEUCE, "Expected first card to be a deuce")
assert.True(t, s.Last().Rank == ACE, "Expected last card in sorted to be a ace")
assert.True(t, s.Second().Rank == THREE, "Expected second card to be a three")
assert.True(t, s.Third().Rank == FOUR, "Expected third card to be a four")
assert.True(t, s.Fourth().Rank == FIVE, "Expected fourth card to be a five")
assert.True(t, s.Fifth().Rank == ACE, "Expected fifth card to be an ace")
}
func TestAceHighStraight(t *testing.T) {
@ -197,6 +198,35 @@ func TestStraightFlush(t *testing.T) {
assert.True(t, shuffledHand.HighestRank() == SIX, "Expected highest rank to be a six after shuffle even with aces low")
}
func TestFourOfAKind(t *testing.T) {
hand := Cards{
SixOfSpades(),
SixOfHearts(),
SixOfDiamonds(),
SixOfClubs(),
FiveOfSpades(),
}
assert.False(t, hand.containsStraight(), "Expected hand to not be a straight")
assert.False(t, hand.containsFlush(), "Expected hand to not be a flush")
assert.False(t, hand.containsStraightFlush(), "Expected hand to not be a straight flush")
assert.False(t, hand.containsRoyalFlush(), "Expected hand to not be a royal flush")
assert.True(t, hand.containsFourOfAKind(), "Expected hand to be four of a kind")
assert.False(t, hand.containsFullHouse(), "Expected hand to not be a full house")
// note that these are *expected* to be true. the contains* functions
// are used in the PokerHand.calculateScore method to determine the best hand
// and are checked in sequence of descending value, so if a hand is four of a kind
// it will not be checked for full house, three of a kind, etc.
// technically quads *is* two pair also, and a hand with quads does
// indeed contain three of a kind, and contains a pair.
assert.True(t, hand.containsThreeOfAKind(), "Expected hand to contain three of a kind")
assert.True(t, hand.containsTwoPair(), "Expected hand to contain two pair")
assert.True(t, hand.containsPair(), "Expected hand to contain a pair")
assert.True(t, hand.HighestRank() == SIX, "Expected highest rank to be a six")
}
func TestRoyalFlush(t *testing.T) {
hand := Cards{
TenOfSpades(),
@ -262,6 +292,17 @@ func TestTwoPair(t *testing.T) {
assert.False(t, hand.isUnmadeHand(), "Expected hand to not be unmade")
}
func TestDetectDuplicates(t *testing.T) {
hand := Cards{
KingOfSpades(),
JackOfDiamonds(),
JackOfSpades(),
KingOfSpades(),
TenOfSpades(),
}
assert.True(t, hand.containsDuplicates(), "Expected hand to contain duplicates")
}
func TestHandScore(t *testing.T) {
hand := Cards{
KingOfSpades(),