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

@ -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;
}

View File

@ -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)

View File

@ -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