added more tests, cleaned up some code, found another bug

This commit is contained in:
2024-05-18 22:10:53 -07:00
parent 5f7dba942c
commit 14ffbe4eb4
5 changed files with 250 additions and 132 deletions

View File

@@ -7,74 +7,148 @@ import (
"git.eeqj.de/sneak/pokercore"
)
// just like on tv
var delayTime = 2 * time.Second
var playerCount = 8
type Player struct {
Hand pokercore.Cards
ScoredHand *pokercore.PokerHand
Position int
CommunityCardsAvailable pokercore.Cards
}
type Game struct {
Deck *pokercore.Deck
Players []*Player
Community pokercore.Cards
Street int
}
func NewGame(seed int) *Game {
g := &Game{}
g.Street = 0
g.Deck = pokercore.NewDeck()
g.SpreadCards()
fmt.Printf("Shuffle up and deal!\n")
time.Sleep(delayTime)
fmt.Printf("Shuffling...\n")
g.Deck.ShuffleDeterministically(3141592653)
return g
}
func (g *Game) StreetAsString() string {
switch g.Street {
case 0:
return "pre-flop"
case 1:
return "flop"
case 2:
return "turn"
case 3:
return "river"
}
return "unknown"
}
func (g *Game) SpreadCards() {
fmt.Printf("deck before shuffling: %s\n", g.Deck.FormatForTerminal())
}
func (g *Game) DealPlayersIn() {
for i := 0; i < playerCount; i++ {
p := Player{Hand: g.Deck.Deal(2), Position: i + 1}
g.Players = append(g.Players, &p)
}
}
func (g *Game) ShowGameStatus() {
fmt.Printf("Street: %s\n", g.StreetAsString())
fmt.Printf("Community cards: %s\n", g.Community.FormatForTerminal())
for _, p := range g.Players {
if p != nil {
fmt.Printf("Player %d: %s\n", p.Position, p.Hand.FormatForTerminal())
if g.Street > 0 {
ac := append(p.Hand, g.Community...)
ph, err := ac.PokerHand()
if err != nil {
panic(err)
}
fmt.Printf("Player %d has %s\n", p.Position, ph.Description())
fmt.Printf("Player %d Score: %d\n", p.Position, ph.Score)
}
}
}
}
func (g *Game) DealFlop() {
g.Community = g.Deck.Deal(3)
g.Street = 1
}
func (g *Game) DealTurn() {
g.Community = append(g.Community, g.Deck.Deal(1)...)
g.Street = 2
}
func (g *Game) DealRiver() {
g.Community = append(g.Community, g.Deck.Deal(1)...)
g.Street = 3
}
func (g *Game) ShowWinner() {
var winner *Player
var winningHand *pokercore.PokerHand
for _, p := range g.Players {
if p != nil {
ac := append(p.Hand, g.Community...)
ph, err := ac.PokerHand()
if err != nil {
panic(err)
}
if winner == nil {
winner = p
winningHand = ph
} else {
if ph.Score > winningHand.Score {
winner = p
winningHand = ph
}
}
}
}
fmt.Printf("Winner: Player %d with %s.\n", winner.Position, winningHand.Description())
}
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)
//g := NewGame(1337))
fmt.Printf("##############################################\n")
// deal the flop
var community pokercore.Cards
community = d.Deal(3)
fmt.Printf("flop: %s\n", community.FormatForTerminal())
time.Sleep(sleepTime)
// nothing up my sleeve:
g := NewGame(3141592653)
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())
g.ShowGameStatus()
time.Sleep(sleepTime)
g.DealPlayersIn()
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())
g.ShowGameStatus()
g.DealFlop()
g.ShowGameStatus()
g.DealTurn()
g.ShowGameStatus()
g.DealRiver()
g.ShowGameStatus()
g.ShowWinner()
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")
}