Cleanup of poker code done, about to start testing.

This commit is contained in:
2024-09-10 06:51:04 -05:00
parent 5fae94c722
commit a3a891ddb2
4 changed files with 46 additions and 37 deletions

View File

@ -60,8 +60,8 @@ void PokerPlayer::bet(
pot.chips += chips; pot.chips += chips;
pot.call = Math::max<int32_t>(pot.call, this ->currentBet); pot.call = Math::max<int32_t>(pot.call, this ->currentBet);
auto existing = std::find(pot.players.begin(), pot.players.end(), this); auto existing = std::find(pot.players.begin(), pot.players.end(), shared_from_this());
if(existing == pot.players.end()) pot.players.push_back(this); if(existing == pot.players.end()) pot.players.push_back(shared_from_this());
} }
void PokerPlayer::bet(const int32_t chips) { void PokerPlayer::bet(const int32_t chips) {
@ -98,7 +98,7 @@ struct PokerTurn PokerPlayer::getAITurn() {
// Can the player do anything? // Can the player do anything?
if(this->isFolded || this->isOut) { if(this->isFolded || this->isOut) {
turn.type = POKER_TURN_TYPE_OUT; turn.type = PokerTurnType::Out;
return turn; return turn;
} }
@ -241,13 +241,13 @@ struct PokerTurn PokerPlayer::getAITurn() {
} }
amount = Math::max<int32_t>(amount, callBet); amount = Math::max<int32_t>(amount, callBet);
turn = PokerTurn::bet(this, amount); turn = PokerTurn::bet(shared_from_this(), amount);
turn.confidence = confidence; turn.confidence = confidence;
} else if(this->canCheck()) { } else if(this->canCheck()) {
turn = PokerTurn::bet(this, 0); turn = PokerTurn::bet(shared_from_this(), 0);
turn.confidence = 1; turn.confidence = 1;
} else { } else {
turn = PokerTurn::fold(this); turn = PokerTurn::fold(shared_from_this());
turn.confidence = 1 - confidence; turn.confidence = 1 - confidence;
} }
@ -266,7 +266,9 @@ int32_t PokerPlayer::getSumOfChips() {
int32_t count = 0; int32_t count = 0;
auto it = pg->pots.begin(); auto it = pg->pots.begin();
while(it != pg->pots.end()) { 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; count += it->chips;
} }
++it; ++it;

View File

@ -73,7 +73,7 @@ struct PokerPotWinning PokerPot::getWinners() {
} }
// Equal, compare hands. // Equal, compare hands.
card = PokerWinning::compare(winnerLeft, winnerRight); card = PokerWinning::compare(*winnerLeft, *winnerRight);
if(card.cardValue == 0xFF) { if(card.cardValue == 0xFF) {
isWinner = false; isWinner = false;
break; break;

View File

@ -9,64 +9,69 @@
using namespace Dawn; using namespace Dawn;
struct PokerTurn PokerTurn::bet(PokerPlayer *player, int32_t chips) { struct PokerTurn PokerTurn::bet(
std::shared_ptr<PokerPlayer> player,
const int32_t chips
) {
struct PokerTurn turn; struct PokerTurn turn;
int32_t i; int32_t i;
assertNotNull(player); assertNotNull(player, "Player cannot be null.");
assertTrue(chips >= 0); assertTrue(chips >= 0, "Chips must be greater than or equal to 0.");
turn.player = player; turn.player = player;
turn.confidence = 1; turn.confidence = 1;
if(chips == 0) { if(chips == 0) {
turn.type = POKER_TURN_TYPE_CHECK; turn.type = PokerTurnType::Check;
turn.chips = 0; turn.chips = 0;
} else if(player->chips <= chips) { } else if(player->chips <= chips) {
turn.chips = player->chips; turn.chips = player->chips;
turn.type = POKER_TURN_TYPE_ALL_IN; turn.type = PokerTurnType::AllIn;
} else { } else {
turn.chips = chips; turn.chips = chips;
turn.type = POKER_TURN_TYPE_BET; turn.type = PokerTurnType::Bet;
i = player->pokerGame->getCurrentCallValue();
if(chips == (i - player->currentBet)) { auto pg = player->pokerGame.lock();
turn.type = POKER_TURN_TYPE_CALL; assertNotNull(pg, "Player must be in a game.");
}
i = pg->getCurrentCallValue();
if(chips == (i - player->currentBet)) turn.type = PokerTurnType::Call;
} }
return turn; return turn;
} }
struct PokerTurn PokerTurn::fold(PokerPlayer *player) { struct PokerTurn PokerTurn::fold(std::shared_ptr<PokerPlayer> player) {
struct PokerTurn turn; struct PokerTurn turn;
turn.player = player; turn.player = player;
turn.chips = 0; turn.chips = 0;
turn.confidence = 1; turn.confidence = 1;
turn.type = POKER_TURN_TYPE_FOLD; turn.type = PokerTurnType::Fold;
return turn; return turn;
} }
void PokerTurn::action() { void PokerTurn::action() {
assertNotNull(this->player); assertNotNull(this->player, "Player cannot be null.");
switch(this->type) { switch(this->type) {
case POKER_TURN_TYPE_BET: case PokerTurnType::Bet:
case POKER_TURN_TYPE_CALL: case PokerTurnType::Call:
case POKER_TURN_TYPE_ALL_IN: case PokerTurnType::AllIn:
this->player->bet(this->chips); this->player->bet(this->chips);
break; break;
case POKER_TURN_TYPE_CHECK: case PokerTurnType::Check:
player->bet(0); player->bet(0);
break; break;
case POKER_TURN_TYPE_FOLD: case PokerTurnType::Fold:
player->fold(); player->fold();
break; break;
default: default:
assertUnreachable(); assertUnreachable("Unknown turn type.");
break; break;
} }
} }

View File

@ -9,13 +9,13 @@
namespace Dawn { namespace Dawn {
class PokerPlayer; class PokerPlayer;
enum PokerTurnType { enum class PokerTurnType {
POKER_TURN_TYPE_OUT, Out,
POKER_TURN_TYPE_FOLD, Fold,
POKER_TURN_TYPE_BET, Bet,
POKER_TURN_TYPE_CALL, Call,
POKER_TURN_TYPE_CHECK, Check,
POKER_TURN_TYPE_ALL_IN AllIn
}; };
struct PokerTurn { struct PokerTurn {
@ -27,7 +27,7 @@ namespace Dawn {
/** How confident the AI is about their turn. 0 = none, 1 = full */ /** How confident the AI is about their turn. 0 = none, 1 = full */
float_t confidence; float_t confidence;
/** Player that this action belongs to */ /** Player that this action belongs to */
PokerPlayer *player; std::shared_ptr<PokerPlayer> player;
/** /**
* Generate a turn action for betting as a player. * Generate a turn action for betting as a player.
@ -36,14 +36,16 @@ namespace Dawn {
* @param chips Chips to raise by. * @param chips Chips to raise by.
* @return A turn for a bet action. * @return A turn for a bet action.
*/ */
static struct PokerTurn bet(PokerPlayer *player, int32_t chips); static struct PokerTurn bet(
std::shared_ptr<PokerPlayer> player, const int32_t chips
);
/** /**
* Return a turn action for the given player to fold. * Return a turn action for the given player to fold.
* *
* @return A turn for a fold action. * @return A turn for a fold action.
*/ */
static struct PokerTurn fold(PokerPlayer *player); static struct PokerTurn fold(std::shared_ptr<PokerPlayer> player);
/** /**
* Actions / Performs this turn against the defined player. * Actions / Performs this turn against the defined player.