Changed winning to allow pointer to winning definiton for arraying.

This commit is contained in:
2021-06-24 15:40:11 -07:00
parent 2b772816d8
commit bf0de5bfa8
4 changed files with 100 additions and 92 deletions

View File

@ -55,8 +55,9 @@ void pokerPlayerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards){
cardHandSort(cards, poker->cardsFacing+player->cardCount); cardHandSort(cards, poker->cardsFacing+player->cardCount);
} }
pokerwinning_t pokerPlayerGetWinning(poker_t *poker, pokerplayer_t *player) { pokerwinning_t pokerPlayerGetWinning(
pokerwinning_t winning; poker_t *poker, pokerplayer_t *player, pokerwinning_t *winning
) {
uint8_t i, j, l; uint8_t i, j, l;
int32_t index; int32_t index;
card_t card; card_t card;
@ -64,170 +65,172 @@ pokerwinning_t pokerPlayerGetWinning(poker_t *poker, pokerplayer_t *player) {
int32_t pairs[CARD_SUIT_COUNT]; int32_t pairs[CARD_SUIT_COUNT];
// Get the full poker hand (should be a 7 card hand) // Get the full poker hand (should be a 7 card hand)
winning.size = poker->cardsFacing + player->cardCount; winning->size = poker->cardsFacing + player->cardCount;
pokerPlayerGetFullHand(poker, player, winning.full); pokerPlayerGetFullHand(poker, player, winning->full);
// Reset the winning status. // Reset the winning status.
winning.count = 0; winning->count = 0;
winning.type = 0x00; winning->type = 0x00;
for(i = 0; i < POKER_WINNING_SET_SIZE; i++) winning.set[i] = -1; for(i = 0; i < POKER_WINNING_SET_SIZE; i++) winning->set[i] = -1;
//////////////////////// Now look for the winning set //////////////////////// //////////////////////// Now look for the winning set ////////////////////////
// Royal / Straight Flush // Royal / Straight Flush
for(i = 0; i < winning.size; i++) { for(i = 0; i < winning->size; i++) {
card = winning.full[i]; card = winning->full[i];
number = cardGetNumber(card); number = cardGetNumber(card);
if(number < CARD_FIVE) continue; if(number < CARD_FIVE) continue;
suit = cardGetSuit(card); suit = cardGetSuit(card);
winning.count = 1; 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 = 1; j <= 4; j++) { for(j = 1; j <= 4; j++) {
l = number == CARD_FIVE && j == 4 ? CARD_ACE : number - j;//Ace low. l = number == CARD_FIVE && j == 4 ? CARD_ACE : number - j;//Ace low.
index = cardContains(winning.full, winning.size, cardGet(l, suit)); index = cardContains(winning->full, winning->size, cardGet(l, suit));
if(index == -1) break; if(index == -1) break;
winning.set[j] = index; winning->set[j] = index;
winning.count++; winning->count++;
} }
// Check if has all necessary cards. // Check if has all necessary cards.
if(winning.count < POKER_WINNING_SET_SIZE) continue; if(winning->count < POKER_WINNING_SET_SIZE) continue;
// Add self to array // Add self to array
winning.set[0] = i; winning->set[0] = i;
winning.type = ( winning->type = (
number == CARD_ACE ? POKER_WINNING_TYPE_ROYAL_FLUSH : number == CARD_ACE ? POKER_WINNING_TYPE_ROYAL_FLUSH :
POKER_WINNING_TYPE_STRAIGHT_FLUSH POKER_WINNING_TYPE_STRAIGHT_FLUSH
); );
return winning; return;
} }
// Four of a kind. // Four of a kind.
winning.count = 0; winning->count = 0;
for(i = 0; i < winning.size; i++) { for(i = 0; i < winning->size; i++) {
card = winning.full[i]; card = winning->full[i];
number = cardGetNumber(card); number = cardGetNumber(card);
pairCount = cardCountPairs(winning.full, winning.size, number, pairs); pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
if(pairCount < CARD_SUIT_COUNT) continue; if(pairCount < CARD_SUIT_COUNT) continue;
winning.count = pairCount; winning->count = pairCount;
arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set); arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set);
winning.type = POKER_WINNING_TYPE_FOUR_OF_A_KIND; winning->type = POKER_WINNING_TYPE_FOUR_OF_A_KIND;
pokerPlayerWinningFillRemaining(&winning); pokerPlayerWinningFillRemaining(winning);
return winning; return;
} }
// Full House // Full House
winning.count = 0; winning->count = 0;
for(i = 0; i < winning.size; i++) { for(i = 0; i < winning->size; i++) {
index = i;// Check we haven't already added this card. index = i;// Check we haven't already added this card.
if(arrayContains(sizeof(int32_t),winning.set,winning.count,&index))continue; if(arrayContains(sizeof(int32_t),winning->set,winning->count,&index))continue;
card = winning.full[i]; card = winning->full[i];
number = cardGetNumber(card); number = cardGetNumber(card);
pairCount = cardCountPairs(winning.full, winning.size, number, pairs); pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
// Did we find either two pair or three pair? // Did we find either two pair or three pair?
if(pairCount != 2 && pairCount != 3) continue; if(pairCount != 2 && pairCount != 3) continue;
if(winning.count == 3) pairCount = 2;//Clamp to 5 max. if(winning->count == 3) pairCount = 2;//Clamp to 5 max.
arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set+winning.count); arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set+winning->count);
winning.count += pairCount; winning->count += pairCount;
if(winning.count != POKER_WINNING_SET_SIZE) continue; if(winning->count != POKER_WINNING_SET_SIZE) continue;
winning.type = POKER_WINNING_TYPE_FULL_HOUSE; winning->type = POKER_WINNING_TYPE_FULL_HOUSE;
printf("Full House\n"); printf("Full House\n");
return winning; return;
} }
// Flush (5 same suit) // Flush (5 same suit)
for(i = 0; i < winning.size; i++) { for(i = 0; i < winning->size; i++) {
card = winning.full[i]; card = winning->full[i];
suit = cardGetSuit(card); suit = cardGetSuit(card);
winning.count = 1; winning->count = 1;
for(j = i+1; j < winning.size; j++) { for(j = i+1; j < winning->size; j++) {
if(cardGetSuit(winning.full[j]) != suit) continue; if(cardGetSuit(winning->full[j]) != suit) continue;
winning.set[winning.count] = j; winning->set[winning->count] = j;
winning.count++; winning->count++;
if(winning.count == POKER_WINNING_SET_SIZE) break; if(winning->count == POKER_WINNING_SET_SIZE) break;
} }
if(winning.count < POKER_WINNING_SET_SIZE) continue; if(winning->count < POKER_WINNING_SET_SIZE) continue;
winning.set[0] = i; winning->set[0] = i;
winning.type = POKER_WINNING_TYPE_FLUSH; winning->type = POKER_WINNING_TYPE_FLUSH;
return winning; return;
} }
// Straight (sequence any suit) // Straight (sequence any suit)
winning.count = 0; winning->count = 0;
for(i = 0; i < winning.size; i++) { for(i = 0; i < winning->size; i++) {
card = winning.full[i]; card = winning->full[i];
number = cardGetNumber(card); number = cardGetNumber(card);
if(number < CARD_FIVE) continue; if(number < CARD_FIVE) continue;
winning.count = 1; winning->count = 1;
for(j = 1; j <= 4; j++) { for(j = 1; j <= 4; j++) {
l = number == CARD_FIVE && j == 4 ? CARD_ACE : number - j;//Ace low. l = number == CARD_FIVE && j == 4 ? CARD_ACE : number - j;//Ace low.
index = cardContainsNumber(winning.full, winning.size, l); index = cardContainsNumber(winning->full, winning->size, l);
if(index == -1) break; if(index == -1) break;
winning.set[j] = index; winning->set[j] = index;
winning.count++; winning->count++;
} }
// Check if has all necessary cards. // Check if has all necessary cards.
if(winning.count < POKER_WINNING_SET_SIZE) continue; if(winning->count < POKER_WINNING_SET_SIZE) continue;
winning.set[0] = i; winning->set[0] = i;
winning.type = POKER_WINNING_TYPE_STRAIGHT; winning->type = POKER_WINNING_TYPE_STRAIGHT;
return winning; return;
} }
// Three of a kind // Three of a kind
winning.count = 0; winning->count = 0;
for(i = 0; i < winning.size; i++) { for(i = 0; i < winning->size; i++) {
card = winning.full[i]; card = winning->full[i];
number = cardGetNumber(card); number = cardGetNumber(card);
pairCount = cardCountPairs(winning.full, winning.size, number, pairs); pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
if(pairCount != 3) continue; if(pairCount != 3) continue;
winning.count = pairCount; winning->count = pairCount;
arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set); arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set);
winning.type = POKER_WINNING_TYPE_THREE_OF_A_KIND; winning->type = POKER_WINNING_TYPE_THREE_OF_A_KIND;
pokerPlayerWinningFillRemaining(&winning); pokerPlayerWinningFillRemaining(winning);
return winning; return;
} }
// Two Pair // Two Pair
winning.count = 0; winning->count = 0;
for(i = 0; i < winning.size; i++) { for(i = 0; i < winning->size; i++) {
index = i;// Check we haven't already added this card. index = i;// Check we haven't already added this card.
if(arrayContains(sizeof(int32_t),winning.set,winning.count,&index))continue; if(arrayContains(sizeof(int32_t), winning->set, winning->count, &index)) {
continue;
}
card = winning.full[i]; card = winning->full[i];
number = cardGetNumber(card); number = cardGetNumber(card);
pairCount = cardCountPairs(winning.full, winning.size, number, pairs); pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
if(pairCount != 2) continue; if(pairCount != 2) continue;
arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set+winning.count); arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set+winning->count);
winning.count += pairCount; winning->count += pairCount;
if(winning.count != 4) continue; if(winning->count != 4) continue;
winning.type = POKER_WINNING_TYPE_TWO_PAIR; winning->type = POKER_WINNING_TYPE_TWO_PAIR;
pokerPlayerWinningFillRemaining(&winning); pokerPlayerWinningFillRemaining(winning);
return winning; return;
} }
// Pair // Pair
if(winning.count == 2) { if(winning->count == 2) {
winning.type = POKER_WINNING_TYPE_PAIR; winning->type = POKER_WINNING_TYPE_PAIR;
pokerPlayerWinningFillRemaining(&winning); pokerPlayerWinningFillRemaining(winning);
return winning; return;
} }
// High card // High card
winning.count = 0; winning->count = 0;
pokerPlayerWinningFillRemaining(&winning); pokerPlayerWinningFillRemaining(winning);
winning.type = POKER_WINNNIG_TYPE_HIGH_CARD; winning->type = POKER_WINNNIG_TYPE_HIGH_CARD;
return winning; return;
} }
void pokerPlayerWinningFillRemaining(pokerwinning_t *winning) { void pokerPlayerWinningFillRemaining(pokerwinning_t *winning) {

View File

@ -66,9 +66,12 @@ void pokerPlayerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards);
* *
* @param poker Poker game instance. * @param poker Poker game instance.
* @param player Player game instance. * @param player Player game instance.
* @param winning Pointer to the poker winning to fill out.
* @return The winning state for this player. * @return The winning state for this player.
*/ */
pokerwinning_t pokerPlayerGetWinning(poker_t *poker, pokerplayer_t *player); pokerwinning_t pokerPlayerGetWinning(
poker_t *poker, pokerplayer_t *player, pokerwinning_t *winning
);
/** /**
* Fills the remaining cards for a given poker player winning hand. * Fills the remaining cards for a given poker player winning hand.

View File

@ -14,6 +14,7 @@ void pokerDealInit(poker_t *poker) {
// Shuffle the deck // Shuffle the deck
arrayShuffle(sizeof(card_t), poker->deck, CARD_DECK_SIZE); arrayShuffle(sizeof(card_t), poker->deck, CARD_DECK_SIZE);
arrayShuffle(sizeof(card_t), poker->deck, CARD_DECK_SIZE);
// Deal 2 card to each player // Deal 2 card to each player
pokerPlayerDealAll(poker, POKER_DEAL_CARD_EACH); pokerPlayerDealAll(poker, POKER_DEAL_CARD_EACH);

View File

@ -10,18 +10,19 @@
void pokerWinnerInit(poker_t *poker) { void pokerWinnerInit(poker_t *poker) {
uint8_t winners[POKER_PLAYER_COUNT]; uint8_t winners[POKER_PLAYER_COUNT];
uint8_t winnerCount = 0; uint8_t i, winnerCount;
pokerwinning_t winning;
poker->round = POKER_ROUND_WINNER; poker->round = POKER_ROUND_WINNER;
printf("Winner Winner Chicken Dinner\n"); printf("Winner Winner Chicken Dinner\n");
// Kill me // Kill me
for(uint8_t i = 0; i < POKER_PLAYER_COUNT; i++) { for(i = 0; i < POKER_PLAYER_COUNT; i++) {
pokerplayer_t *player = poker->players + i; pokerplayer_t *player = poker->players + i;
if(!pokerPlayerIsAlive(player)) continue; if(!pokerPlayerIsAlive(player)) continue;
// Get the players' full hand // Get the players' full hand
pokerwinning_t winning = pokerPlayerGetWinning(poker, player); pokerPlayerGetWinning(poker, player, &winning);
printf("Winning state %u\n", winning.type); printf("Winning state %u\n", winning.type);
} }
} }