From 3be72f4ea63cb517ef1b466c3c224af6b9b84a6c Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 20 Sep 2021 18:33:00 -0700 Subject: [PATCH] Fixed Winner logic --- src/engine/engine.c | 2 ++ src/engine/engine.h | 1 + src/game/poker/actions/bet.c | 14 +++++++++++--- src/game/poker/actions/bet.h | 8 ++++---- src/game/poker/actions/winner.c | 32 ++++++++++++++++++++++++++++++++ src/game/poker/actions/winner.h | 25 +++++++++++++++++++++++++ src/game/poker/pokerdiscussion.c | 2 ++ src/game/poker/pokerdiscussion.h | 1 + src/game/poker/pokerui.c | 7 +++++++ src/poker/winner.c | 26 ++++++++++++++++++++++---- src/util/rand.h | 6 ++++++ 11 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 src/game/poker/actions/winner.c create mode 100644 src/game/poker/actions/winner.h diff --git a/src/engine/engine.c b/src/engine/engine.c index 90c42a0b..07f64c93 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -8,6 +8,8 @@ #include "engine.h" void engineInit(engine_t *engine) { + randSeed(123); + epochInit(&engine->time); renderInit(); inputInit(&engine->input); diff --git a/src/engine/engine.h b/src/engine/engine.h index f68e68f9..d5d34c58 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -7,6 +7,7 @@ #pragma once #include "../libs.h" +#include "../util/rand.h" #include "../input/input.h" #include "../epoch/epoch.h" #include "../display/render.h" diff --git a/src/game/poker/actions/bet.c b/src/game/poker/actions/bet.c index a706195e..0eed7a1e 100644 --- a/src/game/poker/actions/bet.c +++ b/src/game/poker/actions/bet.c @@ -34,7 +34,7 @@ void _pokerGameActionBetOnUpdate( isHuman = game->poker.bet.better == POKER_PLAYER_HUMAN_INDEX; // Handle as an AI - if(isHuman) { + if(isHuman && false) { turn = game->ui.betTurn; turnMade = game->ui.betTurnMade; } else { @@ -90,6 +90,7 @@ void _pokerGameActionBetOnEnd( pokerdiscussiondata_t discussion; pokergame_t *game = (pokergame_t *)action->data; + // Get which player is remaining to move. game->poker.bet.better = pokerBetGetRemainingPlayer( &game->poker.bet, game->poker.players, game->poker.roundSmallBlind @@ -109,6 +110,9 @@ void _pokerGameActionBetOnEnd( return; } + // Prep convo + discussion.poker = game; + // Not waiting, restack and do next action. printf("Not waiting on anything!\n"); @@ -116,7 +120,6 @@ void _pokerGameActionBetOnEnd( next = pokerActionNextFlopAdd(queue, &game->poker); if(next != NULL) { discussion.reason = POKER_DISCUSSION_REASON_FLOP; - discussion.poker = game; pokerDiscussionQueue(&discussion); pokerBetResetBetter( @@ -128,8 +131,13 @@ void _pokerGameActionBetOnEnd( return; } - /** Queue a restack */ + // Done betting printf("All betting is done, reveal\n"); + discussion.reason = POKER_DISCUSSION_REASON_BETTING_DONE; + discussion.poker = game; + pokerDiscussionQueue(&discussion); + pokerGameActionRestackAdd(game); + pokerGameActionWinnerAdd(game); } queueaction_t * pokerGameActionBetAdd(pokergame_t *game) { diff --git a/src/game/poker/actions/bet.h b/src/game/poker/actions/bet.h index 9b60b0d9..6dbbe97b 100644 --- a/src/game/poker/actions/bet.h +++ b/src/game/poker/actions/bet.h @@ -7,13 +7,13 @@ #pragma once #include "../../../libs.h" -#include "action.h" -#include "restack.h" -#include "../../../input/input.h" +#include "../../../poker/actions/flop.h" #include "../../../poker/turn.h" #include "../../../poker/bet.h" -#include "../../../poker/actions/flop.h" #include "../pokerdiscussion.h" +#include "action.h" +#include "restack.h" +#include "winner.h" /** Callback when the bet action is updated. */ void _pokerGameActionBetOnUpdate( diff --git a/src/game/poker/actions/winner.c b/src/game/poker/actions/winner.c new file mode 100644 index 00000000..b0002914 --- /dev/null +++ b/src/game/poker/actions/winner.c @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "winner.h" + +void _pokerGameActionWinnerOnStart( + queue_t *queue, queueaction_t *action, uint8_t i +) { + pokergame_t *game = (pokergame_t *)action->data; + printf("Winner start action"); + pokerWinnerCalculate( + &game->poker.winner, + &game->poker.dealer, + game->poker.players + ); + + printf("Winner Count %u\n", game->poker.winner.winnerCount); + for(uint8_t i = 0; i < game->poker.winner.winnerCount; i++) { + uint8_t winner = game->poker.winner.winners[i]; + printf("Winner %u\n", winner); + } +} + +queueaction_t * pokerGameActionWinnerAdd(pokergame_t *game) { + queueaction_t *action = pokerGameActionAdd(game); + action->onStart = &_pokerGameActionWinnerOnStart; + return action; +} \ No newline at end of file diff --git a/src/game/poker/actions/winner.h b/src/game/poker/actions/winner.h new file mode 100644 index 00000000..de9069d6 --- /dev/null +++ b/src/game/poker/actions/winner.h @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../../../libs.h" +#include "../../../poker/winner.h" +#include "../pokerdiscussion.h" +#include "action.h" + +/** Callback to fire when the winner starts */ +void _pokerGameActionWinnerOnStart( + queue_t *queue, queueaction_t *action, uint8_t i +); + +/** + * Queue a winning game action. + * + * @param game Game to queue for. + * @return The queued winning action. + */ +queueaction_t * pokerGameActionWinnerAdd(pokergame_t *game); \ No newline at end of file diff --git a/src/game/poker/pokerdiscussion.c b/src/game/poker/pokerdiscussion.c index 9847dc32..aec958da 100644 --- a/src/game/poker/pokerdiscussion.c +++ b/src/game/poker/pokerdiscussion.c @@ -96,6 +96,8 @@ void pokerDiscussionGet( } void pokerDiscussionQueue(pokerdiscussiondata_t *data) { + return; + pokerdiscussion_t discussion; uint8_t i, player; diff --git a/src/game/poker/pokerdiscussion.h b/src/game/poker/pokerdiscussion.h index c3834b6e..8e787159 100644 --- a/src/game/poker/pokerdiscussion.h +++ b/src/game/poker/pokerdiscussion.h @@ -25,6 +25,7 @@ #define POKER_DISCUSSION_REASON_PLAYER_RAISING 0x07 #define POKER_DISCUSSION_REASON_FLOP 0x08 #define POKER_DISCUSSION_REASON_DEAL 0x09 +#define POKER_DISCUSSION_REASON_BETTING_DONE 0x0A typedef struct { pokergame_t *poker; diff --git a/src/game/poker/pokerui.c b/src/game/poker/pokerui.c index 81d4dadc..91fd7253 100644 --- a/src/game/poker/pokerui.c +++ b/src/game/poker/pokerui.c @@ -116,6 +116,13 @@ void pokerUiRender( imageRender(&ui->card, &assets->shader, i * 64.0f, j * 100.0f); } } + + for(j = 0; j < poker->dealer.cardsFacing; j++) { + pokerUiSetImageToCard( + &ui->card, &assets->cardTexture, poker->dealer.cards[j] + ); + imageRender(&ui->card, &assets->shader, 200, j * 100.0f); + } } // Betting UI diff --git a/src/poker/winner.c b/src/poker/winner.c index 5f9156cd..317a5fab 100644 --- a/src/poker/winner.c +++ b/src/poker/winner.c @@ -258,10 +258,18 @@ card_t pokerWinnerCompare(pokerplayerwinning_t *left, pokerplayerwinning_t *righ card = left->set[i]; number = cardGetNumber(card); if(highNumberLeft != 0xFF && number < highNumberLeft) continue;// Quick check - index = cardContainsNumber(right->set, right->setSize, number); - if(index != -1) continue;// In other? - if(highNumberLeft == 0xFF||number == CARD_ACE||highNumberRight < number) { + // Check if this number is within the other hand or not + index = cardContainsNumber(right->set, right->setSize, number); + if(index != -1) { + // This number IS within the other hand, let's check that the EXACT card + // is a match/isn't a match. + index = cardContains(right->set, right->setSize, card); + if(index != -1) continue;// Exact card match + // Not exact card match.. ? + } + + if(highNumberLeft == 0xFF||number == CARD_ACE||highNumberLeft < number) { highNumberLeft = number; highCardLeft = card; } @@ -271,8 +279,12 @@ card_t pokerWinnerCompare(pokerplayerwinning_t *left, pokerplayerwinning_t *righ card = right->set[i]; number = cardGetNumber(card); if(highNumberRight != 0xFF && number < highNumberRight) continue; + index = cardContainsNumber(left->set, left->setSize, number); - if(index != -1) continue; + if(index != -1) { + index = cardContains(left->set, left->setSize, card); + if(index != -1) continue; + } if(highNumberRight == 0xFF||number == CARD_ACE||highNumberRight < number) { highNumberRight = number; @@ -280,6 +292,11 @@ card_t pokerWinnerCompare(pokerplayerwinning_t *left, pokerplayerwinning_t *righ } } + if(highCardLeft == 0xFF && highCardRight == 0xFF) { + printf("Here\n"); + } + + if(highCardLeft == 0xFF) return 0xFF; if(highNumberLeft < highNumberRight) return 0xFF; return highCardLeft;//Greater or Equal to. } @@ -294,6 +311,7 @@ void pokerWinnerCalculate( bool isWinner; winner->winnerCount = 0; + highCard = 0xFF; // Get winning sets for(i = 0; i < POKER_PLAYER_COUNT; i++) { diff --git a/src/util/rand.h b/src/util/rand.h index 89571a43..47ca5bdf 100644 --- a/src/util/rand.h +++ b/src/util/rand.h @@ -9,6 +9,12 @@ #include "../libs.h" #include "math.h" +/** + * Seed the random number generator + * @param seed Seed to use for the seeded random number generator. + */ +#define randSeed(seed) (srand(seed)) + /** * Generates a random int32_t. * @returns A random int32_t number.