Moved code from card to poker

This commit is contained in:
2021-05-16 21:01:36 -07:00
parent 06464c260a
commit 4dbd5acab9
36 changed files with 9 additions and 9 deletions

80
src/poker/action/action.c Normal file
View File

@ -0,0 +1,80 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "action.h"
void holdemActionInit() {
// Free up all actions
memset(HOLDEM_GAME_STATE.actionQueue, (int32_t)NULL,
sizeof(holdemaction_t) * HOLDEM_GAME_ACTION_QUEUE_SIZE
);
// Free up all data
memset(HOLDEM_GAME_STATE.actionData, (int32_t)NULL, sizeof(void *) *
HOLDEM_GAME_ACTION_DATA_SIZE * HOLDEM_GAME_ACTION_QUEUE_SIZE
);
}
int32_t holdemActionAdd(holdemaction_t action) {
int32_t i = -1;
int32_t j = -1;
for(i = 0; i < HOLDEM_GAME_ACTION_QUEUE_SIZE; i++) {
if(HOLDEM_GAME_STATE.actionQueue[i].init != NULL) continue;
j = i;
break;
}
if(j == -1) return j;
HOLDEM_GAME_STATE.actionQueue[j] = action;
HOLDEM_GAME_STATE.actionInitState[j] = false;
return j;
}
void holdemActionRemove(int32_t index) {
if(HOLDEM_GAME_STATE.actionQueue[index].dispose != NULL) {
HOLDEM_GAME_STATE.actionQueue[index].dispose(
index, HOLDEM_GAME_STATE.actionData + index
);
}
memset(HOLDEM_GAME_STATE.actionQueue+index, (int32_t)NULL,
sizeof(holdemaction_t)
);
memset(HOLDEM_GAME_STATE.actionData+index, (int32_t)NULL,
sizeof(void *) * HOLDEM_GAME_ACTION_DATA_SIZE
);
}
void holdemActionUpdate() {
int32_t i;
void **data;
holdemaction_t *action;
for(i = 0; i < HOLDEM_GAME_ACTION_QUEUE_SIZE; i++) {
action = HOLDEM_GAME_STATE.actionQueue + i;
data = HOLDEM_GAME_STATE.actionData + i;
if(action->init != NULL && !HOLDEM_GAME_STATE.actionInitState[i]) {
HOLDEM_GAME_STATE.actionInitState[i] = true;
action->init(i, data);
}
if(action->update != NULL) {
action->update(i, data);
}
}
}
void holdemActionDispose() {
int32_t i;
for(i = 0; i < HOLDEM_GAME_ACTION_QUEUE_SIZE; i++) {
if(HOLDEM_GAME_STATE.actionQueue[i].dispose == NULL) continue;
HOLDEM_GAME_STATE.actionQueue[i].dispose(i, HOLDEM_GAME_STATE.actionData+i);
}
}

40
src/poker/action/action.h Normal file
View File

@ -0,0 +1,40 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
/**
* Initializes the action manager.
*/
void holdemActionInit();
/**
* Adds an action to the action queue.
*
* @param action Action to add to the queue.
* @returns The index of the action within the queue, or -1 if failure occured.
*/
int32_t holdemActionAdd(holdemaction_t action);
/**
* Removes an action from the action queue.
*
* @param index Action to remove (by index in the queue).
*/
void holdemActionRemove(int32_t index);
/**
* Updates the action manager, which (in turn) updates all actions that are
* currently running.
*/
void holdemActionUpdate();
/**
* Cleans up the action manager and all actions.
*/
void holdemActionDispose();

39
src/poker/action/ai.c Normal file
View File

@ -0,0 +1,39 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "ai.h"
holdemaction_t actionAi() {
return (holdemaction_t){
.init = &actionAiInit,
.update = &actionAiUpdate,
.dispose = &actionAiDispose
};
}
void actionAiInit(int32_t index, void *data) {
logText("AI Round start");
/*
Current theory for the AI code will be;
1 - Determine weight of my cards+flop as %
2 - Determine current bet, compare against [something] and then determine if
worth the risk. I may need history of the game to make informed decision
*/
holdemActionRemove(index);
}
void actionAiUpdate(int32_t index, void *data) {
}
void actionAiDispose(int32_t index, void *data) {
// Do we need to do a flop?
if(HOLDEM_GAME_STATE.cardsFacing < HOLDEM_DEALER_HAND) {
holdemActionAdd(actionFlop());
}
}

