From d632662520d7510e5bf4b18f8b08bf21e64c69b2 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 22 Jun 2021 20:18:53 -0700 Subject: [PATCH] Implemented royal and straight flushes --- include/dawn/poker/card.h | 137 ++++++++++++++++++-------------------- src/poker/card.c | 7 -- src/poker/round/winner.c | 53 +++++++++------ src/poker/round/winner.h | 2 +- 4 files changed, 97 insertions(+), 102 deletions(-) diff --git a/include/dawn/poker/card.h b/include/dawn/poker/card.h index db8e125d..a48f85f9 100644 --- a/include/dawn/poker/card.h +++ b/include/dawn/poker/card.h @@ -14,68 +14,64 @@ //////////////////////////////////////////////////////////////////////////////// // Aces -#define CARD_CLUBS_ACE 0x00 -#define CARD_CLUBS_TWO 0x01 -#define CARD_CLUBS_THREE 0x02 -#define CARD_CLUBS_FOUR 0x03 -#define CARD_CLUBS_FIVE 0x04 -#define CARD_CLUBS_SIX 0x05 -#define CARD_CLUBS_SEVEN 0x06 -#define CARD_CLUBS_EIGHT 0x07 -#define CARD_CLUBS_NINE 0x08 -#define CARD_CLUBS_TEN 0x09 -#define CARD_CLUBS_JACK 0x0A -#define CARD_CLUBS_QUEEN 0x0B -#define CARD_CLUBS_KING 0x0C +#define CARD_CLUBS_TWO 0x00 +#define CARD_CLUBS_THREE 0x01 +#define CARD_CLUBS_FOUR 0x02 +#define CARD_CLUBS_FIVE 0x03 +#define CARD_CLUBS_SIX 0x04 +#define CARD_CLUBS_SEVEN 0x05 +#define CARD_CLUBS_EIGHT 0x06 +#define CARD_CLUBS_NINE 0x07 +#define CARD_CLUBS_TEN 0x08 +#define CARD_CLUBS_JACK 0x09 +#define CARD_CLUBS_QUEEN 0x0A +#define CARD_CLUBS_KING 0x0B +#define CARD_CLUBS_ACE 0x0C // Diamonds -#define CARD_DIAMONDS_ACE 0x0D -#define CARD_DIAMONDS_TWO 0x0E -#define CARD_DIAMONDS_THREE 0x0F -#define CARD_DIAMONDS_FOUR 0x10 -#define CARD_DIAMONDS_FIVE 0x11 -#define CARD_DIAMONDS_SIX 0x12 -#define CARD_DIAMONDS_SEVEN 0x13 -#define CARD_DIAMONDS_EIGHT 0x14 -#define CARD_DIAMONDS_NINE 0x15 -#define CARD_DIAMONDS_TEN 0x16 -#define CARD_DIAMONDS_JACK 0x17 -#define CARD_DIAMONDS_QUEEN 0x18 -#define CARD_DIAMONDS_KING 0x19 +#define CARD_DIAMONDS_TWO 0x0D +#define CARD_DIAMONDS_THREE 0x0E +#define CARD_DIAMONDS_FOUR 0x0F +#define CARD_DIAMONDS_FIVE 0x10 +#define CARD_DIAMONDS_SIX 0x11 +#define CARD_DIAMONDS_SEVEN 0x12 +#define CARD_DIAMONDS_EIGHT 0x13 +#define CARD_DIAMONDS_NINE 0x14 +#define CARD_DIAMONDS_TEN 0x15 +#define CARD_DIAMONDS_JACK 0x16 +#define CARD_DIAMONDS_QUEEN 0x17 +#define CARD_DIAMONDS_KING 0x18 +#define CARD_DIAMONDS_ACE 0x19 // Hearts -#define CARD_HEARTS_ACE 0x1A -#define CARD_HEARTS_TWO 0x1B -#define CARD_HEARTS_THREE 0x1C -#define CARD_HEARTS_FOUR 0x1D -#define CARD_HEARTS_FIVE 0x1E -#define CARD_HEARTS_SIX 0x1F -#define CARD_HEARTS_SEVEN 0x20 -#define CARD_HEARTS_EIGHT 0x21 -#define CARD_HEARTS_NINE 0x22 -#define CARD_HEARTS_TEN 0x23 -#define CARD_HEARTS_JACK 0x24 -#define CARD_HEARTS_QUEEN 0x25 -#define CARD_HEARTS_KING 0x26 +#define CARD_HEARTS_TWO 0x1A +#define CARD_HEARTS_THREE 0x1B +#define CARD_HEARTS_FOUR 0x1C +#define CARD_HEARTS_FIVE 0x1D +#define CARD_HEARTS_SIX 0x1E +#define CARD_HEARTS_SEVEN 0x1F +#define CARD_HEARTS_EIGHT 0x20 +#define CARD_HEARTS_NINE 0x21 +#define CARD_HEARTS_TEN 0x22 +#define CARD_HEARTS_JACK 0x23 +#define CARD_HEARTS_QUEEN 0x24 +#define CARD_HEARTS_KING 0x25 +#define CARD_HEARTS_ACE 0x26 // Spades -#define CARD_SPADES_ACE 0x27 -#define CARD_SPADES_TWO 0x28 -#define CARD_SPADES_THREE 0x29 -#define CARD_SPADES_FOUR 0x2A -#define CARD_SPADES_FIVE 0x2B -#define CARD_SPADES_SIX 0x2C -#define CARD_SPADES_SEVEN 0x2D -#define CARD_SPADES_EIGHT 0x2E -#define CARD_SPADES_NINE 0x2F -#define CARD_SPADES_TEN 0x30 -#define CARD_SPADES_JACK 0x31 -#define CARD_SPADES_QUEEN 0x32 -#define CARD_SPADES_KING 0x33 - -// Special Cards -#define CARD_RED_JOKER 0x34 -#define CARD_BLACK_JOKER 0x35 +#define CARD_SPADES_TWO 0x27 +#define CARD_SPADES_THREE 0x28 +#define CARD_SPADES_FOUR 0x29 +#define CARD_SPADES_FIVE 0x2A +#define CARD_SPADES_SIX 0x2B +#define CARD_SPADES_SEVEN 0x2C +#define CARD_SPADES_EIGHT 0x2D +#define CARD_SPADES_NINE 0x2E +#define CARD_SPADES_TEN 0x2F +#define CARD_SPADES_JACK 0x30 +#define CARD_SPADES_QUEEN 0x31 +#define CARD_SPADES_KING 0x32 +#define CARD_SPADES_ACE 0x33 //////////////////////////////////////////////////////////////////////////////// // Suits @@ -88,19 +84,19 @@ //////////////////////////////////////////////////////////////////////////////// // Card numbers //////////////////////////////////////////////////////////////////////////////// -#define CARD_ACE 0x00 -#define CARD_TWO 0x01 -#define CARD_THREE 0x02 -#define CARD_FOUR 0x03 -#define CARD_FIVE 0x04 -#define CARD_SIX 0x05 -#define CARD_SEVEN 0x06 -#define CARD_EIGHT 0x07 -#define CARD_NINE 0x08 -#define CARD_TEN 0x09 -#define CARD_JACK 0x0A -#define CARD_QUEEN 0x0B -#define CARD_KING 0x0C +#define CARD_TWO 0x00 +#define CARD_THREE 0x01 +#define CARD_FOUR 0x02 +#define CARD_FIVE 0x03 +#define CARD_SIX 0x04 +#define CARD_SEVEN 0x05 +#define CARD_EIGHT 0x06 +#define CARD_NINE 0x07 +#define CARD_TEN 0x08 +#define CARD_JACK 0x09 +#define CARD_QUEEN 0x0A +#define CARD_KING 0x0B +#define CARD_ACE 0x0C /** Count of cards in each suit */ #define CARD_COUNT_PER_SUIT 13 @@ -108,8 +104,5 @@ /** Standard Card Deck Size */ #define CARD_DECK_SIZE 52 -/** Full Card Deck Size */ -#define CARD_DECK_SIZE_FULL 54 - /** Type Representing a card's id */ typedef uint8_t card_t; \ No newline at end of file diff --git a/src/poker/card.c b/src/poker/card.c index d1d27bb3..4c8955d3 100644 --- a/src/poker/card.c +++ b/src/poker/card.c @@ -38,13 +38,6 @@ int32_t _cardSorter(const void* left, const void* right) { uint8_t numberL = cardGetNumber(cardL); uint8_t numberR = cardGetNumber(cardR); - // Make Aces have higher weight (reversed) - if(numberL == CARD_ACE) { - return numberR == CARD_ACE ? 0 : -1; - } else if(numberR == CARD_ACE) { - return 1; - } - // Get the suit and the value of the card (reversed) return numberR - numberL; } diff --git a/src/poker/round/winner.c b/src/poker/round/winner.c index 769864fb..2675bc56 100644 --- a/src/poker/round/winner.c +++ b/src/poker/round/winner.c @@ -20,11 +20,14 @@ void pokerWinnerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards){ for(i = 0; i < player->cardCount; i++) { cards[i+poker->cardsFacing] = player->cards[i]; } + + // Sort by card value + cardHandSort(cards, poker->cardsFacing+player->cardCount); } winning_t pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) { winning_t winning; - uint8_t i, j; + uint8_t i, j, l; int32_t index; card_t card; uint8_t number, suit; @@ -40,42 +43,48 @@ winning_t pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) { // TESTING HAND winning.full[0] = CARD_CLUBS_ACE; - winning.full[1] = CARD_CLUBS_QUEEN; - winning.full[2] = CARD_CLUBS_TEN; + winning.full[1] = CARD_CLUBS_TWO; + winning.full[2] = CARD_CLUBS_THREE; winning.full[3] = CARD_SPADES_FOUR; - winning.full[4] = CARD_CLUBS_KING; - winning.full[5] = CARD_DIAMONDS_NINE; + winning.full[4] = CARD_CLUBS_FOUR; + winning.full[5] = CARD_CLUBS_FIVE; winning.full[6] = CARD_CLUBS_JACK; //////////////////////// Now look for the winning set //////////////////////// - // Ace High Royal flush. - for(i = 0; i < winning.count; i++) { - // Look for Ace + // Royal / Straight Flush + for(i = 0; i < winning.size; i++) { card = winning.full[i]; number = cardGetNumber(card); - if(number != CARD_ACE) continue; + if(number < CARD_FIVE) continue; + suit = cardGetSuit(card); + winning.count = 1; // Now look for the matching cards (Reverse order to order from A to 10) - for(j = 0; j < 4; j++) { - index = cardContains(winning.full,winning.size,cardGet(CARD_KING-j,suit)); - if(index == -1) { - winning.count = 0; - break; - } - winning.set[j+1] = index; + for(j = 1; j <= 4; j++) { + l = card == CARD_FIVE && j == 4 ? CARD_ACE : number - j;//Ace low. + index = cardContains(winning.full, winning.size, cardGet(l, suit)); + if(index == -1) break; + winning.set[j] = index; winning.count++; } - // Check if has all necessary cards. Adds the Ace to the array. - if(winning.count > 0) { - winning.set[0] = card; - winning.count++; - winning.type = POKER_WINNING_TYPE_ROYAL_FLUSH; + // Check if has all necessary cards. + if(winning.count < POKER_WINNING_SET_SIZE) continue; + + // Add self to array + winning.set[0] = card; + winning.type = ( + number == CARD_ACE ? POKER_WINNING_TYPE_ROYAL_FLUSH : + POKER_WINNING_TYPE_STRAIGHT_FLUSH + ); + if(winning.type == POKER_WINNING_TYPE_ROYAL_FLUSH) { printf("Royal Flush\n"); - return winning; + } else { + printf("Straight Flush\n"); } + return winning; } // High card (worst) diff --git a/src/poker/round/winner.h b/src/poker/round/winner.h index ff8b06e4..c8d438bf 100644 --- a/src/poker/round/winner.h +++ b/src/poker/round/winner.h @@ -11,7 +11,7 @@ #include "../card.h" #define POKER_WINNING_TYPE_ROYAL_FLUSH 0x01 -#define POKER_WINNING_TYPE_HIGH_CARD 0x00 +#define POKER_WINNING_TYPE_STRAIGHT_FLUSH 0x02 #define POKER_WINNING_SET_SIZE 5