From a3a891ddb2338dcb0c2ad59e92082b236af6a90c Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 10 Sep 2024 06:51:04 -0500 Subject: [PATCH] Cleanup of poker code done, about to start testing. --- src/dawnpoker/poker/PokerPlayer.cpp | 16 ++++++----- src/dawnpoker/poker/PokerPot.cpp | 2 +- src/dawnpoker/poker/PokerTurn.cpp | 43 ++++++++++++++++------------- src/dawnpoker/poker/PokerTurn.hpp | 22 ++++++++------- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/dawnpoker/poker/PokerPlayer.cpp b/src/dawnpoker/poker/PokerPlayer.cpp index 2ac34684..993bd1ca 100644 --- a/src/dawnpoker/poker/PokerPlayer.cpp +++ b/src/dawnpoker/poker/PokerPlayer.cpp @@ -60,8 +60,8 @@ void PokerPlayer::bet( pot.chips += chips; pot.call = Math::max(pot.call, this ->currentBet); - auto existing = std::find(pot.players.begin(), pot.players.end(), this); - if(existing == pot.players.end()) pot.players.push_back(this); + auto existing = std::find(pot.players.begin(), pot.players.end(), shared_from_this()); + if(existing == pot.players.end()) pot.players.push_back(shared_from_this()); } void PokerPlayer::bet(const int32_t chips) { @@ -98,7 +98,7 @@ struct PokerTurn PokerPlayer::getAITurn() { // Can the player do anything? if(this->isFolded || this->isOut) { - turn.type = POKER_TURN_TYPE_OUT; + turn.type = PokerTurnType::Out; return turn; } @@ -241,13 +241,13 @@ struct PokerTurn PokerPlayer::getAITurn() { } amount = Math::max(amount, callBet); - turn = PokerTurn::bet(this, amount); + turn = PokerTurn::bet(shared_from_this(), amount); turn.confidence = confidence; } else if(this->canCheck()) { - turn = PokerTurn::bet(this, 0); + turn = PokerTurn::bet(shared_from_this(), 0); turn.confidence = 1; } else { - turn = PokerTurn::fold(this); + turn = PokerTurn::fold(shared_from_this()); turn.confidence = 1 - confidence; } @@ -266,7 +266,9 @@ int32_t PokerPlayer::getSumOfChips() { int32_t count = 0; auto it = pg->pots.begin(); while(it != pg->pots.end()) { - if(std::find(it->players.begin(), it->players.end(), this) != it->players.end()) { + if(std::find( + it->players.begin(), it->players.end(), shared_from_this() + ) != it->players.end()) { count += it->chips; } ++it; diff --git a/src/dawnpoker/poker/PokerPot.cpp b/src/dawnpoker/poker/PokerPot.cpp index 3fa1a94f..4f1ad30b 100644 --- a/src/dawnpoker/poker/PokerPot.cpp +++ b/src/dawnpoker/poker/PokerPot.cpp @@ -73,7 +73,7 @@ struct PokerPotWinning PokerPot::getWinners() { } // Equal, compare hands. - card = PokerWinning::compare(winnerLeft, winnerRight); + card = PokerWinning::compare(*winnerLeft, *winnerRight); if(card.cardValue == 0xFF) { isWinner = false; break; diff --git a/src/dawnpoker/poker/PokerTurn.cpp b/src/dawnpoker/poker/PokerTurn.cpp index 4598aa6e..e9425cc7 100644 --- a/src/dawnpoker/poker/PokerTurn.cpp +++ b/src/dawnpoker/poker/PokerTurn.cpp @@ -9,64 +9,69 @@ using namespace Dawn; -struct PokerTurn PokerTurn::bet(PokerPlayer *player, int32_t chips) { +struct PokerTurn PokerTurn::bet( + std::shared_ptr player, + const int32_t chips +) { struct PokerTurn turn; int32_t i; - assertNotNull(player); - assertTrue(chips >= 0); + assertNotNull(player, "Player cannot be null."); + assertTrue(chips >= 0, "Chips must be greater than or equal to 0."); turn.player = player; turn.confidence = 1; if(chips == 0) { - turn.type = POKER_TURN_TYPE_CHECK; + turn.type = PokerTurnType::Check; turn.chips = 0; } else if(player->chips <= chips) { turn.chips = player->chips; - turn.type = POKER_TURN_TYPE_ALL_IN; + turn.type = PokerTurnType::AllIn; } else { turn.chips = chips; - turn.type = POKER_TURN_TYPE_BET; - i = player->pokerGame->getCurrentCallValue(); + turn.type = PokerTurnType::Bet; - if(chips == (i - player->currentBet)) { - turn.type = POKER_TURN_TYPE_CALL; - } + auto pg = player->pokerGame.lock(); + assertNotNull(pg, "Player must be in a game."); + + i = pg->getCurrentCallValue(); + + if(chips == (i - player->currentBet)) turn.type = PokerTurnType::Call; } return turn; } -struct PokerTurn PokerTurn::fold(PokerPlayer *player) { +struct PokerTurn PokerTurn::fold(std::shared_ptr player) { struct PokerTurn turn; turn.player = player; turn.chips = 0; turn.confidence = 1; - turn.type = POKER_TURN_TYPE_FOLD; + turn.type = PokerTurnType::Fold; return turn; } void PokerTurn::action() { - assertNotNull(this->player); + assertNotNull(this->player, "Player cannot be null."); switch(this->type) { - case POKER_TURN_TYPE_BET: - case POKER_TURN_TYPE_CALL: - case POKER_TURN_TYPE_ALL_IN: + case PokerTurnType::Bet: + case PokerTurnType::Call: + case PokerTurnType::AllIn: this->player->bet(this->chips); break; - case POKER_TURN_TYPE_CHECK: + case PokerTurnType::Check: player->bet(0); break; - case POKER_TURN_TYPE_FOLD: + case PokerTurnType::Fold: player->fold(); break; default: - assertUnreachable(); + assertUnreachable("Unknown turn type."); break; } } \ No newline at end of file diff --git a/src/dawnpoker/poker/PokerTurn.hpp b/src/dawnpoker/poker/PokerTurn.hpp index 42e9aa95..179c7a8c 100644 --- a/src/dawnpoker/poker/PokerTurn.hpp +++ b/src/dawnpoker/poker/PokerTurn.hpp @@ -9,13 +9,13 @@ namespace Dawn { class PokerPlayer; - enum PokerTurnType { - POKER_TURN_TYPE_OUT, - POKER_TURN_TYPE_FOLD, - POKER_TURN_TYPE_BET, - POKER_TURN_TYPE_CALL, - POKER_TURN_TYPE_CHECK, - POKER_TURN_TYPE_ALL_IN + enum class PokerTurnType { + Out, + Fold, + Bet, + Call, + Check, + AllIn }; struct PokerTurn { @@ -27,7 +27,7 @@ namespace Dawn { /** How confident the AI is about their turn. 0 = none, 1 = full */ float_t confidence; /** Player that this action belongs to */ - PokerPlayer *player; + std::shared_ptr player; /** * Generate a turn action for betting as a player. @@ -36,14 +36,16 @@ namespace Dawn { * @param chips Chips to raise by. * @return A turn for a bet action. */ - static struct PokerTurn bet(PokerPlayer *player, int32_t chips); + static struct PokerTurn bet( + std::shared_ptr player, const int32_t chips + ); /** * Return a turn action for the given player to fold. * * @return A turn for a fold action. */ - static struct PokerTurn fold(PokerPlayer *player); + static struct PokerTurn fold(std::shared_ptr player); /** * Actions / Performs this turn against the defined player.