18
src/poker/action/ai.h Normal file
View File

@ -0,0 +1,18 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "action.h"
#include "flop.h"
#include "../../debug/log.h"
holdemaction_t actionAi();
void actionAiInit(int32_t index, void *data);
void actionAiUpdate(int32_t index, void *data);
void actionAiDispose(int32_t index, void *data);

45
src/poker/action/deal.c Normal file
View File

@ -0,0 +1,45 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "deal.h"
holdemaction_t actionDeal() {
return (holdemaction_t){
.init = &actionDealInit,
.update = &actionDealUpdate,
.dispose = &actionDealDispose
};
}
void actionDealInit(int32_t index, void *data) {
uint8_t i, j;
holdemplayer_t *player;
logText("Dealing Cards");
for(i = 0; i < 2; i++) {
for(j = 0; j < HOLDEM_PLAYER_COUNT; j++) {
player = HOLDEM_GAME_STATE.players + j;
cardDeal(HOLDEM_GAME_STATE.deck,
player->cards,
HOLDEM_GAME_STATE.deckSize,
player->cardCount
);
HOLDEM_GAME_STATE.deckSize--;
player->cardCount++;
}
}
holdemActionRemove(index);
}
void actionDealUpdate(int32_t i, void *data) {
}
void actionDealDispose(int32_t i, void *data) {
int32_t newI = holdemActionAdd(actionAi());
}

18
src/poker/action/deal.h Normal file
View File

@ -0,0 +1,18 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../../debug/log.h"
#include "action.h"
#include "ai.h"
holdemaction_t actionDeal();
void actionDealInit(int32_t i, void *data);
void actionDealUpdate(int32_t i, void *data);
void actionDealDispose(int32_t i, void *data);

51
src/poker/action/flop.c Normal file
View File

@ -0,0 +1,51 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "flop.h"
holdemaction_t actionFlop() {
return (holdemaction_t){
.init = &actionFlopInit,
.update = &actionFlopUpdate,
.dispose = &actionFlopDispose
};
}
void actionFlopInit(int32_t index, void *data) {
uint8_t i, count;
logText("Flop");
// Look at the dealer
holdemRenderLookHand(&HOLDEM_GAME_STATE.cameraLeft, HOLDEM_GAME_SEAT_DEALER);
// Do the flop
// if(match->cardsFacing >= HOLDEM_DEALER_HAND) return;
// Burn the card off the top
HOLDEM_GAME_STATE.deckSize -= 1;
// Change count depending on facing
count = HOLDEM_GAME_STATE.cardsFacing == 0 ? 0x03 : 0x01;
// Deal
for(i = 0; i < count; i++) {
cardDeal(HOLDEM_GAME_STATE.deck, HOLDEM_GAME_STATE.cards, HOLDEM_GAME_STATE.deckSize, HOLDEM_GAME_STATE.cardsFacing);
HOLDEM_GAME_STATE.deckSize -= 1;
HOLDEM_GAME_STATE.cardsFacing += 1;
}
// Next action
holdemActionRemove(index);
}
void actionFlopUpdate(int32_t index, void *data) {
}
void actionFlopDispose(int32_t index, void *data) {
holdemActionAdd(actionAi());
}

19
src/poker/action/flop.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 <dawn/dawn.h>
#include "../../debug/log.h"
#include "../render/look.h"
#include "action.h"
#include "ai.h"
holdemaction_t actionFlop();
void actionFlopInit(int32_t index, void *data);
void actionFlopUpdate(int32_t index, void *data);
void actionFlopDispose(int32_t index, void *data);

63
src/poker/action/round.c Normal file
View File

