Added basic betting, turns, and added queue restacking

This commit is contained in:
2021-08-13 09:32:15 -07:00
parent 82fe9a7e3c
commit eddc5bfafd
14 changed files with 217 additions and 8 deletions

View File

@ -52,6 +52,7 @@
#include "locale/language.h" #include "locale/language.h"
// Poker Game Logic // Poker Game Logic
#include "poker/turn.h"
#include "poker/bet.h" #include "poker/bet.h"
#include "poker/card.h" #include "poker/card.h"
#include "poker/dealer.h" #include "poker/dealer.h"

View File

@ -19,11 +19,14 @@
typedef struct { typedef struct {
/** Blinds */ /** Blinds */
uint32_t blindSmall, blindBig; int32_t blindSmall, blindBig;
/** How big the current bet is for the round. */
int32_t currentBet;
/** For Betting round, which player is currently betting */ /** For Betting round, which player is currently betting */
uint8_t better; uint8_t better;
/** Current pot of chips */ /** Current pot of chips */
uint32_t pot; int32_t pot;
} pokerbet_t; } pokerbet_t;

View File

@ -48,8 +48,8 @@ typedef struct {
uint8_t state; uint8_t state;
/** Chips in players' posession */ /** Chips in players' posession */
uint32_t chips; int32_t chips;
/** Current bet in current round player has placed */ /** Current bet in current round player has placed */
uint32_t currentBet; int32_t currentBet;
} pokerplayer_t; } pokerplayer_t;

19
include/dawn/poker/turn.h Normal file
View File

@ -0,0 +1,19 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../libs.h"
#define POKER_TURN_TYPE_OUT 0x00
#define POKER_TURN_TYPE_FOLD 0x01
#define POKER_TURN_TYPE_BET 0x02
typedef struct {
uint8_t type;
int32_t chips;
float confidence;
} pokerturn_t;

View File

