Writing more poker logic.
This commit is contained in:
@ -8,21 +8,33 @@
|
||||
#include "poker.h"
|
||||
|
||||
void pokerInit(poker2_t *poker) {
|
||||
pokerResetRound(poker);
|
||||
poker->playerCount = 0;
|
||||
poker->state = 0;
|
||||
poker->playerDealer = 0;
|
||||
poker->playerSmallBlind = 0;
|
||||
poker->playerBigBlind = 0;
|
||||
|
||||
pokerResetRound(poker);
|
||||
}
|
||||
|
||||
void pokerResetRound(poker2_t *poker) {
|
||||
uint8_t i;
|
||||
poker2player_t *player;
|
||||
|
||||
poker->deckSize = cardWriteDeck(poker->deck);
|
||||
poker->graveSize = 0;
|
||||
poker->communitySize = 0;
|
||||
poker->potCount = 0;
|
||||
pokerPotAdd(poker);
|
||||
|
||||
for(i = 0; i < poker->playerCount; i++) {
|
||||
player = poker->players + i;
|
||||
player->cardCount = 0;
|
||||
player->currentBet = 0;
|
||||
player->state &= ~(
|
||||
POKER_PLAYER_STATE_FOLDED |
|
||||
POKER_PLAYER_STATE_HAS_BET_THIS_ROUND
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void pokerNewDealer(poker2_t *poker) {
|
||||
@ -57,6 +69,16 @@ void pokerTakeBlinds(poker2_t *poker, int32_t small, int32_t big) {
|
||||
pokerPlayerBet(poker, poker->playerBigBlind, big);
|
||||
}
|
||||
|
||||
int32_t pokerGetCallValue(poker2_t *poker) {
|
||||
uint8_t i;
|
||||
int32_t call;
|
||||
call = 0;
|
||||
for(i = 0; i < poker->playerCount; i++) {
|
||||
call = mathMax(call, poker->players[i].currentBet);
|
||||
}
|
||||
return call;
|
||||
}
|
||||
|
||||
// Pot functions
|
||||
uint8_t pokerPotAdd(poker2_t *poker) {
|
||||
poker2pot_t *pot;
|
||||
@ -99,6 +121,7 @@ uint8_t pokerPlayerAdd(poker2_t *poker) {
|
||||
player = poker->players + i;
|
||||
player->cardCount = 0;
|
||||
player->chips = 0;
|
||||
player->currentBet = 0;
|
||||
player->state = POKER_PLAYER_STATE_OUT;
|
||||
|
||||
return i;
|
||||
@ -136,6 +159,15 @@ void pokerPlayerDealAll(poker2_t *poker, uint8_t count) {
|
||||
}
|
||||
}
|
||||
|
||||
bool pokerPlayerDoesNeedToBetThisRound(poker2_t *poker, uint8_t playerIndex) {
|
||||
poker2player_t *player;
|
||||
player = poker->players + playerIndex;
|
||||
if(player->state & POKER_PLAYER_STATE_FOLDED) return false;
|
||||
if(!(player->state & POKER_PLAYER_STATE_HAS_BET_THIS_ROUND)) return true;
|
||||
if(player->currentBet < pokerGetCallValue(poker)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Betting
|
||||
void pokerPlayerBetPot(
|
||||
poker2_t *poker, poker2pot_t *pot, uint8_t playerIndex, int32_t chips
|
||||
@ -143,6 +175,7 @@ void pokerPlayerBetPot(
|
||||
poker2player_t *player;
|
||||
player = poker->players + playerIndex;
|
||||
player->chips -= chips;
|
||||
player->currentBet += chips;
|
||||
pot->chips += chips;
|
||||
player->state |= POKER_PLAYER_STATE_HAS_BET_THIS_ROUND;
|
||||
pokerPotAddPlayer(pot, playerIndex);
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "../libs.h"
|
||||
#include "../util/flags.h"
|
||||
#include "../util/array.h"
|
||||
#include "../util/math.h"
|
||||
#include "../poker/card.h"
|
||||
|
||||
#define POKER_PLAYER_COUNT_MAX 5
|
||||
@ -27,6 +28,7 @@ typedef struct {
|
||||
card_t cards[POKER_PLAYER_HAND_SIZE_MAX];
|
||||
uint8_t cardCount;
|
||||
uint8_t state;
|
||||
int32_t currentBet;
|
||||
} poker2player_t;
|
||||
|
||||
typedef struct {
|
||||
@ -55,7 +57,6 @@ typedef struct {
|
||||
uint8_t playerSmallBlind;
|
||||
uint8_t playerBigBlind;
|
||||
|
||||
|
||||
uint8_t state;
|
||||
} poker2_t;
|
||||
|
||||
@ -69,6 +70,7 @@ void pokerInit(poker2_t *poker);
|
||||
void pokerResetRound(poker2_t *poker);
|
||||
void pokerNewDealer(poker2_t *poker);
|
||||
void pokerTakeBlinds(poker2_t *poker, int32_t small, int32_t big);
|
||||
int32_t pokerGetCallValue(poker2_t *poker);
|
||||
|
||||
uint8_t pokerPotAdd(poker2_t *poker);
|
||||
void pokerPotAddPlayer(poker2pot_t *pot, uint8_t playerIndex);
|
||||
@ -80,6 +82,7 @@ uint8_t pokerPlayerAdd(poker2_t *poker);
|
||||
void pokerPlayerDeal(poker2_t *poker, poker2player_t *player, uint8_t count);
|
||||
void pokerPlayerChipsAdd(poker2player_t *player, int32_t chips);
|
||||
void pokerPlayerDealAll(poker2_t *poker, uint8_t count);
|
||||
bool pokerPlayerDoesNeedToBetThisRound(poker2_t *poker, uint8_t playerIndex);
|
||||
void pokerPlayerBetPot(
|
||||
poker2_t *poker, poker2pot_t *pot, uint8_t playerIndex, int32_t chips
|
||||
);
|
||||
|
@ -34,6 +34,37 @@ void test_pokerResetRound_should_ResetTheRound(void) {
|
||||
TEST_ASSERT_EQUAL_UINT8(1, poker.potCount);
|
||||
}
|
||||
|
||||
void test_pokerResetRound_should_ResetThePlayers(void) {
|
||||
poker2_t poker;
|
||||
uint8_t i;
|
||||
pokerInit(&poker);
|
||||
|
||||
pokerPlayerAdd(&poker);
|
||||
pokerPlayerAdd(&poker);
|
||||
pokerPlayerAdd(&poker);
|
||||
|
||||
for(i = 0; i < poker.playerCount; i++) {
|
||||
poker.players[i].cardCount = 32;
|
||||
poker.players[i].chips = 100;
|
||||
poker.players[i].state = 0xFF;
|
||||
poker.players[i].currentBet = 12345;
|
||||
};
|
||||
|
||||
pokerResetRound(&poker);
|
||||
|
||||
for(i = 0; i < poker.playerCount; i++) {
|
||||
TEST_ASSERT_EQUAL_UINT8(0, poker.players[i].cardCount);
|
||||
TEST_ASSERT_EQUAL_INT32(0, poker.players[i].currentBet);
|
||||
TEST_ASSERT_EQUAL_INT32(100, poker.players[i].chips);
|
||||
TEST_ASSERT_BITS_LOW(POKER_PLAYER_STATE_FOLDED, poker.players[i].state);
|
||||
TEST_ASSERT_BITS_LOW(POKER_PLAYER_STATE_HAS_BET_THIS_ROUND, poker.players[i].state);
|
||||
TEST_ASSERT_EQUAL_UINT8(
|
||||
0xFF - POKER_PLAYER_STATE_FOLDED - POKER_PLAYER_STATE_HAS_BET_THIS_ROUND,
|
||||
poker.players[i].state
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void test_pokerNewDealer_should_FindANewDealer(void) {
|
||||
poker2_t poker;
|
||||
pokerInit(&poker);
|
||||
@ -191,8 +222,9 @@ void test_pokerPlayerAdd_should_ResetThePlayer(void) {
|
||||
|
||||
player = poker.players + pokerPlayerAdd(&poker);
|
||||
TEST_ASSERT_EQUAL_INT32(0, player->chips);
|
||||
TEST_ASSERT_EQUAL_UINT8(POKER_PLAYER_STATE_OUT, player->state);
|
||||
TEST_ASSERT_EQUAL_INT32(0, player->currentBet);
|
||||
TEST_ASSERT_EQUAL_UINT8(0, player->cardCount);
|
||||
TEST_ASSERT_EQUAL_UINT8(POKER_PLAYER_STATE_OUT, player->state);
|
||||
}
|
||||
|
||||
void test_pokerTurn_should_TurnCardsFromTheDeck(void) {
|
||||
@ -236,6 +268,53 @@ void test_pokerBurn_should_SendCardsToTheGrave(void) {
|
||||
|
||||
}
|
||||
|
||||
void test_pokerGetCallValue_should_CalculateTheCallValue(void) {
|
||||
poker2_t poker;
|
||||
uint8_t first, second;
|
||||
pokerInit(&poker);
|
||||
first = pokerPlayerAdd(&poker);
|
||||
second = pokerPlayerAdd(&poker);
|
||||
pokerPlayerChipsAdd(poker.players + first, 10000);
|
||||
pokerPlayerChipsAdd(poker.players + second, 10000);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT32(0, pokerGetCallValue(&poker));
|
||||
pokerPlayerBet(&poker, first, 100);
|
||||
TEST_ASSERT_EQUAL_INT32(100, pokerGetCallValue(&poker));
|
||||
pokerPlayerBet(&poker, second, 150);
|
||||
TEST_ASSERT_EQUAL_INT32(150, pokerGetCallValue(&poker));
|
||||
|
||||
pokerPotAdd(&poker);
|
||||
TEST_ASSERT_EQUAL_INT32(150, pokerGetCallValue(&poker));
|
||||
TEST_ASSERT_EQUAL_INT32(0, poker.pots[1].chips);
|
||||
pokerPlayerBet(&poker, second, 50);
|
||||
TEST_ASSERT_EQUAL_INT32(200, pokerGetCallValue(&poker));
|
||||
TEST_ASSERT_EQUAL_INT32(50, poker.pots[1].chips);
|
||||
}
|
||||
|
||||
void test_pokerGetCallValue_should_SkipOutPlayers(void) {
|
||||
poker2_t poker;
|
||||
uint8_t i;
|
||||
pokerInit(&poker);
|
||||
|
||||
i = pokerPlayerAdd(&poker);
|
||||
pokerPlayerChipsAdd(poker.players + i, 10000);
|
||||
pokerPlayerBet(&poker, i, 300);
|
||||
|
||||
i = pokerPlayerAdd(&poker);
|
||||
pokerPlayerChipsAdd(poker.players + i, 10000);
|
||||
pokerPlayerBet(&poker, i, 500);
|
||||
|
||||
i = pokerPlayerAdd(&poker);
|
||||
pokerPlayerChipsAdd(poker.players + i, 10000);
|
||||
pokerPlayerBet(&poker, i, 200);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT32(500, pokerGetCallValue(&poker));
|
||||
poker.players[1].state |= POKER_PLAYER_STATE_OUT;
|
||||
TEST_ASSERT_EQUAL_INT32(300, pokerGetCallValue(&poker));
|
||||
poker.players[0].state |= POKER_PLAYER_STATE_OUT;
|
||||
TEST_ASSERT_EQUAL_INT32(200, pokerGetCallValue(&poker));
|
||||
}
|
||||
|
||||
void test_pokerPlayerDeal_should_DealCardsToThePlayer(void) {
|
||||
poker2_t poker;
|
||||
uint8_t playerIndex;
|
||||
@ -383,6 +462,30 @@ void test_pokerPlayerDealAll_should_NotDealToFoldedPlayers(void) {
|
||||
TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_JACK, poker.players[2].cards[1]);
|
||||
}
|
||||
|
||||
void test_pokerPlayerDoesNeedToBetThisRound_should_CheckCallValue(void) {
|
||||
poker2_t poker;
|
||||
uint8_t first, second;
|
||||
|
||||
pokerInit(&poker);
|
||||
|
||||
first = pokerPlayerAdd(&poker);
|
||||
second = pokerPlayerAdd(&poker);
|
||||
pokerPlayerChipsAdd(poker.players + first, 10000);
|
||||
pokerPlayerChipsAdd(poker.players + second, 10000);
|
||||
|
||||
pokerPlayerBet(&poker, first, 100);
|
||||
TEST_ASSERT_EQUAL(false, pokerPlayerDoesNeedToBetThisRound(&poker, first));
|
||||
TEST_ASSERT_EQUAL(true, pokerPlayerDoesNeedToBetThisRound(&poker, second));
|
||||
|
||||
pokerPlayerBet(&poker, second, 200);
|
||||
TEST_ASSERT_EQUAL(true, pokerPlayerDoesNeedToBetThisRound(&poker, first));
|
||||
TEST_ASSERT_EQUAL(false, pokerPlayerDoesNeedToBetThisRound(&poker, second));
|
||||
|
||||
pokerPlayerBet(&poker, first, 100);
|
||||
TEST_ASSERT_EQUAL(false, pokerPlayerDoesNeedToBetThisRound(&poker, first));
|
||||
TEST_ASSERT_EQUAL(false, pokerPlayerDoesNeedToBetThisRound(&poker, second));
|
||||
}
|
||||
|
||||
void test_pokerPlayerBetPot_should_AddChipsToThePot(void) {
|
||||
poker2_t poker;
|
||||
poker2pot_t *pot;
|
||||
@ -414,8 +517,14 @@ void test_pokerPlayerBetPot_should_UpdatePlayerState(void) {
|
||||
pokerPlayerChipsAdd(player, 1000);
|
||||
|
||||
TEST_ASSERT_BITS_LOW(POKER_PLAYER_STATE_HAS_BET_THIS_ROUND, player->state);
|
||||
TEST_ASSERT_EQUAL_INT32(0, player->currentBet);
|
||||
|
||||
pokerPlayerBetPot(&poker, poker.pots, i, 100);
|
||||
TEST_ASSERT_BITS_HIGH(POKER_PLAYER_STATE_HAS_BET_THIS_ROUND, player->state);
|
||||
TEST_ASSERT_EQUAL_INT32(100, player->currentBet);
|
||||
|
||||
pokerPlayerBetPot(&poker, poker.pots, i, 250);
|
||||
TEST_ASSERT_EQUAL_INT32(350, player->currentBet);
|
||||
}
|
||||
|
||||
void test_pokerPlayerBet_should_BetToTheActivePot(void) {
|
||||
@ -444,6 +553,7 @@ int test_poker() {
|
||||
|
||||
RUN_TEST(test_pokerInit_should_InitializePokerGame);
|
||||
RUN_TEST(test_pokerResetRound_should_ResetTheRound);
|
||||
RUN_TEST(test_pokerResetRound_should_ResetThePlayers);
|
||||
RUN_TEST(test_pokerPotAdd_should_AddAPot);
|
||||
RUN_TEST(test_pokerPotAdd_should_ResetThePot);
|
||||
RUN_TEST(test_pokerNewDealer_should_FindANewDealer);
|
||||
@ -453,6 +563,7 @@ int test_poker() {
|
||||
RUN_TEST(test_pokerPlayerAdd_should_ResetThePlayer);
|
||||
RUN_TEST(test_pokerTurn_should_TurnCardsFromTheDeck);
|
||||
RUN_TEST(test_pokerBurn_should_SendCardsToTheGrave);
|
||||
RUN_TEST(test_pokerGetCallValue_should_CalculateTheCallValue);
|
||||
RUN_TEST(test_pokerPlayerDeal_should_DealCardsToThePlayer);
|
||||
RUN_TEST(test_pokerPlayerChipsAdd_should_AddChipsToThePlayer);
|
||||
RUN_TEST(test_pokerPlayerChipsAdd_should_TurnOutStateOff);
|
||||
@ -460,6 +571,7 @@ int test_poker() {
|
||||
RUN_TEST(test_pokerPlayerDealAll_should_DealMultipleCardsToEveryone);
|
||||
RUN_TEST(test_pokerPlayerDealAll_should_NotDealToOutPlayers);
|
||||
RUN_TEST(test_pokerPlayerDealAll_should_NotDealToFoldedPlayers);
|
||||
RUN_TEST(test_pokerPlayerDoesNeedToBetThisRound_should_CheckCallValue);
|
||||
RUN_TEST(test_pokerPlayerBetPot_should_AddChipsToThePot);
|
||||
RUN_TEST(test_pokerPlayerBetPot_should_UpdatePlayerState);
|
||||
RUN_TEST(test_pokerPlayerBet_should_BetToTheActivePot);
|
||||
|
Reference in New Issue
Block a user