Skip to content

Commit

Permalink
Merge pull request #16 from Duzzuti/15-add-head-to-head-rules
Browse files Browse the repository at this point in the history
Add heads up rule + perfomance improvement
  • Loading branch information
Duzzuti authored Dec 29, 2023
2 parents 5aaade7 + 0bbcbc0 commit d1ce6cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion include/data_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
};
Expand All @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -298,32 +304,26 @@ 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();

return this->getOutEnum();
}

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
return this->getOutEnum();
}

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 {
Expand Down

0 comments on commit d1ce6cb

Please sign in to comment.