From 7628417b72103433d2ddb87c916c76ab1eb7536c Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 29 Aug 2021 22:19:54 -0700 Subject: [PATCH] Cleaned some of the betting code. --- src/game/poker/actions/bet.c | 33 ++++++--------------------------- src/poker/bet.c | 20 ++++++++++++++++++++ src/poker/bet.h | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/game/poker/actions/bet.c b/src/game/poker/actions/bet.c index 28e347d8..03c23f61 100644 --- a/src/game/poker/actions/bet.c +++ b/src/game/poker/actions/bet.c @@ -81,42 +81,21 @@ void _pokerGameActionBetOnUpdate( void _pokerGameActionBetOnEnd( queue_t *queue, queueaction_t *action, uint8_t i ) { - uint8_t j, k, p; - pokerplayer_t *player; queueaction_t *next; pokerdiscussiondata_t discussion; - bool playersPending; pokergame_t *game = (pokergame_t *)action->data; - // Go to the next better - game->poker.bet.better = (game->poker.bet.better + 1) % POKER_PLAYER_COUNT; - playersPending = false; - - // Check if each player needs to action. - k = POKER_BET_ROUND_PLAYER_DEFAULT(&game->poker); - - for(j = 0; j < POKER_PLAYER_COUNT; j++) { - p = ((k + j) % POKER_PLAYER_COUNT); - player = game->poker.players + p; - - if((player->state & POKER_PLAYER_STATE_FOLDED) != 0) continue; - if(player->state & POKER_PLAYER_STATE_OUT) continue; - if(player->state & POKER_PLAYER_STATE_ROUND_MOVE) { - if(player->currentBet >= game->poker.bet.currentBet && player) continue; - } - - // Yes, this player needs to check, raise or fold - game->poker.bet.better = p; - pokerGameActionBetAdd(game); - playersPending = true; - break; - } + // Get which player is remaining to move. + game->poker.bet.better = pokerBetGetRemainingPlayer(&game->poker); // Restack pokerGameActionRestackAdd(game); // Are we waiting on any players? - if(playersPending) return; + if(game->poker.bet.better != 0xFF) { + pokerGameActionBetAdd(game); + return; + } // Not waiting, restack and do next action. printf("Not waiting on anything!\n"); diff --git a/src/poker/bet.c b/src/poker/bet.c index 893fece6..14b3f2bf 100644 --- a/src/poker/bet.c +++ b/src/poker/bet.c @@ -34,6 +34,26 @@ void pokerBetResetBetter(poker_t *poker) { poker->bet.better = POKER_BET_ROUND_PLAYER_DEFAULT(poker); } +bool pokerBetPlayerCanBet(pokerbet_t *bet, pokerplayer_t *player) { + if((player->state & POKER_PLAYER_STATE_FOLDED) != 0) return false; + if(player->state & POKER_PLAYER_STATE_OUT) return false; + if(player->state & POKER_PLAYER_STATE_ROUND_MOVE) { + if(player->currentBet >= bet->currentBet && player) return false; + } + return true; +} + +uint8_t pokerBetGetRemainingPlayer(poker_t *poker) { + uint8_t def, j, pi; + def = POKER_BET_ROUND_PLAYER_DEFAULT(poker); + for(j = 0; j < POKER_PLAYER_COUNT; j++) { + pi = ((def + j) % POKER_PLAYER_COUNT); + if(!pokerBetPlayerCanBet(&poker->bet, poker->players + pi)) continue; + return pi; + } + return 0xFF; +} + void pokerBetTakeBlinds(poker_t *poker) { pokerBetPlayer(poker, poker->players + poker->roundSmallBlind, poker->bet.blindSmall diff --git a/src/poker/bet.h b/src/poker/bet.h index 16e605f0..d4d832cb 100644 --- a/src/poker/bet.h +++ b/src/poker/bet.h @@ -37,6 +37,24 @@ void pokerBetPlayer(poker_t *poker, pokerplayer_t *player, int32_t chips); */ void pokerBetResetBetter(poker_t *poker); +/** + * Returns whether or not a player can bet for the current round. + * + * @param bet Game bet state. + * @param player Player to check. + * @return True if the player can bet, otherwise false. + */ +bool pokerBetPlayerCanBet(pokerbet_t *bet, pokerplayer_t *player); + +/** + * Returns the index of the first player that remains to bet for the current + * round. + * + * @param poker Poker instance to fetch from. + * @return The player index of the remaining player, otherwise 0xFF. + */ +uint8_t pokerBetGetRemainingPlayer(poker_t *poker); + /** * Takes the current blinds from the correct players. *