@ -0,0 +1,63 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "round.h"
holdemaction_t actionRound() {
return (holdemaction_t){
.init = &actionRoundInit,
.update = &actionRoundUpdate,
.dispose = &actionRoundDispose
};
}
void actionRoundInit(int32_t index, void *data) {
uint8_t i;
holdemplayer_t *player;
logText("Round Start");
// Look at the dealer.
holdemRenderLookHand(&HOLDEM_GAME_STATE.cameraLeft, HOLDEM_GAME_SEAT_DEALER);
// Init the round and shuffle the deck
cardDeckFill(HOLDEM_GAME_STATE.deck);
HOLDEM_GAME_STATE.deckSize = CARD_DECK_SIZE;
HOLDEM_GAME_STATE.pot = 0;
HOLDEM_GAME_STATE.cardsFacing = 0;
// Reset the players
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
player = HOLDEM_GAME_STATE.players + i;
// Clear Round State(s)
player->state &= ~(
HOLDEM_STATE_FOLDED |
HOLDEM_STATE_SHOWING
);
player->cardCount = 0;
player->currentBet = 0;
}
// Next action
holdemActionRemove(index);
}
void actionRoundUpdate(int32_t index, void *data) {
}
void actionRoundAfterShuffle() {
logText("Shuffle Done");
int32_t i = holdemActionAdd(actionDeal());
}
void actionRoundDispose(int32_t index, void *data) {
int32_t newI = holdemActionAdd(actionShuffle());
shuffledata_t *newData=(shuffledata_t *)(HOLDEM_GAME_STATE.actionData + newI);
newData->done = &actionRoundAfterShuffle;
}

23
src/poker/action/round.h Normal file
View File

@ -0,0 +1,23 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../render/look.h"
#include "../../debug/log.h"
#include "action.h"
#include "ai.h"
#include "shuffle.h"
#include "deal.h"
holdemaction_t actionRound();
void actionRoundInit(int32_t index, void *data);
void actionRoundUpdate(int32_t index, void *data);
void actionRoundDispose(int32_t index, void *data);
void actionRoundAfterShuffle();

View File

@ -0,0 +1,30 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "shuffle.h"
holdemaction_t actionShuffle() {
return (holdemaction_t){
.init = &actionShuffleInit,
.update = &actionShuffleUpdate,
.dispose = &actionShuffleDispose
};
}
void actionShuffleInit(int32_t index, void *data) {
logText("Shuffle Deck");
cardShuffle(HOLDEM_GAME_STATE.deck, HOLDEM_GAME_STATE.deckSize);
holdemActionRemove(index);
}
void actionShuffleUpdate(int32_t index, void *data) {
}
void actionShuffleDispose(int32_t index, void *data) {
if(((shuffledata_t *)data) != NULL) ((shuffledata_t *)data)->done();
}

View File

@ -0,0 +1,23 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "action.h"
#include "../../debug/log.h"
typedef struct {
void (*done)();
} shuffledata_t;
holdemaction_t actionShuffle();
void actionShuffleInit(int32_t index, void *data);
void actionShuffleUpdate(int32_t index, void *data);
void actionShuffleDispose(int32_t index, void *data);

46
src/poker/action/start.c Normal file
View File

@ -0,0 +1,46 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "start.h"
holdemaction_t actionStart() {
holdemaction_t action = {
.init = &actionStartInit,
.update = &actionStartUpdate,
.dispose = &actionStartDispose
};
return action;
}
void actionStartInit(int32_t index, void *data) {
uint8_t i;
holdemplayer_t *player;
logText("Holdem Starting");
// Prepare the match
HOLDEM_GAME_STATE.blindBig = 0;
HOLDEM_GAME_STATE.blindSmall = 0;
HOLDEM_GAME_STATE.pot = 0;
// Reset the players
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
player = HOLDEM_GAME_STATE.players + i;
player->state = 0x00;
player->chips = 0;
}
holdemActionRemove(index);
}
void actionStartUpdate(int32_t index, void *data) {
}
void actionStartDispose(int32_t index, void *data) {
// Begin the first round
holdemActionAdd(actionRound());
}

18
src/poker/action/start.h Normal file
View File

@ -0,0 +1,18 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "action.h"
#include "round.h"
#include "../../debug/log.h"
holdemaction_t actionStart();
void actionStartInit(int32_t index, void *data);
void actionStartUpdate(int32_t index, void *data);
void actionStartDispose(int32_t index, void *data);