Winner logic finished.

This commit is contained in:
2021-07-08 09:18:21 -07:00
parent bf0de5bfa8
commit 07ff33300a
6 changed files with 234 additions and 94 deletions

View File

@ -58,6 +58,7 @@
#define POKER_RIVER_CARD_COUNT 1
/** Winning Types */
#define POKER_WINNING_TYPE_NULL 0x00
#define POKER_WINNING_TYPE_ROYAL_FLUSH 0x01
#define POKER_WINNING_TYPE_STRAIGHT_FLUSH 0x02
#define POKER_WINNING_TYPE_FOUR_OF_A_KIND 0x03
@ -72,6 +73,22 @@
/** How many cards make a winning set */
#define POKER_WINNING_SET_SIZE 5
/** Holds information about the winning player state */
typedef struct {
/** The full set of both the dealer and player's hand */
card_t full[POKER_PLAYER_HAND + POKER_DEALER_HAND];
uint8_t fullSize;
/** Holds the winning set */
card_t set[POKER_WINNING_SET_SIZE];
uint8_t setSize;
/** Winning Type */
uint8_t type;
/** If there was a kicker card it will be here, otherwise -1 for no kicker */
card_t kicker;
} pokerwinning_t;
typedef struct {
//////////////////////////////////////////////////////////////////////////////
@ -96,6 +113,11 @@ typedef struct {
/** Player States */
pokerplayer_t players[POKER_PLAYER_COUNT];
/** Winning States */
pokerwinning_t winnings[POKER_PLAYER_COUNT];
uint8_t winners[POKER_PLAYER_COUNT];
uint8_t winnerCount;
/** The current round the game is on */
uint8_t round;
@ -153,18 +175,4 @@ typedef struct {
texture_t cardTexture;
tileset_t cardTileset;
primitive_t cardPrimitive;
} poker_t;
/** Holds information about the winning player state */
typedef struct {
/** The full set of both the dealer and player's hand */
card_t full[POKER_PLAYER_HAND + POKER_DEALER_HAND];
uint8_t size;
/** Holds the winning set */
int32_t set[POKER_WINNING_SET_SIZE];
uint8_t count;
/** Winning Type */
uint8_t type;
} pokerwinning_t;
} poker_t;

View File

@ -55,7 +55,7 @@ void pokerPlayerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards){
cardHandSort(cards, poker->cardsFacing+player->cardCount);
}
pokerwinning_t pokerPlayerGetWinning(
void pokerPlayerGetWinning(
poker_t *poker, pokerplayer_t *player, pokerwinning_t *winning
) {
uint8_t i, j, l;
@ -65,39 +65,37 @@ pokerwinning_t pokerPlayerGetWinning(
int32_t pairs[CARD_SUIT_COUNT];
// Get the full poker hand (should be a 7 card hand)
winning->size = poker->cardsFacing + player->cardCount;
winning->fullSize = 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->setSize = 0;
//////////////////////// Now look for the winning set ////////////////////////
// Royal / Straight Flush
for(i = 0; i < winning->size; i++) {
for(i = 0; i < winning->fullSize; i++) {
card = winning->full[i];
number = cardGetNumber(card);
if(number < CARD_FIVE) continue;
suit = cardGetSuit(card);
winning->count = 1;
winning->setSize = 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->fullSize, cardGet(l, suit));
if(index == -1) break;
winning->set[j] = index;
winning->count++;
winning->set[j] = winning->full[index];
winning->setSize++;
}
// Check if has all necessary cards.
if(winning->count < POKER_WINNING_SET_SIZE) continue;
if(winning->setSize < POKER_WINNING_SET_SIZE) continue;
// Add self to array
winning->set[0] = i;
winning->set[0] = winning->full[i];
winning->type = (
number == CARD_ACE ? POKER_WINNING_TYPE_ROYAL_FLUSH :
POKER_WINNING_TYPE_STRAIGHT_FLUSH
@ -106,112 +104,120 @@ pokerwinning_t pokerPlayerGetWinning(
}
// Four of a kind.
winning->count = 0;
for(i = 0; i < winning->size; i++) {
winning->setSize = 0;
for(i = 0; i < winning->fullSize; i++) {
card = winning->full[i];
number = cardGetNumber(card);
pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
pairCount = cardCountPairs(winning->full, winning->fullSize, number, pairs);
if(pairCount < CARD_SUIT_COUNT) continue;
winning->count = pairCount;
arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set);
winning->setSize = pairCount;
for(j = 0; j < pairCount; j++) winning->set[j] = winning->full[pairs[j]];
winning->type = POKER_WINNING_TYPE_FOUR_OF_A_KIND;
pokerPlayerWinningFillRemaining(winning);
return;
}
// Full House
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;
winning->setSize = 0;
for(i = 0; i < winning->fullSize; i++) {
// Check we haven't already added this card.
card = winning->full[i];
if(cardContains(winning->set, winning->setSize, card) != -1) continue;
number = cardGetNumber(card);
pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
pairCount = cardCountPairs(winning->full, winning->fullSize, 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;
if(winning->setSize == 3) pairCount = 2;//Clamp to 5 max.
// Copy found pairs.
for(j = 0; j < pairCount; j++) {
winning->set[winning->setSize + j] = winning->full[pairs[j]];
}
winning->setSize += pairCount;
// Winned?
if(winning->setSize != POKER_WINNING_SET_SIZE) continue;
winning->type = POKER_WINNING_TYPE_FULL_HOUSE;
printf("Full House\n");
return;
}
// Flush (5 same suit)
for(i = 0; i < winning->size; i++) {
for(i = 0; i < winning->fullSize; i++) {
card = winning->full[i];
suit = cardGetSuit(card);
winning->count = 1;
for(j = i+1; j < winning->size; j++) {
winning->setSize = 1;
for(j = i+1; j < winning->fullSize; j++) {
if(cardGetSuit(winning->full[j]) != suit) continue;
winning->set[winning->count] = j;
winning->count++;
if(winning->count == POKER_WINNING_SET_SIZE) break;
winning->set[winning->setSize] = winning->full[j];
winning->setSize++;
if(winning->setSize == POKER_WINNING_SET_SIZE) break;
}
if(winning->count < POKER_WINNING_SET_SIZE) continue;
winning->set[0] = i;
if(winning->setSize < POKER_WINNING_SET_SIZE) continue;
winning->set[0] = winning->full[0];
winning->type = POKER_WINNING_TYPE_FLUSH;
return;
}
// Straight (sequence any suit)
winning->count = 0;
for(i = 0; i < winning->size; i++) {
winning->setSize = 0;
for(i = 0; i < winning->fullSize; i++) {
card = winning->full[i];
number = cardGetNumber(card);
if(number < CARD_FIVE) continue;
winning->count = 1;
winning->setSize = 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->fullSize, l);
if(index == -1) break;
winning->set[j] = index;
winning->count++;
winning->set[j] = winning->full[index];
winning->setSize++;
}
// Check if has all necessary cards.
if(winning->count < POKER_WINNING_SET_SIZE) continue;
winning->set[0] = i;
if(winning->setSize < POKER_WINNING_SET_SIZE) continue;
winning->set[0] = winning->full[i];
winning->type = POKER_WINNING_TYPE_STRAIGHT;
return;
}
// Three of a kind
winning->count = 0;
for(i = 0; i < winning->size; i++) {
winning->setSize = 0;
for(i = 0; i < winning->fullSize; i++) {
card = winning->full[i];
number = cardGetNumber(card);
pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
pairCount = cardCountPairs(winning->full, winning->fullSize, number, pairs);
if(pairCount != 3) continue;
winning->count = pairCount;
arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set);
winning->setSize = pairCount;
for(j = 0; j < pairCount; j++) winning->set[j] = winning->full[pairs[j]];
winning->type = POKER_WINNING_TYPE_THREE_OF_A_KIND;
pokerPlayerWinningFillRemaining(winning);
return;
}
// Two Pair
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)) {
winning->setSize = 0;
for(i = 0; i < winning->fullSize; i++) {
card = winning->full[i];// Check we haven't already added this card.
if(cardContains(winning->set, winning->setSize, card) != -1) {
continue;
}
card = winning->full[i];
number = cardGetNumber(card);
pairCount = cardCountPairs(winning->full, winning->size, number, pairs);
pairCount = cardCountPairs(winning->full, winning->fullSize, number, pairs);
if(pairCount != 2) continue;
arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set+winning->count);
winning->count += pairCount;
if(winning->count != 4) continue;
for(j = 0; j < pairCount; j++) {
winning->set[j] = winning->full[winning->setSize + pairs[j]];
}
// arrayCopy(sizeof(int32_t), pairs, pairCount, winning->set+winning->setSize);
winning->setSize += pairCount;
if(winning->setSize != 4) continue;
winning->type = POKER_WINNING_TYPE_TWO_PAIR;
pokerPlayerWinningFillRemaining(winning);
@ -219,28 +225,91 @@ pokerwinning_t pokerPlayerGetWinning(
}
// Pair
if(winning->count == 2) {
if(winning->setSize == 2) {
winning->type = POKER_WINNING_TYPE_PAIR;
pokerPlayerWinningFillRemaining(winning);
return;
}
// High card
winning->count = 0;
winning->setSize = 0;
pokerPlayerWinningFillRemaining(winning);
winning->type = POKER_WINNNIG_TYPE_HIGH_CARD;
return;
}
void pokerPlayerWinningFillRemaining(pokerwinning_t *winning) {
uint8_t i;
card_t pokerPlayerCompareWinning(pokerwinning_t *left, pokerwinning_t *right) {
uint8_t i, number;
card_t card;
int32_t index;
for(i = winning->count; i < POKER_WINNING_SET_SIZE; i++) {
if(arrayContains(sizeof(int32_t),winning->set,winning->count,&i)) continue;
winning->set[winning->count++] = i;
card_t highCardLeft, highCardRight;
uint8_t highNumberLeft, highNumberRight;
highNumberLeft = 0xFF;
highNumberRight = 0xFF;
for(i = 0; i < left->setSize; i++) {
card = left->set[i];
number = cardGetNumber(card);
if(highNumberLeft != 0xFF && number < highNumberLeft) continue;// Quick check
index = cardContainsNumber(right->set, right->setSize, number);
if(index != -1) continue;// In other?
if(highNumberLeft == 0xFF||number == CARD_ACE||highNumberRight < number) {
highNumberLeft = number;
highCardLeft = card;
}
}
for(i = 0; i < right->setSize; i++) {
card = right->set[i];
number = cardGetNumber(card);
if(highNumberRight != 0xFF && number < highNumberRight) continue;
index = cardContainsNumber(left->set, left->setSize, number);
if(index != -1) continue;
if(highNumberRight == 0xFF||number == CARD_ACE||highNumberRight < number) {
highNumberRight = number;
highCardRight = card;
}
}
arraySortInt32(winning->set, winning->count);
if(highNumberLeft < highNumberRight) return 0xFF;
return highCardLeft;//Greater or Equal to.
}
void pokerPlayerWinningFillRemaining(pokerwinning_t *winning) {
uint8_t i, highest, current;
card_t highestCard, currentCard;
// Set the kicker
winning->kicker = 0xFF;
// Fill the remaining cards
while(winning->setSize < POKER_WINNING_SET_SIZE) {
highest = 0xFF;
for(i = 0; i < winning->fullSize; i++) {
currentCard = winning->full[i];
if(cardContains(winning->set, winning->setSize, currentCard) != -1) {
continue;
}
if(highest == 0xFF) {
highestCard = currentCard;
highest = cardGetNumber(highestCard);
} else {
current = cardGetNumber(currentCard);
if(current != CARD_ACE && current < highest) continue;
highestCard = currentCard;
highest = current;
}
}
winning->set[winning->setSize++] = highestCard;
}
cardHandSort(winning->set, winning->setSize);
}

View File

@ -69,10 +69,20 @@ void pokerPlayerGetFullHand(poker_t *poker,pokerplayer_t *player,card_t *cards);
* @param winning Pointer to the poker winning to fill out.
* @return The winning state for this player.
*/
pokerwinning_t pokerPlayerGetWinning(
void pokerPlayerGetWinning(
poker_t *poker, pokerplayer_t *player, pokerwinning_t *winning
);
/**
* Compares two winning sets. The returned card is the kicker if the LEFT side
* is the winner. If LEFT is not a winner then 0xFF will be returned.
*
* @param left Left winning set.
* @param right Right winning set.
* @return The kicker card from left's hand or 0xFF if left is not the winner.
*/
card_t pokerPlayerCompareWinning(pokerwinning_t *left, pokerwinning_t *right);
/**
* Fills the remaining cards for a given poker player winning hand.
* @param winning Pointer to the poker winning to fill out.

View File

@ -14,7 +14,6 @@ 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);

View File

@ -4,25 +4,77 @@
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "winner.h"
int32_t _pokerWinnersSorter(void *left, void* right) {
return ((pokerwinning_t *)right)->type - ((pokerwinning_t *)left)->type;
}
void pokerWinnersSort(pokerwinning_t *winnings, int32_t length) {
arraySort(sizeof(pokerwinning_t),
winnings, length, (arraysort_t *)&_pokerWinnersSorter
);
}
void pokerWinnerInit(poker_t *poker) {
uint8_t winners[POKER_PLAYER_COUNT];
uint8_t i, winnerCount;
pokerwinning_t winning;
uint8_t i, j, number, highNumber;
pokerwinning_t *left, *right;
pokerplayer_t *player;
card_t card, highCard;
bool winner;
poker->round = POKER_ROUND_WINNER;
printf("Winner Winner Chicken Dinner\n");
poker->winnerCount = 0;
// Kill me
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
pokerplayer_t *player = poker->players + i;
left = poker->winnings + i;
left->type = POKER_WINNING_TYPE_NULL;
player = poker->players + i;
if(!pokerPlayerIsAlive(player)) continue;
// Get the players' full hand
pokerPlayerGetWinning(poker, player, &winning);
printf("Winning state %u\n", winning.type);
// Get the players' full hand.
pokerPlayerGetWinning(poker, player, left);
}
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
left = poker->winnings + i;
if(left->type == POKER_WINNING_TYPE_NULL) continue;
winner = true;
highNumber = 0xFF;
for(j = 0; j < POKER_PLAYER_COUNT; j++) {
if(i == j) continue;
right = poker->winnings + j;
if(right->type == POKER_WINNING_TYPE_NULL) continue;
// Am I the better hand?
if(left->type < right->type) continue;
if(left->type > right->type) {
// No
winner = false;
break;
}
// Equal, compare hands.
card = pokerPlayerCompareWinning(left, right);
if(card == 0xFF) {
winner = false;
break;
}
number = cardGetNumber(card);
if(highNumber == 0xFF || number == CARD_ACE || number > highNumber) {
highCard = card;
highNumber = number;
}
}
if(!winner) continue;
left->kicker = highCard;
poker->winners[poker->winnerCount++] = i;
}
printf("Winner Count %u\n", poker->winnerCount);
}

View File

@ -9,4 +9,6 @@
#include <dawn/dawn.h>
#include "../player.h"
int32_t _pokerWinnersSorter(void *left, void* right);
void pokerWinnersSort(pokerwinning_t *winnings, int32_t length);
void pokerWinnerInit(poker_t *poker);