From 3b334c602c8ea42bfecac3e194c86ffd45a39178 Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Wed, 6 Oct 2021 07:24:59 -0700
Subject: [PATCH] Fixed some poker bugs.
---
src/poker2/poker.c | 3 +-
test/poker2/poker.c | 208 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 210 insertions(+), 1 deletion(-)
diff --git a/src/poker2/poker.c b/src/poker2/poker.c
index a7db5b90..71f28d96 100644
--- a/src/poker2/poker.c
+++ b/src/poker2/poker.c
@@ -427,6 +427,7 @@ void pokerWinnerGetForPlayer(
if(winning->setSize < POKER_WINNING_SET_SIZE) continue;
winning->set[0] = winning->full[i];
winning->type = POKER_WINNING_TYPE_STRAIGHT;
+ debug_WinnerFillRemaining(winning);
return;
}
@@ -458,7 +459,7 @@ void pokerWinnerGetForPlayer(
if(pairCount != 2) continue;
for(j = 0; j < pairCount; j++) {
- winning->set[j] = winning->full[winning->setSize + pairs[j]];
+ winning->set[winning->setSize+j] = winning->full[pairs[j]];
}
// arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set+winning->setSize);
winning->setSize += pairCount;
diff --git a/test/poker2/poker.c b/test/poker2/poker.c
index bc933286..fa1f9310 100644
--- a/test/poker2/poker.c
+++ b/test/poker2/poker.c
@@ -1036,6 +1036,209 @@ void test_pokerWinnerGetForPlayer_should_CalculateTwoPair(void) {
TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_QUEEN, winning.set[4]);
}
+void test_pokerWinnerGetForPlayer_should_CalculateThreeOfAKind(void) {
+ poker2_t poker;
+ uint8_t i;
+ poker2player_t *player;
+ poker2playerwinning_t winning;
+
+ pokerInit(&poker);
+ i = pokerPlayerAdd(&poker);
+ player = poker.players + i;
+
+ poker.communitySize = 5;
+ poker.community[0] = CARD_HEARTS_ACE;
+ poker.community[1] = CARD_CLUBS_THREE;
+ poker.community[2] = CARD_SPADES_TWO;
+ poker.community[3] = CARD_SPADES_QUEEN;
+ poker.community[4] = CARD_HEARTS_SIX;
+ player->cardCount = 2;
+ player->cards[0] = CARD_CLUBS_TWO;
+ player->cards[1] = CARD_DIAMONDS_TWO;
+
+ pokerWinnerGetForPlayer(&poker, player, &winning);
+ TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_THREE_OF_A_KIND, winning.type);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_TWO, winning.set[0]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_DIAMONDS_TWO, winning.set[1]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_ACE, winning.set[2]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_QUEEN, winning.set[3]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_TWO, winning.set[4]);
+}
+
+void test_pokerWinnerGetForPlayer_should_CalculateAStraight(void) {
+ poker2_t poker;
+ uint8_t i;
+ poker2player_t *player;
+ poker2playerwinning_t winning;
+
+ pokerInit(&poker);
+ i = pokerPlayerAdd(&poker);
+ player = poker.players + i;
+
+ poker.communitySize = 5;
+ poker.community[0] = CARD_HEARTS_ACE;//0 - 3
+ poker.community[1] = CARD_CLUBS_THREE;//2 - 1
+ poker.community[2] = CARD_SPADES_THREE;//Hmm?
+ poker.community[3] = CARD_SPADES_QUEEN;
+ poker.community[4] = CARD_HEARTS_FOUR;//3 - 4
+ player->cardCount = 2;
+ player->cards[0] = CARD_CLUBS_TWO;//1 - 0
+ player->cards[1] = CARD_DIAMONDS_FIVE;//4 - 2
+
+ pokerWinnerGetForPlayer(&poker, player, &winning);
+ TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_STRAIGHT, winning.type);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_THREE, winning.set[0]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_TWO, winning.set[1]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_DIAMONDS_FIVE, winning.set[2]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_ACE, winning.set[3]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_FOUR, winning.set[4]);
+}
+
+void test_pokerWinnerGetForPlayer_should_CalculateAFlush(void) {
+ poker2_t poker;
+ uint8_t i;
+ poker2player_t *player;
+ poker2playerwinning_t winning;
+
+ pokerInit(&poker);
+ i = pokerPlayerAdd(&poker);
+ player = poker.players + i;
+
+ poker.communitySize = 5;
+ poker.community[0] = CARD_HEARTS_ACE;
+ poker.community[1] = CARD_SPADES_SIX;
+ poker.community[2] = CARD_SPADES_THREE;
+ poker.community[3] = CARD_SPADES_QUEEN;
+ poker.community[4] = CARD_HEARTS_NINE;
+ player->cardCount = 2;
+ player->cards[0] = CARD_SPADES_TWO;
+ player->cards[1] = CARD_SPADES_KING;
+
+ pokerWinnerGetForPlayer(&poker, player, &winning);
+ TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_FLUSH, winning.type);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_TWO, winning.set[0]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_DIAMONDS_TWO, winning.set[1]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_ACE, winning.set[2]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_ACE, winning.set[3]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_TWO, winning.set[4]);
+}
+
+void test_pokerWinnerGet_should_CalculateFullHouse(void) {
+ poker2_t poker;
+ uint8_t i;
+ poker2player_t *player;
+ poker2playerwinning_t winning;
+
+ pokerInit(&poker);
+ i = pokerPlayerAdd(&poker);
+ player = poker.players + i;
+
+ poker.communitySize = 5;
+ poker.community[0] = CARD_HEARTS_ACE;
+ poker.community[1] = CARD_SPADES_SIX;
+ poker.community[2] = CARD_SPADES_THREE;
+ poker.community[3] = CARD_SPADES_QUEEN;
+ poker.community[4] = CARD_HEARTS_NINE;
+ player->cardCount = 2;
+ player->cards[0] = CARD_SPADES_TWO;
+ player->cards[1] = CARD_SPADES_KING;
+
+ pokerWinnerGetForPlayer(&poker, player, &winning);
+ TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_FULL_HOUSE, winning.type);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_TWO, winning.set[0]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_DIAMONDS_TWO, winning.set[1]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_ACE, winning.set[2]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_ACE, winning.set[3]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_TWO, winning.set[4]);
+}
+
+void test_pokerWinnerGetForPlayer_should_CalculateFourOfAKind(void) {
+ poker2_t poker;
+ uint8_t i;
+ poker2player_t *player;
+ poker2playerwinning_t winning;
+
+ pokerInit(&poker);
+ i = pokerPlayerAdd(&poker);
+ player = poker.players + i;
+
+ poker.communitySize = 5;
+ poker.community[0] = CARD_HEARTS_ACE;
+ poker.community[1] = CARD_SPADES_SIX;
+ poker.community[2] = CARD_SPADES_THREE;
+ poker.community[3] = CARD_SPADES_QUEEN;
+ poker.community[4] = CARD_HEARTS_NINE;
+ player->cardCount = 2;
+ player->cards[0] = CARD_SPADES_TWO;
+ player->cards[1] = CARD_SPADES_KING;
+
+ pokerWinnerGetForPlayer(&poker, player, &winning);
+ TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_FOUR_OF_A_KIND, winning.type);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_TWO, winning.set[0]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_DIAMONDS_TWO, winning.set[1]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_ACE, winning.set[2]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_ACE, winning.set[3]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_TWO, winning.set[4]);
+}
+
+void test_pokerWinnerGetForPlayer_should_CalculateStraightFlush(void) {
+ poker2_t poker;
+ uint8_t i;
+ poker2player_t *player;
+ poker2playerwinning_t winning;
+
+ pokerInit(&poker);
+ i = pokerPlayerAdd(&poker);
+ player = poker.players + i;
+
+ poker.communitySize = 5;
+ poker.community[0] = CARD_HEARTS_ACE;
+ poker.community[1] = CARD_SPADES_SIX;
+ poker.community[2] = CARD_SPADES_THREE;
+ poker.community[3] = CARD_SPADES_QUEEN;
+ poker.community[4] = CARD_HEARTS_NINE;
+ player->cardCount = 2;
+ player->cards[0] = CARD_SPADES_TWO;
+ player->cards[1] = CARD_SPADES_KING;
+
+ pokerWinnerGetForPlayer(&poker, player, &winning);
+ TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_STRAIGHT_FLUSH, winning.type);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_TWO, winning.set[0]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_DIAMONDS_TWO, winning.set[1]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_ACE, winning.set[2]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_ACE, winning.set[3]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_TWO, winning.set[4]);
+}
+
+void test_pokerWinnerGetForPlayer_should_CalculateRoyalFlush(void) {
+ poker2_t poker;
+ uint8_t i;
+ poker2player_t *player;
+ poker2playerwinning_t winning;
+
+ pokerInit(&poker);
+ i = pokerPlayerAdd(&poker);
+ player = poker.players + i;
+
+ poker.communitySize = 5;
+ poker.community[0] = CARD_HEARTS_ACE;
+ poker.community[1] = CARD_SPADES_SIX;
+ poker.community[2] = CARD_SPADES_THREE;
+ poker.community[3] = CARD_SPADES_QUEEN;
+ poker.community[4] = CARD_HEARTS_NINE;
+ player->cardCount = 2;
+ player->cards[0] = CARD_SPADES_TWO;
+ player->cards[1] = CARD_SPADES_KING;
+
+ pokerWinnerGetForPlayer(&poker, player, &winning);
+ TEST_ASSERT_EQUAL_UINT8(POKER_WINNING_TYPE_ROYAL_FLUSH, winning.type);
+ TEST_ASSERT_EQUAL_UINT8(CARD_CLUBS_TWO, winning.set[0]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_DIAMONDS_TWO, winning.set[1]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_HEARTS_ACE, winning.set[2]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_ACE, winning.set[3]);
+ TEST_ASSERT_EQUAL_UINT8(CARD_SPADES_TWO, winning.set[4]);
+}
+
int test_poker2() {
UNITY_BEGIN();
@@ -1079,6 +1282,11 @@ int test_poker2() {
RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateHighCard);
RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculatePair);
RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateTwoPair);
+ RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateThreeOfAKind);
+ RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateAStraight);
+ RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateAFlush);
+ RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateFourOfAKind);
+ RUN_TEST(test_pokerWinnerGetForPlayer_should_CalculateRoyalFlush);
return UNITY_END();
}
\ No newline at end of file