From 9756b4eb7d0b52bcfa184cfa8354e5fbcefd84e3 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 15 May 2022 01:08:03 -0700 Subject: [PATCH] Progress has been made. --- src/conversation/items/better.c | 120 ++++++++++++++++ src/conversation/items/better.h | 13 ++ src/conversation/items/flop.c | 57 ++++++++ src/conversation/items/flop.h | 13 ++ src/conversation/items/gameintro.c | 15 ++ src/conversation/items/gameintro.h | 14 ++ src/conversation/items/winner.c | 56 ++++++++ src/conversation/items/winner.h | 11 ++ src/conversation/queue.c | 223 ++--------------------------- src/conversation/queue.h | 5 +- src/strings.c | 19 ++- src/strings.h | 11 +- 12 files changed, 338 insertions(+), 219 deletions(-) create mode 100644 src/conversation/items/better.c create mode 100644 src/conversation/items/better.h create mode 100644 src/conversation/items/flop.c create mode 100644 src/conversation/items/flop.h create mode 100644 src/conversation/items/gameintro.c create mode 100644 src/conversation/items/gameintro.h create mode 100644 src/conversation/items/winner.c create mode 100644 src/conversation/items/winner.h diff --git a/src/conversation/items/better.c b/src/conversation/items/better.c new file mode 100644 index 0000000..2c4b49d --- /dev/null +++ b/src/conversation/items/better.c @@ -0,0 +1,120 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "better.h" +#include "../../poker/poker.h" +#include "../textbox.h" + +void conversationQueueBeginBetting() { + pokerturn_t turn; + turn.chips = 0; + // Begin the betting process. First we need to decide if the player or the + // AI is betting, then based on that we decide what to do next. + + // TODO: Actually bet. + if(POKER_PLAYER_BETTER == POKER_HUMAN_INDEX) { + // This is the human player. + BGB_MESSAGE("Player folding."); + conversationTextboxSetText(STR_DEBUG_PLAYER); + POKER_PLAYERS[POKER_PLAYER_BETTER].state |= POKER_PLAYER_STATE_FOLDED; + } else { + // This is an AI player, get their turn. + BGB_MESSAGE("AI turn to bet"); + pokerAi(POKER_PLAYER_BETTER, &turn); + BGB_printf("AI Decided to %u, with %u chips and %u confidence, bluffin: %u", turn.type, turn.chips, turn.confidence, turn.bluff); + + switch(turn.type) { + case POKER_TURN_TYPE_FOLD: + POKER_PLAYERS[POKER_PLAYER_BETTER].state |= POKER_PLAYER_STATE_FOLDED; + conversationTextboxSetText(STR_POKER_GAME_AI_FOLD); + break; + + case POKER_TURN_TYPE_BET: + if(turn.bluff) { + conversationTextboxSetText(STR_POKER_GAME_AI_RAISE_BLUFF); + } else { + conversationTextboxSetText(STR_POKER_GAME_AI_RAISE); + } + break; + + case POKER_TURN_TYPE_CALL: + if(turn.bluff) { + conversationTextboxSetText(STR_POKER_GAME_AI_CALL_BLUFF); + } else { + conversationTextboxSetText(STR_POKER_GAME_AI_CALL); + } + break; + + case POKER_TURN_TYPE_ALL_IN: + if(turn.bluff) { + conversationTextboxSetText(STR_POKER_GAME_AI_ALL_IN_BLUFF); + } else { + conversationTextboxSetText(STR_POKER_GAME_AI_ALL_IN); + } + break; + + case POKER_TURN_TYPE_CHECK: + if(turn.bluff) { + conversationTextboxSetText(STR_POKER_GAME_AI_CHECK_BLUFF); + } else { + conversationTextboxSetText(STR_POKER_GAME_AI_CHECK); + } + break; + } + + // Now we have their turn, decide what to say based on that. + } + + // Update bet state + POKER_PLAYERS[POKER_PLAYER_BETTER].state |= ( + POKER_PLAYER_STATE_HAS_BET_THIS_ROUND + ); + + if(turn.chips > 0) { + pokerBet(POKER_PLAYER_BETTER, turn.chips); + } + + QUEUE_ITEM = QUEUE_NEXT_BETTER; +} + +void conversationQueueNextBetter() { + uint8_t i, j, countStillInGame; + + // Now we decide the next better. + countStillInGame = 0; + for(i = 0; i < POKER_PLAYER_COUNT; i++) { + //In theory I don't think the current better can ever be selected again. + // if(i == POKER_PLAYER_BETTER) continue; // Commented because we now count + + // Next better is the better to the right of the current better. + j = (POKER_PLAYER_BETTER + i) % POKER_PLAYER_COUNT; + if(!pokerDoesPlayerNeedToBet(j)) { + if((POKER_PLAYERS[j].state & (POKER_PLAYER_STATE_FOLDED | POKER_PLAYER_STATE_OUT)) == 0) { + countStillInGame++; + } + continue; + } + + // They haven't bet yet, make them the "next better" + POKER_PLAYER_BETTER = j; + + QUEUE_ITEM = QUEUE_BEGIN_BETTING; + conversationQueueNext(); + return; + } + + // If we reach this point then we either need to begin the betting round, or + // we are going to move to the winning decider. + if(POKER_COMMUNITY_SIZE_MAX == POKER_COMMUNITY_SIZE || countStillInGame == 1) { + QUEUE_ITEM = QUEUE_WINNER_DECIDE; + conversationQueueNext(); + return; + } + + QUEUE_ITEM = QUEUE_FLOP; + conversationQueueNext(); +} diff --git a/src/conversation/items/better.h b/src/conversation/items/better.h new file mode 100644 index 0000000..e13fc34 --- /dev/null +++ b/src/conversation/items/better.h @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../../libs.h" +#include "../queue.h" + +void conversationQueueBeginBetting(); +void conversationQueueNextBetter(); \ No newline at end of file diff --git a/src/conversation/items/flop.c b/src/conversation/items/flop.c new file mode 100644 index 0000000..008b26e --- /dev/null +++ b/src/conversation/items/flop.c @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "flop.h" +#include "../queue.h" + +void conversationQueueFlopTurnRiver() { + uint8_t i, count; + pokerplayer_t *player; + + + switch(POKER_COMMUNITY_SIZE) { + case 0: + count = POKER_COUNT_FLOP; + conversationTextboxSetText(STR_POKER_GAME_CARDS_FLOPPED); + break; + + case POKER_COUNT_FLOP: + count = POKER_COUNT_TURN; + conversationTextboxSetText(STR_POKER_GAME_CARDS_TURNED); + break; + + default: + count = POKER_COUNT_RIVER; + conversationTextboxSetText(STR_POKER_GAME_CARDS_RIVERED); + break; + } + + // Reset each players required to bet state + do { + player = POKER_PLAYERS + i; + player->state &= ~POKER_PLAYER_STATE_HAS_BET_THIS_ROUND; + player->timesRaised = 0; + } while(++i < POKER_PLAYER_COUNT); + + // In reality we'd burn the top card but that would waste some CPU I need. + // Deal the top cards. + for(i = 0; i < count; i++) { + POKER_COMMUNITY[POKER_COMMUNITY_SIZE++] = POKER_DECK[--POKER_DECK_SIZE]; + } + + // Check how many players need to bet, if it's zero then all players are + // either folded or all-in + if(pokerGetRemainingBetterCount() == 0x00) { + if(POKER_COMMUNITY_SIZE == POKER_COMMUNITY_SIZE_MAX) { + QUEUE_ITEM = QUEUE_WINNER_DECIDE; + } else { + QUEUE_ITEM = QUEUE_FLOP; + } + } else { + QUEUE_ITEM = QUEUE_BEGIN_BETTING; + } +} diff --git a/src/conversation/items/flop.h b/src/conversation/items/flop.h new file mode 100644 index 0000000..af860d8 --- /dev/null +++ b/src/conversation/items/flop.h @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../../libs.h" +#include "../../poker/poker.h" +#include "../textbox.h" + +void conversationQueueFlopTurnRiver(); \ No newline at end of file diff --git a/src/conversation/items/gameintro.c b/src/conversation/items/gameintro.c new file mode 100644 index 0000000..b1d4b7d --- /dev/null +++ b/src/conversation/items/gameintro.c @@ -0,0 +1,15 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "gameintro.h" +#include "../queue.h" + +void conversationQueueGameBegin() { + char buffer[TEXTBOX_BUFFER_MAX]; + sprintf(STR_GAME_BEGIN, POKER_GAME_BLINDS_CURRENT * 2, POKER_GAME_BLINDS_CURRENT); + conversationTextboxSetText(buffer); +} \ No newline at end of file diff --git a/src/conversation/items/gameintro.h b/src/conversation/items/gameintro.h new file mode 100644 index 0000000..f9c13f5 --- /dev/null +++ b/src/conversation/items/gameintro.h @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../../libs.h" +#include "../../poker/poker.h" +#include "../textbox.h" +#include "../../strings.h" + +void conversationQueueGameBegin(); \ No newline at end of file diff --git a/src/conversation/items/winner.c b/src/conversation/items/winner.c new file mode 100644 index 0000000..1304cb5 --- /dev/null +++ b/src/conversation/items/winner.c @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "winner.h" +#include "../../poker/poker.h" +#include "../textbox.h" + +void conversationQueueWinnerDecide() { + pokerpot_t *pot; + uint8_t i, j, countOfPotsWithChips, chipsEach; + + QUEUE_ITEM = QUEUE_START; + + countOfPotsWithChips = 0; + for(i = 0; i < POKER_POT_COUNT; i++) { + pot = POKER_POTS + i; + if(pot->chips == 0) break; + countOfPotsWithChips++; + } + + // TODO: Messages + if(countOfPotsWithChips == 1) { + } else { + } + + // Pots. + for(i = 0; i < countOfPotsWithChips; i++) { + pot = POKER_POTS + i; + pokerWinnerDetermineForPot( + pot, + POKER_WINNERS, + POKER_WINNER_PLAYERS, + &POKER_WINNER_COUNT, + POKER_WINNER_PARTICIPANTS, + &POKER_WINNER_PARTICIPANT_COUNT + ); + + chipsEach = pot->chips / POKER_WINNER_COUNT; + for(j = 0; j < POKER_WINNER_COUNT; j++) { + POKER_PLAYERS[POKER_WINNER_PLAYERS[j]].chips += chipsEach; + // TODO: I can determine rounding error here if I need to, just sub the + // taken chips for each player and when I get to chips remaining less than + // the chipsEach, then reward the rounding offset. + } + } + + // TODO: Decide on a winner for real. + conversationTextboxSetText(STR_DEBUG_WINNER_DECIDED); + + // New Round + pokerNewRound(); +} diff --git a/src/conversation/items/winner.h b/src/conversation/items/winner.h new file mode 100644 index 0000000..bf1b1d8 --- /dev/null +++ b/src/conversation/items/winner.h @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../../libs.h" + +void conversationQueueWinnerDecide(); \ No newline at end of file diff --git a/src/conversation/queue.c b/src/conversation/queue.c index 6bbe776..751e887 100644 --- a/src/conversation/queue.c +++ b/src/conversation/queue.c @@ -10,6 +10,10 @@ #include "textbox.h" #include "fade.h" #include "../poker/poker.h" +#include "items/flop.h" +#include "items/winner.h" +#include "items/better.h" +#include "items/gameintro.h" uint16_t QUEUE_ITEM; @@ -17,225 +21,16 @@ void conversationQueueDebug() { conversationTextboxSetText(STR_ERROR); } -void conversationQueueBegin() { - QUEUE_ITEM = QUEUE_TAKE_BLINDS; - conversationTextboxSetText(STR_POKER_GAME_START); -} - -void conversationQueueTakeBlinds() { - QUEUE_ITEM = QUEUE_DEAL_CARDS; - conversationTextboxSetText(STR_POKER_GAME_TAKING_BLINDS); -} - void conversationQueueDealCards() { QUEUE_ITEM = QUEUE_BEGIN_BETTING; conversationTextboxSetText(STR_POKER_GAME_CARDS_DEALT); } -void conversationQueueBeginBetting() { - pokerturn_t turn; - turn.chips = 0; - // Begin the betting process. First we need to decide if the player or the - // AI is betting, then based on that we decide what to do next. - - // TODO: Actually bet. - if(POKER_PLAYER_BETTER == POKER_HUMAN_INDEX) { - // This is the human player. - BGB_MESSAGE("Player folding."); - conversationTextboxSetText(STR_DEBUG_PLAYER); - POKER_PLAYERS[POKER_PLAYER_BETTER].state |= POKER_PLAYER_STATE_FOLDED; - } else { - // This is an AI player, get their turn. - BGB_MESSAGE("AI turn to bet"); - pokerAi(POKER_PLAYER_BETTER, &turn); - BGB_printf("AI Decided to %u, with %u chips and %u confidence, bluffin: %u", turn.type, turn.chips, turn.confidence, turn.bluff); - - switch(turn.type) { - case POKER_TURN_TYPE_FOLD: - POKER_PLAYERS[POKER_PLAYER_BETTER].state |= POKER_PLAYER_STATE_FOLDED; - conversationTextboxSetText(STR_POKER_GAME_AI_FOLD); - break; - - case POKER_TURN_TYPE_BET: - if(turn.bluff) { - conversationTextboxSetText(STR_POKER_GAME_AI_RAISE_BLUFF); - } else { - conversationTextboxSetText(STR_POKER_GAME_AI_RAISE); - } - break; - - case POKER_TURN_TYPE_CALL: - if(turn.bluff) { - conversationTextboxSetText(STR_POKER_GAME_AI_CALL_BLUFF); - } else { - conversationTextboxSetText(STR_POKER_GAME_AI_CALL); - } - break; - - case POKER_TURN_TYPE_ALL_IN: - if(turn.bluff) { - conversationTextboxSetText(STR_POKER_GAME_AI_ALL_IN_BLUFF); - } else { - conversationTextboxSetText(STR_POKER_GAME_AI_ALL_IN); - } - break; - - case POKER_TURN_TYPE_CHECK: - if(turn.bluff) { - conversationTextboxSetText(STR_POKER_GAME_AI_CHECK_BLUFF); - } else { - conversationTextboxSetText(STR_POKER_GAME_AI_CHECK); - } - break; - } - - // Now we have their turn, decide what to say based on that. - } - - // Update bet state - POKER_PLAYERS[POKER_PLAYER_BETTER].state |= ( - POKER_PLAYER_STATE_HAS_BET_THIS_ROUND - ); - - if(turn.chips > 0) { - pokerBet(POKER_PLAYER_BETTER, turn.chips); - } - - QUEUE_ITEM = QUEUE_NEXT_BETTER; -} - -void conversationQueueNextBetter() { - uint8_t i, j, countStillInGame; - - // Now we decide the next better. - countStillInGame = 0; - for(i = 0; i < POKER_PLAYER_COUNT; i++) { - //In theory I don't think the current better can ever be selected again. - // if(i == POKER_PLAYER_BETTER) continue; // Commented because we now count - - // Next better is the better to the right of the current better. - j = (POKER_PLAYER_BETTER + i) % POKER_PLAYER_COUNT; - if(!pokerDoesPlayerNeedToBet(j)) { - if((POKER_PLAYERS[j].state & (POKER_PLAYER_STATE_FOLDED | POKER_PLAYER_STATE_OUT)) == 0) { - countStillInGame++; - } - continue; - } - - // They haven't bet yet, make them the "next better" - POKER_PLAYER_BETTER = j; - - QUEUE_ITEM = QUEUE_BEGIN_BETTING; - conversationQueueNext(); - return; - } - - // If we reach this point then we either need to begin the betting round, or - // we are going to move to the winning decider. - if(POKER_COMMUNITY_SIZE_MAX == POKER_COMMUNITY_SIZE || countStillInGame == 1) { - QUEUE_ITEM = QUEUE_WINNER_DECIDE; - conversationQueueNext(); - return; - } - - QUEUE_ITEM = QUEUE_FLOP; +void conversationQueueStart() { + QUEUE_ITEM = QUEUE_GAME_BEGIN; conversationQueueNext(); } -void conversationQueueFlopTurnRiver() { - uint8_t i, count; - pokerplayer_t *player; - - - switch(POKER_COMMUNITY_SIZE) { - case 0: - count = POKER_COUNT_FLOP; - conversationTextboxSetText(STR_POKER_GAME_CARDS_FLOPPED); - break; - - case POKER_COUNT_FLOP: - count = POKER_COUNT_TURN; - conversationTextboxSetText(STR_POKER_GAME_CARDS_TURNED); - break; - - default: - count = POKER_COUNT_RIVER; - conversationTextboxSetText(STR_POKER_GAME_CARDS_RIVERED); - break; - } - - // Reset each players required to bet state - do { - player = POKER_PLAYERS + i; - player->state &= ~POKER_PLAYER_STATE_HAS_BET_THIS_ROUND; - player->timesRaised = 0; - } while(++i < POKER_PLAYER_COUNT); - - // In reality we'd burn the top card but that would waste some CPU I need. - // Deal the top cards. - for(i = 0; i < count; i++) { - POKER_COMMUNITY[POKER_COMMUNITY_SIZE++] = POKER_DECK[--POKER_DECK_SIZE]; - } - - // Check how many players need to bet, if it's zero then all players are - // either folded or all-in - if(pokerGetRemainingBetterCount() == 0x00) { - if(POKER_COMMUNITY_SIZE == POKER_COMMUNITY_SIZE_MAX) { - QUEUE_ITEM = QUEUE_WINNER_DECIDE; - } else { - QUEUE_ITEM = QUEUE_FLOP; - } - } else { - QUEUE_ITEM = QUEUE_BEGIN_BETTING; - } -} - -void conversationQueueWinnerDecide() { - pokerpot_t *pot; - uint8_t i, j, countOfPotsWithChips, chipsEach; - - QUEUE_ITEM = QUEUE_BEGIN; - - countOfPotsWithChips = 0; - for(i = 0; i < POKER_POT_COUNT; i++) { - pot = POKER_POTS + i; - if(pot->chips == 0) break; - countOfPotsWithChips++; - } - - // TODO: Messages - if(countOfPotsWithChips == 1) { - } else { - } - - // Pots. - for(i = 0; i < countOfPotsWithChips; i++) { - pot = POKER_POTS + i; - pokerWinnerDetermineForPot( - pot, - POKER_WINNERS, - POKER_WINNER_PLAYERS, - &POKER_WINNER_COUNT, - POKER_WINNER_PARTICIPANTS, - &POKER_WINNER_PARTICIPANT_COUNT - ); - - chipsEach = pot->chips / POKER_WINNER_COUNT; - for(j = 0; j < POKER_WINNER_COUNT; j++) { - POKER_PLAYERS[POKER_WINNER_PLAYERS[j]].chips += chipsEach; - // TODO: I can determine rounding error here if I need to, just sub the - // taken chips for each player and when I get to chips remaining less than - // the chipsEach, then reward the rounding offset. - } - } - - // TODO: Decide on a winner for real. - conversationTextboxSetText(STR_DEBUG_WINNER_DECIDED); - - // New Round - pokerNewRound(); -} - //////////////////////////////////////////////////////////////////////////////// queuecallback_t *QUEUE_CALLBACKS[] = { @@ -247,14 +42,14 @@ queuecallback_t *QUEUE_CALLBACKS[] = { NULL, // 5 - &conversationQueueBegin, + &conversationQueueStart, NULL, NULL, NULL, NULL, // 10 - &conversationQueueTakeBlinds, + &conversationQueueGameBegin, NULL, NULL, NULL, @@ -304,7 +99,7 @@ queuecallback_t *QUEUE_CALLBACKS[] = { }; inline void conversationQueueInit() { - QUEUE_ITEM = QUEUE_BEGIN; + QUEUE_ITEM = QUEUE_START; } inline void conversationQueueNext() { diff --git a/src/conversation/queue.h b/src/conversation/queue.h index 6ef2a94..cf81cd1 100644 --- a/src/conversation/queue.h +++ b/src/conversation/queue.h @@ -15,13 +15,14 @@ extern uint16_t QUEUE_ITEM; extern queuecallback_t *QUEUE_CALLBACKS[]; #define QUEUE_DEBUG 1 -#define QUEUE_BEGIN 5 -#define QUEUE_TAKE_BLINDS 10 +#define QUEUE_START 5 +#define QUEUE_GAME_BEGIN 10 #define QUEUE_DEAL_CARDS 15 #define QUEUE_BEGIN_BETTING 20 #define QUEUE_NEXT_BETTER 25 #define QUEUE_FLOP 30 #define QUEUE_WINNER_DECIDE 40 +void conversationQueueStart(); inline void conversationQueueInit(); inline void conversationQueueNext(); \ No newline at end of file diff --git a/src/strings.c b/src/strings.c index 6f34736..03619be 100644 --- a/src/strings.c +++ b/src/strings.c @@ -7,11 +7,19 @@ #include "strings.h" +const char STR_GAME_BEGIN[] = "The blinds are set %u for the big, and %u for the small blinds."; + + + + + + + + const char STR_ERROR[] = "An error\nhas occured"; const char STR_HELLO[] = "Hello World, How are you today?\nGood thanks. Thank god!"; const char STR_POKER_GAME_START[] = "Poker game started"; -const char STR_POKER_GAME_TAKING_BLINDS[] = "Blinds taken."; const char STR_POKER_GAME_CARDS_DEALT[] = "Cards dealt."; const char STR_POKER_GAME_CARDS_FLOPPED[] = "Cards flopped"; @@ -30,4 +38,11 @@ const char STR_POKER_GAME_AI_ALL_IN[] = "AI All In"; const char STR_POKER_GAME_AI_ALL_IN_BLUFF[] = "AI All In\nBut Bluffing"; const char STR_POKER_GAME_AI_CHECK[] = "AI Checking"; const char STR_POKER_GAME_AI_CHECK_BLUFF[] = "AI Checking\nBut Bluffing"; -const char STR_FOX[] = "The quick brown fox jumps over the lazy dog."; \ No newline at end of file +const char STR_FOX[] = "The quick brown fox jumps over the lazy dog."; + +const char *STR_PLAYER_NAMES[] = { + "Player", + "Mikuru", + "Haruhi", + "Matoi" +}; \ No newline at end of file diff --git a/src/strings.h b/src/strings.h index 5328d53..7cd2ccd 100644 --- a/src/strings.h +++ b/src/strings.h @@ -8,6 +8,13 @@ #pragma once #include "libs.h" +extern const char STR_GAME_BEGIN[]; + + + + + + extern const char STR_ERROR[]; extern const char STR_HELLO[]; @@ -31,4 +38,6 @@ extern const char STR_POKER_GAME_AI_ALL_IN[]; extern const char STR_POKER_GAME_AI_ALL_IN_BLUFF[]; extern const char STR_POKER_GAME_AI_CHECK[]; extern const char STR_POKER_GAME_AI_CHECK_BLUFF[]; -extern const char STR_FOX[]; \ No newline at end of file +extern const char STR_FOX[]; + +extern const char *STR_PLAYER_NAMES[]; \ No newline at end of file