Implemented royal and straight flushes

This commit is contained in:
2021-06-22 20:18:53 -07:00
parent 7e39195433
commit d632662520
4 changed files with 97 additions and 102 deletions

View File

@ -14,68 +14,64 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Aces // Aces
#define CARD_CLUBS_ACE 0x00 #define CARD_CLUBS_TWO 0x00
#define CARD_CLUBS_TWO 0x01 #define CARD_CLUBS_THREE 0x01
#define CARD_CLUBS_THREE 0x02 #define CARD_CLUBS_FOUR 0x02
#define CARD_CLUBS_FOUR 0x03 #define CARD_CLUBS_FIVE 0x03
#define CARD_CLUBS_FIVE 0x04 #define CARD_CLUBS_SIX 0x04
#define CARD_CLUBS_SIX 0x05 #define CARD_CLUBS_SEVEN 0x05
#define CARD_CLUBS_SEVEN 0x06 #define CARD_CLUBS_EIGHT 0x06
#define CARD_CLUBS_EIGHT 0x07 #define CARD_CLUBS_NINE 0x07
#define CARD_CLUBS_NINE 0x08 #define CARD_CLUBS_TEN 0x08
#define CARD_CLUBS_TEN 0x09 #define CARD_CLUBS_JACK 0x09
#define CARD_CLUBS_JACK 0x0A #define CARD_CLUBS_QUEEN 0x0A
#define CARD_CLUBS_QUEEN 0x0B #define CARD_CLUBS_KING 0x0B
#define CARD_CLUBS_KING 0x0C #define CARD_CLUBS_ACE 0x0C
// Diamonds // Diamonds
#define CARD_DIAMONDS_ACE 0x0D #define CARD_DIAMONDS_TWO 0x0D
#define CARD_DIAMONDS_TWO 0x0E #define CARD_DIAMONDS_THREE 0x0E
#define CARD_DIAMONDS_THREE 0x0F #define CARD_DIAMONDS_FOUR 0x0F
#define CARD_DIAMONDS_FOUR 0x10 #define CARD_DIAMONDS_FIVE 0x10
#define CARD_DIAMONDS_FIVE 0x11 #define CARD_DIAMONDS_SIX 0x11
#define CARD_DIAMONDS_SIX 0x12 #define CARD_DIAMONDS_SEVEN 0x12
#define CARD_DIAMONDS_SEVEN 0x13 #define CARD_DIAMONDS_EIGHT 0x13
#define CARD_DIAMONDS_EIGHT 0x14 #define CARD_DIAMONDS_NINE 0x14
#define CARD_DIAMONDS_NINE 0x15 #define CARD_DIAMONDS_TEN 0x15
#define CARD_DIAMONDS_TEN 0x16 #define CARD_DIAMONDS_JACK 0x16
#define CARD_DIAMONDS_JACK 0x17 #define CARD_DIAMONDS_QUEEN 0x17
#define CARD_DIAMONDS_QUEEN 0x18 #define CARD_DIAMONDS_KING 0x18
#define CARD_DIAMONDS_KING 0x19 #define CARD_DIAMONDS_ACE 0x19
// Hearts // Hearts
#define CARD_HEARTS_ACE 0x1A #define CARD_HEARTS_TWO 0x1A
#define CARD_HEARTS_TWO 0x1B #define CARD_HEARTS_THREE 0x1B
#define CARD_HEARTS_THREE 0x1C #define CARD_HEARTS_FOUR 0x1C
#define CARD_HEARTS_FOUR 0x1D #define CARD_HEARTS_FIVE 0x1D
#define CARD_HEARTS_FIVE 0x1E #define CARD_HEARTS_SIX 0x1E
#define CARD_HEARTS_SIX 0x1F #define CARD_HEARTS_SEVEN 0x1F
#define CARD_HEARTS_SEVEN 0x20 #define CARD_HEARTS_EIGHT 0x20
#define CARD_HEARTS_EIGHT 0x21 #define CARD_HEARTS_NINE 0x21
#define CARD_HEARTS_NINE 0x22 #define CARD_HEARTS_TEN 0x22
#define CARD_HEARTS_TEN 0x23 #define CARD_HEARTS_JACK 0x23
#define CARD_HEARTS_JACK 0x24 #define CARD_HEARTS_QUEEN 0x24
#define CARD_HEARTS_QUEEN 0x25 #define CARD_HEARTS_KING 0x25
#define CARD_HEARTS_KING 0x26 #define CARD_HEARTS_ACE 0x26
// Spades // Spades
#define CARD_SPADES_ACE 0x27 #define CARD_SPADES_TWO 0x27
#define CARD_SPADES_TWO 0x28 #define CARD_SPADES_THREE 0x28
#define CARD_SPADES_THREE 0x29 #define CARD_SPADES_FOUR 0x29
#define CARD_SPADES_FOUR 0x2A #define CARD_SPADES_FIVE 0x2A
#define CARD_SPADES_FIVE 0x2B #define CARD_SPADES_SIX 0x2B
#define CARD_SPADES_SIX 0x2C #define CARD_SPADES_SEVEN 0x2C
#define CARD_SPADES_SEVEN 0x2D #define CARD_SPADES_EIGHT 0x2D
#define CARD_SPADES_EIGHT 0x2E #define CARD_SPADES_NINE 0x2E
#define CARD_SPADES_NINE 0x2F #define CARD_SPADES_TEN 0x2F
#define CARD_SPADES_TEN 0x30 #define CARD_SPADES_JACK 0x30
#define CARD_SPADES_JACK 0x31 #define CARD_SPADES_QUEEN 0x31
#define CARD_SPADES_QUEEN 0x32 #define CARD_SPADES_KING 0x32
#define CARD_SPADES_KING 0x33 #define CARD_SPADES_ACE 0x33
// Special Cards
#define CARD_RED_JOKER 0x34
#define CARD_BLACK_JOKER 0x35
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Suits // Suits
@ -88,19 +84,19 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Card numbers // Card numbers
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#define CARD_ACE 0x00 #define CARD_TWO 0x00
#define CARD_TWO 0x01 #define CARD_THREE 0x01
#define CARD_THREE 0x02 #define CARD_FOUR 0x02
#define CARD_FOUR 0x03 #define CARD_FIVE 0x03
#define CARD_FIVE 0x04 #define CARD_SIX 0x04
#define CARD_SIX 0x05 #define CARD_SEVEN 0x05
#define CARD_SEVEN 0x06 #define CARD_EIGHT 0x06
#define CARD_EIGHT 0x07 #define CARD_NINE 0x07
#define CARD_NINE 0x08 #define CARD_TEN 0x08
#define CARD_TEN 0x09 #define CARD_JACK 0x09
#define CARD_JACK 0x0A #define CARD_QUEEN 0x0A
#define CARD_QUEEN 0x0B #define CARD_KING 0x0B
#define CARD_KING 0x0C #define CARD_ACE 0x0C
/** Count of cards in each suit */ /** Count of cards in each suit */
#define CARD_COUNT_PER_SUIT 13 #define CARD_COUNT_PER_SUIT 13
@ -108,8 +104,5 @@
/** Standard Card Deck Size */ /** Standard Card Deck Size */
#define CARD_DECK_SIZE 52 #define CARD_DECK_SIZE 52
/** Full Card Deck Size */
#define CARD_DECK_SIZE_FULL 54
/** Type Representing a card's id */ /** Type Representing a card's id */
typedef uint8_t card_t; typedef uint8_t card_t;

