Added some turn logic

This commit is contained in:
2021-09-20 13:21:46 -07:00
parent 9505aff1a4
commit a21fbdb881
3 changed files with 88 additions and 31 deletions

View File

@ -66,7 +66,6 @@ void pokerUiUpdate(
j = 0;
shaderUse(shader);
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
if(i == POKER_PLAYER_HUMAN_INDEX) continue;
player = players + j;
// Locate the XYZ position of the camera to look at the player
@ -121,7 +120,7 @@ void pokerUiRender(
// Betting UI
if(ui->betShow) {
sprintf(message, "Press down to fold, up to bet, right to check/call.");
sprintf(message, "Press down to fold, up to raise, right to check/call.");
labelSetText(&ui->label, &assets->font, message);
labelRender(&ui->label, &assets->shader, 300, 100);
@ -129,7 +128,12 @@ void pokerUiRender(
ui->betTurn = pokerTurnFold(poker, poker->bet.better);
ui->betTurnMade = true;
} else if(inputIsPressed(&engine->input, INPUT_RIGHT)) {
ui->betTurn = pokerTurnCall(poker, poker->bet.better);
if(pokerTurnCanPlayerCheck(poker, poker->bet.better)) {
ui->betTurn = pokerTurnCheck(poker, poker->bet.better);
} else {
ui->betTurn = pokerTurnCall(poker, poker->bet.better);
}
ui->betTurnMade = true;
}
if(ui->betTurnMade) ui->betShow = false;
@ -138,7 +142,6 @@ void pokerUiRender(
// Player UI
j = 0;
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
if(i == POKER_PLAYER_HUMAN_INDEX) continue;
// Get the player.
player = poker->players + i;

View File

@ -7,29 +7,20 @@
#include "turn.h"
pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex) {
pokerturn_t turn;
pokerplayer_t *player;
player = poker->players + playerIndex;
// Can the player do anything?
turn.type = POKER_TURN_TYPE_OUT;
if(player->state & POKER_PLAYER_STATE_FOLDED) return turn;
if(player->state & POKER_PLAYER_STATE_OUT) return turn;
if(pokerTurnCanPlayerCheck(poker, playerIndex)) {
turn.type = POKER_TURN_TYPE_CHECK;
return turn;
if(player->state & (POKER_PLAYER_STATE_FOLDED | POKER_PLAYER_STATE_OUT)) {
return pokerTurnOut(poker, playerIndex);
}
// I have nfi
turn.type = POKER_TURN_TYPE_BET;
turn.chips = poker->bet.currentBet - player->currentBet;
turn.confidence = 1;
if(pokerTurnCanPlayerCheck(poker, playerIndex)) {
return pokerTurnCheck(poker, playerIndex);
}
return turn;
return pokerTurnCall(poker, playerIndex);
}
void pokerTurnAction(poker_t *poker, pokerplayer_t *player, pokerturn_t *turn) {
@ -37,10 +28,11 @@ void pokerTurnAction(poker_t *poker, pokerplayer_t *player, pokerturn_t *turn) {
case POKER_TURN_TYPE_BET:
case POKER_TURN_TYPE_CALL:
case POKER_TURN_TYPE_ALL_IN:
pokerBetPlayer(poker, player, turn->chips);
pokerBetPlayer(&poker->bet, player, turn->chips);
break;
case POKER_TURN_TYPE_CHECK:
pokerBetPlayer(&poker->bet, player, 0);
break;
case POKER_TURN_TYPE_FOLD:
@ -51,27 +43,51 @@ void pokerTurnAction(poker_t *poker, pokerplayer_t *player, pokerturn_t *turn) {
player->state |= POKER_PLAYER_STATE_ROUND_MOVE;
}
pokerturn_t pokerTurnOut(poker_t *poker, uint8_t player) {
pokerturn_t turn;
turn.type = POKER_TURN_TYPE_OUT;
return turn;
}
pokerturn_t pokerTurnFold(poker_t *poker, uint8_t player) {
pokerturn_t turn;
turn.type = POKER_TURN_TYPE_FOLD;
return turn;
}
void pokerTurnCheck(poker_t *poker, uint8_t player) {
pokerturn_t pokerTurnCheck(poker_t *poker, uint8_t player) {
pokerturn_t turn;
turn.type = POKER_TURN_TYPE_CHECK;
turn.chips = 0;
return turn;
}
void pokerTurnCall(poker_t *poker, uint8_t playerIndex) {
pokerturn_t pokerTurnCall(poker_t *poker, uint8_t playerIndex) {
pokerturn_t turn;
pokerplayer_t *player;
bool canCheck;
player = poker->players + playerIndex;
turn.type = POKER_TURN_TYPE_CHECK;
turn.chips = poker->bet.currentBet - player->chips;
turn = pokerTurnRaise(
poker, playerIndex, poker->bet.currentBet - player->currentBet
);
turn.type = (
turn.type == POKER_TURN_TYPE_ALL_IN ?
POKER_TURN_TYPE_CALL_ALL_IN :
POKER_TURN_TYPE_CALL
);
return turn;
}
pokerturn_t pokerTurnRaise(poker_t *poker, uint8_t playerIndex, int32_t chips) {
pokerturn_t turn;
pokerplayer_t *player;
player = poker->players + playerIndex;
if(player->chips <= chips) {
turn.chips = player->chips;
turn.type = POKER_TURN_TYPE_ALL_IN;
} else {
turn.chips = chips;
turn.type = POKER_TURN_TYPE_BET;
}
return turn;
}

View File

@ -14,6 +14,7 @@
#define POKER_TURN_TYPE_BET 0x02
#define POKER_TURN_TYPE_CALL 0x03
#define POKER_TURN_TYPE_ALL_IN 0x04
#define POKER_TURN_TYPE_CALL_ALL_IN 0x04
#define POKER_TURN_TYPE_CHECK 0x05
/** The turn that a player/the AI decided to do for its turn */
@ -35,6 +36,24 @@ typedef struct {
*/
pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex);
/**
* Perform the turn's action.
*
* @param poker Poker game instance.
* @param player Player instance.
* @param turn Turn to action.
*/
void pokerTurnAction(poker_t *poker, pokerplayer_t *player, pokerturn_t *turn);
/**
* Return a turn action for the player being out.
*
* @param poker Poker game instance.
* @param player Player index.
* @return A turn for an out action.
*/
pokerturn_t pokerTurnOut(poker_t *poker, uint8_t player);
/**
* Return a turn action for the given player to fold.
*
@ -45,13 +64,32 @@ pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex);
pokerturn_t pokerTurnFold(poker_t *poker, uint8_t player);
/**
* Perform the turn's action.
* Perform a check as a player.
*
* @param poker Poker game instance.
* @param player Player instance.
* @param turn Turn to action.
* @param player Player index for who is checking.
* @return A turn for a check action.
*/
void pokerTurnAction(poker_t *poker, pokerplayer_t *player, pokerturn_t *turn);
pokerturn_t pokerTurnCheck(poker_t *poker, uint8_t player);
/**
* Perform a call as a player
*
* @param poker Poker game instance.
* @param player Player index who is calling.
* @return A turn for a call action.
*/
pokerturn_t pokerTurnCall(poker_t *poker, uint8_t player);
/**
* Perform a bet as a player.
*
* @param poker Poker game instance.
* @param playerIndex Player index who is betting.
* @param chips Chips to raise by.
* @return A turn for a bet action.
*/
pokerturn_t pokerTurnRaise(poker_t *poker, uint8_t playerIndex, int32_t chips);
/**
* Determine whether or not a player CAN check, given the current max bet and