diff --git a/src/poker/player.c b/src/poker/player.c index 5524e8dc..b29723b8 100644 --- a/src/poker/player.c +++ b/src/poker/player.c @@ -55,8 +55,9 @@ void pokerPlayerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards){ cardHandSort(cards, poker->cardsFacing+player->cardCount); } -pokerwinning_t pokerPlayerGetWinning(poker_t *poker, pokerplayer_t *player) { - pokerwinning_t winning; +pokerwinning_t pokerPlayerGetWinning( + poker_t *poker, pokerplayer_t *player, pokerwinning_t *winning +) { uint8_t i, j, l; int32_t index; card_t card; @@ -64,170 +65,172 @@ pokerwinning_t pokerPlayerGetWinning(poker_t *poker, pokerplayer_t *player) { int32_t pairs[CARD_SUIT_COUNT]; // Get the full poker hand (should be a 7 card hand) - winning.size = poker->cardsFacing + player->cardCount; - pokerPlayerGetFullHand(poker, player, winning.full); + winning->size = poker->cardsFacing + player->cardCount; + pokerPlayerGetFullHand(poker, player, winning->full); // Reset the winning status. - winning.count = 0; - winning.type = 0x00; - for(i = 0; i < POKER_WINNING_SET_SIZE; i++) winning.set[i] = -1; + winning->count = 0; + winning->type = 0x00; + for(i = 0; i < POKER_WINNING_SET_SIZE; i++) winning->set[i] = -1; //////////////////////// Now look for the winning set //////////////////////// // Royal / Straight Flush - for(i = 0; i < winning.size; i++) { - card = winning.full[i]; + for(i = 0; i < winning->size; i++) { + card = winning->full[i]; number = cardGetNumber(card); if(number < CARD_FIVE) continue; suit = cardGetSuit(card); - winning.count = 1; + winning->count = 1; // Now look for the matching cards (Reverse order to order from A to 10) for(j = 1; j <= 4; j++) { 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; - winning.set[j] = index; - winning.count++; + winning->set[j] = index; + winning->count++; } // 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 - winning.set[0] = i; - winning.type = ( + winning->set[0] = i; + winning->type = ( number == CARD_ACE ? POKER_WINNING_TYPE_ROYAL_FLUSH : POKER_WINNING_TYPE_STRAIGHT_FLUSH ); - return winning; + return; } // Four of a kind. - winning.count = 0; - for(i = 0; i < winning.size; i++) { - card = winning.full[i]; + winning->count = 0; + for(i = 0; i < winning->size; i++) { + card = winning->full[i]; 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; - winning.count = pairCount; - arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set); - winning.type = POKER_WINNING_TYPE_FOUR_OF_A_KIND; - pokerPlayerWinningFillRemaining(&winning); - return winning; + winning->count = pairCount; + arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set); + winning->type = POKER_WINNING_TYPE_FOUR_OF_A_KIND; + pokerPlayerWinningFillRemaining(winning); + return; } // Full House - winning.count = 0; - for(i = 0; i < winning.size; i++) { + winning->count = 0; + for(i = 0; i < winning->size; i++) { 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); - 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? if(pairCount != 2 && pairCount != 3) continue; - if(winning.count == 3) pairCount = 2;//Clamp to 5 max. - arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set+winning.count); - winning.count += pairCount; - if(winning.count != POKER_WINNING_SET_SIZE) continue; - winning.type = POKER_WINNING_TYPE_FULL_HOUSE; + if(winning->count == 3) pairCount = 2;//Clamp to 5 max. + arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set+winning->count); + winning->count += pairCount; + if(winning->count != POKER_WINNING_SET_SIZE) continue; + winning->type = POKER_WINNING_TYPE_FULL_HOUSE; printf("Full House\n"); - return winning; + return; } // Flush (5 same suit) - for(i = 0; i < winning.size; i++) { - card = winning.full[i]; + for(i = 0; i < winning->size; i++) { + card = winning->full[i]; suit = cardGetSuit(card); - winning.count = 1; - for(j = i+1; j < winning.size; j++) { - if(cardGetSuit(winning.full[j]) != suit) continue; - winning.set[winning.count] = j; - winning.count++; - if(winning.count == POKER_WINNING_SET_SIZE) break; + winning->count = 1; + for(j = i+1; j < winning->size; j++) { + if(cardGetSuit(winning->full[j]) != suit) continue; + winning->set[winning->count] = j; + winning->count++; + if(winning->count == POKER_WINNING_SET_SIZE) break; } - if(winning.count < POKER_WINNING_SET_SIZE) continue; - winning.set[0] = i; - winning.type = POKER_WINNING_TYPE_FLUSH; - return winning; + if(winning->count < POKER_WINNING_SET_SIZE) continue; + winning->set[0] = i; + winning->type = POKER_WINNING_TYPE_FLUSH; + return; } // Straight (sequence any suit) - winning.count = 0; - for(i = 0; i < winning.size; i++) { - card = winning.full[i]; + winning->count = 0; + for(i = 0; i < winning->size; i++) { + card = winning->full[i]; number = cardGetNumber(card); if(number < CARD_FIVE) continue; - winning.count = 1; + winning->count = 1; for(j = 1; j <= 4; j++) { 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; - winning.set[j] = index; - winning.count++; + winning->set[j] = index; + winning->count++; } // Check if has all necessary cards. - if(winning.count < POKER_WINNING_SET_SIZE) continue; - winning.set[0] = i; - winning.type = POKER_WINNING_TYPE_STRAIGHT; - return winning; + if(winning->count < POKER_WINNING_SET_SIZE) continue; + winning->set[0] = i; + winning->type = POKER_WINNING_TYPE_STRAIGHT; + return; } // Three of a kind - winning.count = 0; - for(i = 0; i < winning.size; i++) { - card = winning.full[i]; + winning->count = 0; + for(i = 0; i < winning->size; i++) { + card = winning->full[i]; number = cardGetNumber(card); - pairCount = cardCountPairs(winning.full, winning.size, number, pairs); + pairCount = cardCountPairs(winning->full, winning->size, number, pairs); if(pairCount != 3) continue; - winning.count = pairCount; - arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set); - winning.type = POKER_WINNING_TYPE_THREE_OF_A_KIND; - pokerPlayerWinningFillRemaining(&winning); - return winning; + winning->count = pairCount; + arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set); + winning->type = POKER_WINNING_TYPE_THREE_OF_A_KIND; + pokerPlayerWinningFillRemaining(winning); + return; } // Two Pair - winning.count = 0; - for(i = 0; i < winning.size; i++) { + winning->count = 0; + for(i = 0; i < winning->size; i++) { 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); - pairCount = cardCountPairs(winning.full, winning.size, number, pairs); + pairCount = cardCountPairs(winning->full, winning->size, number, pairs); if(pairCount != 2) continue; - arrayCopy(sizeof(int32_t), pairs, pairCount, winning.set+winning.count); - winning.count += pairCount; - if(winning.count != 4) continue; + arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set+winning->count); + winning->count += pairCount; + if(winning->count != 4) continue; - winning.type = POKER_WINNING_TYPE_TWO_PAIR; - pokerPlayerWinningFillRemaining(&winning); - return winning; + winning->type = POKER_WINNING_TYPE_TWO_PAIR; + pokerPlayerWinningFillRemaining(winning); + return; } // Pair - if(winning.count == 2) { - winning.type = POKER_WINNING_TYPE_PAIR; - pokerPlayerWinningFillRemaining(&winning); - return winning; + if(winning->count == 2) { + winning->type = POKER_WINNING_TYPE_PAIR; + pokerPlayerWinningFillRemaining(winning); + return; } // High card - winning.count = 0; - pokerPlayerWinningFillRemaining(&winning); - winning.type = POKER_WINNNIG_TYPE_HIGH_CARD; + winning->count = 0; + pokerPlayerWinningFillRemaining(winning); + winning->type = POKER_WINNNIG_TYPE_HIGH_CARD; - return winning; + return; } void pokerPlayerWinningFillRemaining(pokerwinning_t *winning) { diff --git a/src/poker/player.h b/src/poker/player.h index b7a9bf25..bf25ec95 100644 --- a/src/poker/player.h +++ b/src/poker/player.h @@ -66,9 +66,12 @@ void pokerPlayerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards); * * @param poker Poker game instance. * @param player Player game instance. + * @param winning Pointer to the poker winning to fill out. * @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. diff --git a/src/poker/round/deal.c b/src/poker/round/deal.c index a8b619ed..ac06a386 100644 --- a/src/poker/round/deal.c +++ b/src/poker/round/deal.c @@ -14,6 +14,7 @@ void pokerDealInit(poker_t *poker) { // Shuffle the deck arrayShuffle(sizeof(card_t), poker->deck, CARD_DECK_SIZE); + arrayShuffle(sizeof(card_t), poker->deck, CARD_DECK_SIZE); // Deal 2 card to each player pokerPlayerDealAll(poker, POKER_DEAL_CARD_EACH); diff --git a/src/poker/round/winner.c b/src/poker/round/winner.c index 342aef8d..a541d052 100644 --- a/src/poker/round/winner.c +++ b/src/poker/round/winner.c @@ -10,18 +10,19 @@ void pokerWinnerInit(poker_t *poker) { uint8_t winners[POKER_PLAYER_COUNT]; - uint8_t winnerCount = 0; + uint8_t i, winnerCount; + pokerwinning_t winning; poker->round = POKER_ROUND_WINNER; printf("Winner Winner Chicken Dinner\n"); // 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; if(!pokerPlayerIsAlive(player)) continue; // Get the players' full hand - pokerwinning_t winning = pokerPlayerGetWinning(poker, player); + pokerPlayerGetWinning(poker, player, &winning); printf("Winning state %u\n", winning.type); } } \ No newline at end of file