@ -65,6 +65,30 @@ void queueDispose(queue_t *queue) {
if(action->onEnd != NULL) action->onEnd(queue, action, queue->current); if(action->onEnd != NULL) action->onEnd(queue, action, queue->current);
} }
void queueRestack(queue_t *queue) {
queueaction_t *action;
uint8_t i;
queueaction_t items[ANIMATION_QUEUE_ITEM_MAX];
// Take the current queue and copy it.
arrayCopy(sizeof(queueaction_t),
queue->items+queue->current, queue->count - queue->current,
items
);
// Now rewind the stack
queue->count -= queue->current;
queue->current = 0;
// Now copy back
arrayCopy(sizeof(queueaction_t), items, queue->count, queue->items);
// Now fix indexes
for(i = 0; i < queue->count; i++) {
queue->items[i].index = i;
}
}
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i) { void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i) {
float n = queue->timeline - queue->actionStarted; float n = queue->timeline - queue->actionStarted;

View File

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <dawn/dawn.h> #include <dawn/dawn.h>
#include "../../util/array.h"
/** /**
* Initialize the queue set. * Initialize the queue set.
@ -41,10 +42,17 @@ void queueUpdate(queue_t *queue, engine_t *engine);
*/ */
void queueDispose(queue_t *queue); void queueDispose(queue_t *queue);
/**
* Restacks the queue. The restack process essentially rewinds the queue so that
* the current items move back to position 0, and allows you to add more items
* to the queue again.
*
* @param queue Queue to restack.
*/
void queueRestack(queue_t *queue);
/** Callbacks for Queue Delay Action */ /** Callbacks for Queue Delay Action */
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i); void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i);
/** /**
* Adds a delay action to a queue. * Adds a delay action to a queue.
* @param queue Queue to add to. * @param queue Queue to add to.

View File

@ -7,8 +7,102 @@
#include "bet.h" #include "bet.h"
queueaction_t * pokerGameActionBetAdd(pokergame_t *game) { void _pokerGameActionBetOnStart(
queue_t *queue, queueaction_t *action, uint8_t i
) {
}
void _pokerGameActionBetOnUpdate(
queue_t *queue, queueaction_t *action, uint8_t i
) {
bool isHuman;
bool turnMade = false;
pokerturn_t turn;
pokergame_t *game = (pokergame_t *)action->data;
pokerplayer_t *player;
// As of right now the queue should basically be empty besides this item, so
// let's restack
queueRestack(queue);
// Are they human?
player = game->poker.players + game->poker.bet.better;
isHuman = game->poker.bet.better == POKER_PLAYER_HUMAN_INDEX;
// Handle as an AI
if(isHuman) {
turn.type = POKER_TURN_TYPE_OUT;
turnMade = true;
} else {
turn = pokerTurnGet(&game->poker, game->poker.bet.better);
turnMade = true;
}
// Now decide if we should do something.
if(!turnMade) return;
// What happened?
char *debugAction;
switch(turn.type) {
// Player bets
case POKER_TURN_TYPE_BET:
debugAction = "betting";
pokerBetPlayer(&game->poker, player, turn.chips);
break;
// Player folds
case POKER_TURN_TYPE_FOLD:
debugAction = "folding";
player->state |= POKER_PLAYER_STATE_FOLDED;
break;
// Player may be out
default:
debugAction = "doing nothing";
break;
}
printf("Player %i is %s.\n", game->poker.bet.better, debugAction);
queueNext(queue);
}
void _pokerGameActionBetOnEnd(
queue_t *queue, queueaction_t *action, uint8_t i
) {
uint8_t j;
pokerplayer_t *player;
pokergame_t *game = (pokergame_t *)action->data;
bool playersPending;
// 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.
for(j = 0; j < POKER_PLAYER_COUNT; j++) {
player = game->poker.players + j;
if(player->state & POKER_PLAYER_STATE_FOLDED) continue;
if(player->state & POKER_PLAYER_STATE_OUT) continue;
if(player->currentBet >= game->poker.bet.currentBet) continue;
// Yes, this player needs to check, raise or fold
pokerGameActionBetAdd(game);
playersPending = true;
break;
}
// Are we waiting on any players?
if(playersPending) return;
// No! Begin the next flop.
printf("Not waiting on anything\n");
}
queueaction_t * pokerGameActionBetAdd(pokergame_t *game) {
queueaction_t *action = pokerGameActionAdd(game); queueaction_t *action = pokerGameActionAdd(game);
action->onStart = &_pokerGameActionBetOnStart;
action->onUpdate = &_pokerGameActionBetOnUpdate;
action->onEnd = &_pokerGameActionBetOnEnd;
return action; return action;
} }

View File

@ -8,5 +8,7 @@
#pragma once #pragma once
#include <dawn/dawn.h> #include <dawn/dawn.h>
#include "action.h" #include "action.h"
#include "../../../poker/turn.h"
#include "../../../poker/bet.h"
queueaction_t * pokerGameActionBetAdd(pokergame_t *game); queueaction_t * pokerGameActionBetAdd(pokergame_t *game);

View File

@ -35,7 +35,10 @@ void _pokerGameActionRoundOnEnd(queue_t *queue,queueaction_t *action,uint8_t i){
// Deal // Deal
pokerActionDealAdd(queue, &game->poker); pokerActionDealAdd(queue, &game->poker);
// Begin Betting Round // Begin Betting Round. This will queue for one player only and then the round
// will take over.
pokerBetResetBetter(&game->poker);
pokerGameActionBetAdd(game);
} }
queueaction_t * pokerGameActionRoundAdd(pokergame_t *game) { queueaction_t * pokerGameActionRoundAdd(pokergame_t *game) {

View File

@ -6,9 +6,11 @@
#pragma once #pragma once
#include <dawn/dawn.h> #include <dawn/dawn.h>
#include "action.h" #include "action.h"
#include "../../../poker/bet.h"
#include "../../../poker/actions/round.h" #include "../../../poker/actions/round.h"
#include "../../../poker/actions/blinds.h" #include "../../../poker/actions/blinds.h"
#include "../../../poker/actions/deal.h" #include "../../../poker/actions/deal.h"
#include "bet.h"
void _pokerGameActionRoundOnStart( void _pokerGameActionRoundOnStart(
queue_t *queue, queueaction_t *action, uint8_t i queue_t *queue, queueaction_t *action, uint8_t i

View File

@ -14,12 +14,18 @@ void pokerBetInit(pokerbet_t *bet) {
void pokerBetReset(pokerbet_t *bet) { void pokerBetReset(pokerbet_t *bet) {
bet->pot = 0; bet->pot = 0;
bet->currentBet = 0;
} }
void pokerBetPlayer(poker_t *poker, pokerplayer_t *player, int32_t chips) { void pokerBetPlayer(poker_t *poker, pokerplayer_t *player, int32_t chips) {
poker->bet.pot += chips; poker->bet.pot += chips;
player->chips -= chips; player->chips -= chips;
player->currentBet += chips; player->currentBet += chips;
poker->bet.currentBet = mathMax(poker->bet.currentBet, player->currentBet);
}
void pokerBetResetBetter(poker_t *poker) {
poker->bet.better = (poker->roundSmallBlind + 1) % POKER_PLAYER_COUNT;
} }
void pokerBetTakeBlinds(poker_t *poker) { void pokerBetTakeBlinds(poker_t *poker) {

View File

@ -29,6 +29,14 @@ void pokerBetReset(pokerbet_t *bet);
*/ */
void pokerBetPlayer(poker_t *poker, pokerplayer_t *player, int32_t chips); void pokerBetPlayer(poker_t *poker, pokerplayer_t *player, int32_t chips);
/**
* Reset the current better back to the round/turns default. The better will
* always be the player to the right of the small blind player.
*
* @param poker Poker game to update for.
*/
void pokerBetResetBetter(poker_t *poker);
/** /**
* Takes the current blinds from the correct players. * Takes the current blinds from the correct players.
* *

28
src/poker/turn.c Normal file
View File

@ -0,0 +1,28 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#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;
// I have nfi
turn.type = POKER_TURN_TYPE_BET;
turn.chips = 1;
turn.confidence = 1;
return turn;
}

11
src/poker/turn.h Normal file
View File

@ -0,0 +1,11 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex);