added more tests, cleaned up example app
This commit is contained in:
parent
0757f12dfa
commit
f135f480be
4
Makefile
4
Makefile
@ -5,13 +5,13 @@ default: run
|
|||||||
run: simgame
|
run: simgame
|
||||||
|
|
||||||
simgame: examples
|
simgame: examples
|
||||||
./bin/simgame
|
RANDOM_SHUFFLE=1 ./bin/simgame
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf bin *.test
|
rm -rf bin *.test
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -v -count=1 ./...
|
go test -count=1 .
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
test -d bin || mkdir bin
|
test -d bin || mkdir bin
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.eeqj.de/sneak/pokercore"
|
"git.eeqj.de/sneak/pokercore"
|
||||||
@ -15,7 +16,6 @@ type Player struct {
|
|||||||
Hand pokercore.Cards
|
Hand pokercore.Cards
|
||||||
ScoredHand *pokercore.PokerHand
|
ScoredHand *pokercore.PokerHand
|
||||||
Position int
|
Position int
|
||||||
CommunityCardsAvailable pokercore.Cards
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
@ -25,15 +25,38 @@ type Game struct {
|
|||||||
Street int
|
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 := &Game{}
|
||||||
g.Street = 0
|
g.Street = 0
|
||||||
|
|
||||||
g.Deck = pokercore.NewDeck()
|
g.Deck = pokercore.NewDeck()
|
||||||
g.SpreadCards()
|
g.SpreadCards()
|
||||||
|
|
||||||
fmt.Printf("Shuffle up and deal!\n")
|
fmt.Printf("Shuffle up and deal!\n")
|
||||||
time.Sleep(delayTime)
|
suspense()
|
||||||
fmt.Printf("Shuffling...\n")
|
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
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,32 +144,25 @@ func (g *Game) ShowWinner() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
g := NewGame()
|
||||||
// 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.ShowGameStatus()
|
g.ShowGameStatus()
|
||||||
|
|
||||||
g.DealPlayersIn()
|
g.DealPlayersIn()
|
||||||
|
suspense()
|
||||||
|
|
||||||
g.ShowGameStatus()
|
g.ShowGameStatus()
|
||||||
|
|
||||||
g.DealFlop()
|
g.DealFlop()
|
||||||
|
suspense()
|
||||||
|
|
||||||
g.ShowGameStatus()
|
g.ShowGameStatus()
|
||||||
|
|
||||||
g.DealTurn()
|
g.DealTurn()
|
||||||
|
suspense()
|
||||||
|
|
||||||
g.ShowGameStatus()
|
g.ShowGameStatus()
|
||||||
|
|
||||||
g.DealRiver()
|
g.DealRiver()
|
||||||
|
suspense()
|
||||||
|
|
||||||
g.ShowGameStatus()
|
g.ShowGameStatus()
|
||||||
|
|
||||||
g.ShowWinner()
|
g.ShowWinner()
|
||||||
|
|
||||||
fmt.Printf("What a strange game. The only winning move is to bet really big.\n")
|
fmt.Printf("What a strange game. The only winning move is to bet really big.\n")
|
||||||
|
@ -56,8 +56,13 @@ func measureHandFinding(t *testing.T, iterations int, cardCount int) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error measuring function: %v", err)
|
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.
|
// Print the timing results.
|
||||||
t.Logf(result.String())
|
//t.Logf(result.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func findHandInRandomCards(t *testing.T, shuffleSeed int, cardCount int) {
|
func findHandInRandomCards(t *testing.T, shuffleSeed int, cardCount int) {
|
||||||
|
@ -83,12 +83,13 @@ func TestAceLowStraight(t *testing.T) {
|
|||||||
assert.Equal(t, ph.Score, ScoreStraight+FIVE.Score())
|
assert.Equal(t, ph.Score, ScoreStraight+FIVE.Score())
|
||||||
assert.Equal(t, ph.Description(), "a five high straight")
|
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.HighestRank() == ACE, "Expected highest rank to be an ace")
|
||||||
assert.True(t, hand.SortByRankAscending().First().Rank == DEUCE, "Expected first card to be a deuce")
|
s := hand.SortByRankAscending()
|
||||||
assert.True(t, hand.SortByRankAscending().Last().Rank == ACE, "Expected last card in sorted to be a ace")
|
assert.True(t, s.First().Rank == DEUCE, "Expected first card to be a deuce")
|
||||||
assert.True(t, hand.SortByRankAscending().Second().Rank == THREE, "Expected second card to be a three")
|
assert.True(t, s.Last().Rank == ACE, "Expected last card in sorted to be a ace")
|
||||||
assert.True(t, hand.SortByRankAscending().Third().Rank == FOUR, "Expected third card to be a four")
|
assert.True(t, s.Second().Rank == THREE, "Expected second card to be a three")
|
||||||
assert.True(t, hand.SortByRankAscending().Fourth().Rank == FIVE, "Expected fourth card to be a five")
|
assert.True(t, s.Third().Rank == FOUR, "Expected third card to be a four")
|
||||||
assert.True(t, hand.SortByRankAscending().Fifth().Rank == ACE, "Expected fifth card to be an ace")
|
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) {
|
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")
|
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) {
|
func TestRoyalFlush(t *testing.T) {
|
||||||
hand := Cards{
|
hand := Cards{
|
||||||
TenOfSpades(),
|
TenOfSpades(),
|
||||||
@ -262,6 +292,17 @@ func TestTwoPair(t *testing.T) {
|
|||||||
assert.False(t, hand.isUnmadeHand(), "Expected hand to not be unmade")
|
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) {
|
func TestHandScore(t *testing.T) {
|
||||||
hand := Cards{
|
hand := Cards{
|
||||||
KingOfSpades(),
|
KingOfSpades(),
|
||||||
|
Loading…
Reference in New Issue
Block a user