From 0bbcbc0c1aa9bacde0aa195f183a338d798477a3 Mon Sep 17 00:00:00 2001 From: Duzzuti Date: Fri, 29 Dec 2023 16:31:09 +0000 Subject: [PATCH] Add heads up rule + perfomance improvement --- docs/data.md | 2 ++ include/data_structs.h | 4 +++- src/game.cpp | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/data.md b/docs/data.md index e552850..2451a8c 100644 --- a/docs/data.md +++ b/docs/data.md @@ -17,6 +17,7 @@ The simulation data is available to all players and contains all information abo ### Game Data The game data contains information about one poker game (until only one player is not out yet). It is stored in the `GameData` struct and has the following form: - number of chips per player (int[]) +- number of non out players (int) - bool array of players who are out (bool[]) ### Round Data @@ -28,6 +29,7 @@ The round data contains information about one round (until the pot is won). It i - small blind position (int) - big blind position (int) - pot (int) +- number of players that are still in the round (int) - bool array of players who folded (bool[]) - community cards (Card[]) - OutEnum which represents the state of the round (OutEnum) diff --git a/include/data_structs.h b/include/data_structs.h index be9aac1..67d3dad 100644 --- a/include/data_structs.h +++ b/include/data_structs.h @@ -67,6 +67,7 @@ struct RoundData { u_int8_t smallBlindPos; // position of the small blind u_int8_t bigBlindPos; // position of the big blind u_int64_t pot; // current pot + u_int8_t numNonFoldedPlayers; // number of players that are still in the round bool playerFolded[MAX_PLAYERS]; // true if player folded Card communityCards[5]; // community cards OutEnum result; // whats the state of the round (continue, round won, game won) @@ -75,6 +76,7 @@ struct RoundData { // contains the data for a single game (until only one player is left) struct GameData { + u_int8_t numNonOutPlayers; // number of players that are still in the game bool playerOut[MAX_PLAYERS]; // true if player is out of the game u_int64_t playerChips[MAX_PLAYERS]; // chips of the players }; @@ -97,7 +99,7 @@ struct Data { do { this->betRoundData.playerPos = (this->betRoundData.playerPos + 1) % this->numPlayers; } while (this->gameData.playerOut[this->betRoundData.playerPos]); - }; + } void selectDealer(const bool firstRound) noexcept { if (firstRound) { diff --git a/src/game.cpp b/src/game.cpp index b526b99..46bb06e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -24,6 +24,7 @@ void Game::run() { // shuffle players PLOG_DEBUG << "Starting game " << game; this->initPlayerOrder(); + this->data.gameData.numNonOutPlayers = this->m_config.numPlayers; std::memset(this->data.gameData.playerOut, 0, sizeof(this->data.gameData.playerOut)); // reset player out for (u_int8_t i = 0; i < this->m_config.numPlayers; i++) this->data.gameData.playerChips[i] = this->m_config.startingChips; int32_t round = -1; @@ -33,6 +34,7 @@ void Game::run() { round++; this->deck = Deck(); this->data.roundData.result = OutEnum::ROUND_CONTINUE; + this->data.roundData.numNonFoldedPlayers = this->data.gameData.numNonOutPlayers; PLOG_DEBUG << "Starting round " << round; this->startRound(round == 0); @@ -101,6 +103,10 @@ void Game::initPlayerOrder() noexcept { OutEnum Game::setBlinds() noexcept { // blinds + if (this->data.gameData.numNonOutPlayers == 2) { + // heads up rule + this->data.betRoundData.playerPos = this->data.roundData.dealerPos; + } OutEnum res = OutEnum::ROUND_CONTINUE; PLOG_DEBUG << this->getPlayerInfo() << " bets small blind " << this->data.roundData.smallBlind; this->data.roundData.smallBlindPos = this->data.betRoundData.playerPos; @@ -298,6 +304,7 @@ bool Game::bet(const u_int64_t amount) noexcept { OutEnum Game::playerOut() noexcept { PLOG_WARNING << this->getPlayerInfo() << " is out"; + this->data.gameData.numNonOutPlayers--; this->data.gameData.playerOut[this->data.betRoundData.playerPos] = true; this->data.nextPlayer(); @@ -305,6 +312,7 @@ OutEnum Game::playerOut() noexcept { } OutEnum Game::playerFolded() noexcept { + this->data.roundData.numNonFoldedPlayers--; this->data.roundData.playerFolded[this->data.betRoundData.playerPos] = true; this->data.nextPlayer(); // if only one player is left, he wins the pot @@ -312,18 +320,10 @@ OutEnum Game::playerFolded() noexcept { } OutEnum Game::getOutEnum() const noexcept { - u_int8_t numActivePlayers = 0; // number of players that are not out and not folded - u_int8_t numNonOutPlayers = 0; // number of players that are not out - for (u_int8_t i = 0; i < this->data.numPlayers; i++) { - if (!this->data.gameData.playerOut[i]) { - numNonOutPlayers++; - if (!this->data.roundData.playerFolded[i]) numActivePlayers++; - } - } - if (numNonOutPlayers == 1) { + if (this->data.gameData.numNonOutPlayers == 1) { // only one player is left in the game, he wins the game return OutEnum::GAME_WON; - } else if (numActivePlayers == 1) { + } else if (this->data.roundData.numNonFoldedPlayers == 1) { // only one player is left in the round, he wins the pot return OutEnum::ROUND_WON; } else {