View File

@ -38,13 +38,6 @@ int32_t _cardSorter(const void* left, const void* right) {
uint8_t numberL = cardGetNumber(cardL); uint8_t numberL = cardGetNumber(cardL);
uint8_t numberR = cardGetNumber(cardR); 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) // Get the suit and the value of the card (reversed)
return numberR - numberL; return numberR - numberL;
} }

View File

@ -20,11 +20,14 @@ void pokerWinnerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards){
for(i = 0; i < player->cardCount; i++) { for(i = 0; i < player->cardCount; i++) {
cards[i+poker->cardsFacing] = player->cards[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 pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) {
winning_t winning; winning_t winning;
uint8_t i, j; uint8_t i, j, l;
int32_t index; int32_t index;
card_t card; card_t card;
uint8_t number, suit; uint8_t number, suit;
@ -40,42 +43,48 @@ winning_t pokerWinnerGetStatus(poker_t *poker, pokerplayer_t *player) {
// TESTING HAND // TESTING HAND
winning.full[0] = CARD_CLUBS_ACE; winning.full[0] = CARD_CLUBS_ACE;
winning.full[1] = CARD_CLUBS_QUEEN; winning.full[1] = CARD_CLUBS_TWO;
winning.full[2] = CARD_CLUBS_TEN; winning.full[2] = CARD_CLUBS_THREE;
winning.full[3] = CARD_SPADES_FOUR; winning.full[3] = CARD_SPADES_FOUR;
winning.full[4] = CARD_CLUBS_KING; winning.full[4] = CARD_CLUBS_FOUR;
winning.full[5] = CARD_DIAMONDS_NINE; winning.full[5] = CARD_CLUBS_FIVE;
winning.full[6] = CARD_CLUBS_JACK; winning.full[6] = CARD_CLUBS_JACK;
//////////////////////// Now look for the winning set //////////////////////// //////////////////////// Now look for the winning set ////////////////////////
// Ace High Royal flush. // Royal / Straight Flush
for(i = 0; i < winning.count; i++) { for(i = 0; i < winning.size; i++) {
// Look for Ace
card = winning.full[i]; card = winning.full[i];
number = cardGetNumber(card); number = cardGetNumber(card);
if(number != CARD_ACE) continue; if(number < CARD_FIVE) continue;
suit = cardGetSuit(card); suit = cardGetSuit(card);
winning.count = 1;
// Now look for the matching cards (Reverse order to order from A to 10) // Now look for the matching cards (Reverse order to order from A to 10)
for(j = 0; j < 4; j++) { for(j = 1; j <= 4; j++) {
index = cardContains(winning.full,winning.size,cardGet(CARD_KING-j,suit)); l = card == CARD_FIVE && j == 4 ? CARD_ACE : number - j;//Ace low.
if(index == -1) { index = cardContains(winning.full, winning.size, cardGet(l, suit));
winning.count = 0; if(index == -1) break;
break; winning.set[j] = index;
}
winning.set[j+1] = index;
winning.count++; winning.count++;
} }
// Check if has all necessary cards. Adds the Ace to the array. // Check if has all necessary cards.
if(winning.count > 0) { if(winning.count < POKER_WINNING_SET_SIZE) continue;
winning.set[0] = card;
winning.count++; // Add self to array
winning.type = POKER_WINNING_TYPE_ROYAL_FLUSH; 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"); printf("Royal Flush\n");
return winning; } else {
printf("Straight Flush\n");
} }
return winning;
} }
// High card (worst) // High card (worst)

View File

@ -11,7 +11,7 @@
#include "../card.h" #include "../card.h"
#define POKER_WINNING_TYPE_ROYAL_FLUSH 0x01 #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 #define POKER_WINNING_SET_SIZE 5