Writing winner logic.

This commit is contained in:
2021-10-08 22:26:19 -07:00
parent 37cb960484
commit aff317b904
3 changed files with 119 additions and 25 deletions

View File

@ -561,39 +561,45 @@ card_t debug_pokerWinnerCompare(poker2playerwinning_t *left, poker2playerwinning
return highCardLeft;//Greater or Equal to.
}
void pokerWinnerDetermine(poker2_t *poker) {
uint8_t i, j, number, highNumber;
void pokerWinnerDetermineForPot(
poker2_t *poker,
poker2pot_t *pot,
poker2playerwinning_t winners[POKER_PLAYER_COUNT_MAX],
uint8_t winnerPlayers[POKER_PLAYER_COUNT_MAX],
uint8_t *winnerCount,
uint8_t participants[POKER_PLAYER_COUNT_MAX],
uint8_t *participantCount
) {
uint8_t i, j, countPlayers, countWinners, number, highNumber;
poker2playerwinning_t *left, *right;
poker2player_t *player;
card_t card, highCard;
bool isWinner;
poker->winnerCount = 0;
countPlayers = 0;
countWinners = 0;
highCard = 0xFF;
// Get winning sets
for(i = 0; i < poker->playerCount; i++) {
left = poker->winners + i;
left->type = POKER_WINNING_TYPE_NULL;
player = poker->players + i;
if(!pokerPlayerIsInRound(player)) continue;
// Get the players' winning state.
pokerWinnerGetForPlayer(poker, player, left);
// Get participating players and their hands.
for(i = 0; i < pot->playerCount; i++) {
player = poker->players + pot->players[i];
if(player->state & (POKER_PLAYER_STATE_FOLDED|POKER_PLAYER_STATE_OUT)) {
continue;
}
participants[countPlayers] = pot->players[i];
pokerWinnerGetForPlayer(poker, player, winners + countPlayers++);
}
// Compare against each player
for(i = 0; i < poker->playerCount; i++) {
left = poker->winners + i;
if(left->type == POKER_WINNING_TYPE_NULL) continue;
// Compare participating players
for(i = 0; i < countPlayers; i++) {
left = winners + i;
isWinner = true;
highNumber = 0xFF;
for(j = 0; j < poker->playerCount; j++) {
for(j = 0; j < countPlayers; j++) {
if(i == j) continue;
right = poker->winners + j;
if(right->type == POKER_WINNING_TYPE_NULL) continue;
right = winners + j;
// Am I the better hand / Is it the better hand?
if(left->type < right->type) continue;
@ -603,7 +609,7 @@ void pokerWinnerDetermine(poker2_t *poker) {
}
// Equal, compare hands.
card = pokerWinnerCompare(left, right);
card = debug_pokerWinnerCompare(left, right);
if(card == 0xFF) {
isWinner = false;
break;
@ -619,5 +625,9 @@ void pokerWinnerDetermine(poker2_t *poker) {
if(!isWinner) continue;
left->kicker = highCard;
winnerPlayers[countWinners++] = participants[i];
}
*participantCount = countPlayers;
*winnerCount = countWinners;
}

View File

@ -117,9 +117,6 @@ typedef struct {
uint8_t playerDealer;
uint8_t playerSmallBlind;
uint8_t playerBigBlind;
poker2playerwinning_t winners[POKER_PLAYER_COUNT_MAX];
uint8_t winnerCount;
} poker2_t;
@ -162,4 +159,13 @@ void debug_WinnerFillRemaining(poker2playerwinning_t *winning);
void pokerWinnerGetForPlayer(
poker2_t *poker, poker2player_t *player, poker2playerwinning_t *winning
);
card_t debug_pokerWinnerCompare(poker2playerwinning_t *left, poker2playerwinning_t *right);
card_t debug_pokerWinnerCompare(poker2playerwinning_t *left, poker2playerwinning_t *right);
void pokerWinnerDetermineForPot(
poker2_t *poker,
poker2pot_t *pot,
poker2playerwinning_t winners[POKER_PLAYER_COUNT_MAX],
uint8_t winnerPlayers[POKER_PLAYER_COUNT_MAX],
uint8_t *winnerCount,
uint8_t participants[POKER_PLAYER_COUNT_MAX],
uint8_t *participantCount
);

View File

@ -1295,6 +1295,83 @@ void test_pokerWinnerCompare_should_CompareWinningHands(void) {
TEST_ASSERT_EQUAL_UINT8(0xFF, kicker);
}
void test_pokerWinnerDetermine_should_DecideTheWinnerCorrectly(void) {
poker2_t poker;
uint8_t p0i, p1i;
poker2player_t *p0;
poker2player_t *p1;
// Outputs
uint8_t winnerCount, participantCount;
uint8_t winnerPlayers[POKER_PLAYER_COUNT_MAX];
uint8_t participants[POKER_PLAYER_COUNT_MAX];
poker2playerwinning_t winners[POKER_PLAYER_COUNT_MAX];
// Set up the players
pokerInit(&poker);
p0i = pokerPlayerAdd(&poker);
p1i = pokerPlayerAdd(&poker);
p0 = poker.players + p0i;
p1 = poker.players + p1i;
pokerPotAddPlayer(poker.pots + 0, p0i);
pokerPotAddPlayer(poker.pots + 0, p1i);
pokerPlayerChipsAdd(p0, 10000);
pokerPlayerChipsAdd(p1, 10000);
// Set up the community
poker.communitySize = 5;
poker.community[0] = CARD_HEARTS_TWO;
poker.community[1] = CARD_DIAMONDS_NINE;
poker.community[2] = CARD_DIAMONDS_FOUR;
poker.community[3] = CARD_CLUBS_SIX;
poker.community[4] = CARD_HEARTS_EIGHT;
// Set up the player hands
p0->cardCount = 2;
p0->cards[0] = CARD_CLUBS_EIGHT;
p0->cards[1] = CARD_CLUBS_KING;//Higher, Kicker
p1->cardCount = 2;
p1->cards[0] = CARD_SPADES_EIGHT;
p1->cards[1] = CARD_CLUBS_QUEEN;//Low, not
// Run first test.
pokerWinnerDetermineForPot(
&poker, poker.pots + 0,
winners, winnerPlayers, &winnerCount,
participants, &participantCount
);
TEST_ASSERT_EQUAL_UINT8(1, winnerCount);
TEST_ASSERT_EQUAL_UINT8(2, participantCount);
TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_PAIR, winners[0].type);
TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_PAIR, winners[1].type);
TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_KING, winners[0].kicker);
TEST_ASSERT_EQUAL_UINT8(0, winnerPlayers[0]);
TEST_ASSERT_EQUAL_UINT8(0, participants[0]);
TEST_ASSERT_EQUAL_UINT8(1, participants[1]);
// ----- //
// Test a difference in hands.
p1->cards[1] = CARD_CLUBS_NINE;// Makes p1 have two pair.
pokerWinnerDetermineForPot(
&poker, poker.pots + 0,
winners, winnerPlayers, &winnerCount,
participants, &participantCount
);
TEST_ASSERT_EQUAL_UINT8(1, winnerCount);
TEST_ASSERT_EQUAL_UINT8(2, participantCount);
TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_PAIR, winners[0].type);
TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_TWO_PAIR, winners[1].type);
TEST_ASSERT_EQUAL_UINT8(1, winnerPlayers[0]);
TEST_ASSERT_EQUAL_UINT8(0, participants[0]);
TEST_ASSERT_EQUAL_UINT8(1, participants[1]);
}
int test_poker2() {
UNITY_BEGIN();
@ -1346,6 +1423,7 @@ int test_poker2() {
RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateStraightFlush);
RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateRoyalFlush);
RUN_TEST(test_pokerWinnerCompare_should_CompareWinningHands);
RUN_TEST(test_pokerWinnerDetermine_should_DecideTheWinnerCorrectly);
return UNITY_END();
}