Fixed betting bugs.

This commit is contained in:
2021-09-27 14:27:13 -07:00
parent 733c311d5e
commit d4cc907506
22 changed files with 169 additions and 62 deletions

View File

@ -10,9 +10,7 @@
void _pokerActionBlindsOnStart(queue_t *queue,queueaction_t *action,uint8_t i) {
poker_t *poker;
poker = (poker_t *)action->data;
poker->state = POKER_STATE_TAKING_BLINDS;
pokerBetTakeBlinds(
&poker->bet,
poker->players,

View File

@ -11,9 +11,9 @@ void _pokerActionDealOnStart(queue_t *queue, queueaction_t *action, uint8_t i) {
poker_t *poker;
poker = (poker_t *)action->data;
poker->state = POKER_STATE_DEALING;
// Shuffle the deck
poker->state = POKER_STATE_DEALING;
cardShuffle(poker->dealer.deck, CARD_DECK_SIZE);
// Deal 2 card to each player

View File

@ -30,13 +30,14 @@ void _pokerActionRoundOnStart(queue_t *queue, queueaction_t *action ,uint8_t i){
// Decide on the dealer
poker->roundDealer = (poker->roundDealer+1) % POKER_PLAYER_COUNT;
// Find the players.
// Find (and kill) the players.
j = poker->roundDealer;
foundDealer = false;
foundSmallBlind = false;
while(true) {
player = poker->players + j;
if(!pokerPlayerIsAlive(player)) continue;
if(!pokerPlayerIsInRound(player)) continue;
if(!foundDealer) {
indexDealer = j;
foundDealer = true;

View File

@ -42,10 +42,17 @@ void pokerBetResetBetter(
}
bool pokerBetPlayerCanBet(pokerbet_t *bet, pokerplayer_t *player) {
if(!pokerPlayerIsAlive(player)) return false;
// Is the player out?
if(!pokerPlayerIsInRound(player)) return false;
// Is the player broke?
if(player->chips <= 0) return false;
// Does the player still need to act yet?
if(player->state & POKER_PLAYER_STATE_ROUND_MOVE) {
if(player->currentBet >= bet->currentBet && player) return false;
if(player->currentBet >= bet->currentBet) return false;
}
return true;
}
@ -66,7 +73,7 @@ uint8_t pokerBetGetRemainingPlayerCount(pokerbet_t *bet,pokerplayer_t *players){
uint8_t i, c;
c = 0;
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
if(!pokerPlayerIsAlive(players + i)) continue;
if(!pokerBetPlayerCanBet(bet, players + i)) continue;
c++;
}
return c;

View File

@ -7,6 +7,7 @@
#include "../libs.h"
#include "player.h"
#include "../util/math.h"
#include "../util/array.h"
/** How many chips each player has by defautl */
#define POKER_BET_PLAYER_CHIPS_DEFAULT 10000
@ -41,7 +42,7 @@ void pokerBetInit(pokerbet_t *bet);
/**
* Resets the bet state (for a new round).
*
* @param bet
* @param bet Betting stating.
*/
void pokerBetReset(pokerbet_t *bet);
@ -97,8 +98,7 @@ uint8_t pokerBetGetRemainingPlayer(
);
/**
* Returns the count of players who are still available to bet. This does not
* consider any round logic, simply whether or not they COULD bet.
* Returns the count of players who are still needing to bet.
*
* @param bet Betting instance
* @param players Players array.

View File

@ -48,7 +48,7 @@ void pokerDealerDealAll(
for(y = 0; y < count; y++) {
for(x = 0; x < POKER_PLAYER_COUNT; x++) {
player = players + x;
if(!pokerPlayerIsAlive(player)) continue;
if(!pokerPlayerIsInRound(player)) continue;
pokerDealerDeal(dealer, player);
}
}

View File

@ -7,8 +7,19 @@
#include "player.h"
bool pokerPlayerIsAlive(pokerplayer_t *player) {
return !(player->state & (POKER_PLAYER_STATE_FOLDED|POKER_PLAYER_STATE_OUT));
bool pokerPlayerIsInRound(pokerplayer_t *player) {
return !(
player->state & (POKER_PLAYER_STATE_FOLDED|POKER_PLAYER_STATE_OUT)
);
}
uint8_t pokerPlayerGetCountInRound(pokerplayer_t *players) {
return (uint8_t)arraySum(
sizeof(pokerplayer_t),
players,
POKER_PLAYER_COUNT,
(arraysumcallback_t *)(&pokerPlayerIsInRound)
);
}
void pokerPlayerReset(pokerplayer_t *player) {

View File

@ -8,6 +8,7 @@
#pragma once
#include "../libs.h"
#include "../util/flags.h"
#include "../util/array.h"
#include "card.h"
/** How many cards a player can hold in their hand */
@ -59,13 +60,24 @@ typedef struct {
} pokerplayer_t;
/**
* Returns true if the player is still alive and in the current game/
* Defined as: Not out, not folded.
* Returns whether or not the player is in the current round. This does not
* consider anything about whether they can/have bet or not, simply whether or
* not they can even partake in the round.
*
* @param player Player to check
* @returns True if alive/in teh current game.
* @param player
* @return true
* @return false
*/
bool pokerPlayerIsAlive(pokerplayer_t *player);
bool pokerPlayerIsInRound(pokerplayer_t *player);
/**
* Returns the count of players who remain in the current round. This is a sum
* of pokerPlayerIsInRound.
*
* @param players
* @return uint8_t
*/
uint8_t pokerPlayerGetCountInRound(pokerplayer_t *players);
/**
* Resets a poker player's state (for a new round).

View File

@ -6,3 +6,32 @@
*/
#include "poker.h"
void pokerGameWin(poker_t *poker) {
uint8_t i;
pokerplayerwinning_t *winning;
pokerplayer_t *player;
int32_t chipsRounded, chipsDifferent;
float chipsEach;
// Get the chips each.
chipsEach = poker->bet.pot / ((float)poker->winner.winnerCount);
// Now work out the rounded offset chips.
chipsRounded = (int32_t)mathFloor(chipsEach);
chipsDifferent = poker->bet.pot - chipsRounded;
// Let's start by rewarding the winnings to the players.
for(i = 0; i < poker->winner.winnerCount; i++) {
player = poker->players + poker->winner.winners[i];
winning = poker->winner.winnings + i;
player->chips += chipsRounded;
if(i == 0) player->chips += chipsDifferent;
}
// Now kill the players who are terrible low life gamblers.
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
player = poker->players + i;
if(player->chips <= 0) player->state &= POKER_PLAYER_STATE_OUT;
}
}

View File

@ -50,4 +50,6 @@ typedef struct {
uint8_t roundBigBlind;
uint8_t state;
} poker_t;
} poker_t;
void pokerGameWin(poker_t *poker);

View File

@ -336,7 +336,7 @@ void pokerWinnerCalculate(
left = winner->winnings + i;
left->type = POKER_WINNING_TYPE_NULL;
player = players + i;
if(!pokerPlayerIsAlive(player)) continue;
if(!pokerPlayerIsInRound(player)) continue;
// Get the players' winning state.
pokerWinnerPlayerGet(dealer, player, left);