Working on refactoring winning.

This commit is contained in:
2022-11-20 23:17:40 -08:00
parent 5762f12841
commit ad34be05e3
4 changed files with 62 additions and 40 deletions

View File

@ -13,6 +13,7 @@ void Card::fillDeck(std::vector<struct Card> *deck) {
} }
} }
void Card::sort(std::vector<struct Card> *deck) { void Card::sort(std::vector<struct Card> *deck) {
std::sort(deck->begin(), deck->end(), &cardSorter); std::sort(deck->begin(), deck->end(), &cardSorter);
} }

View File

@ -45,6 +45,11 @@ namespace Dawn {
static void fillDeck(std::vector<struct Card> *deck); static void fillDeck(std::vector<struct Card> *deck);
static void shuffle(std::vector<struct Card> *deck); static void shuffle(std::vector<struct Card> *deck);
static bool_t contains(std::vector<struct Card> *deck, struct Card);
static std::vector<struct Card> countPairs(
std::vector<struct Card> *deck,
enum CardValue value
);
/** /**
* Sort a hand of cards. Cards are ordered in descending weight, aces are * Sort a hand of cards. Cards are ordered in descending weight, aces are
@ -58,9 +63,12 @@ namespace Dawn {
Card(CardSuit suit, CardValue num) : Card(CardSuit suit, CardValue num) :
cardValue((suit * CARD_COUNT_PER_SUIT) + num) cardValue((suit * CARD_COUNT_PER_SUIT) + num)
{ {
assertTrue(suit < CARD_SUIT_COUNT);
assertTrue(num < CARD_COUNT_PER_SUIT);
} }
Card(uint8_t cv) : cardValue(cv) { Card(uint8_t cv) : cardValue(cv) {
assertTrue(cv < CARD_DECK_SIZE);
} }
CardValue getValue() { CardValue getValue() {

View File

@ -239,6 +239,7 @@ int32_t PokerPlayer::getSumOfChips() {
struct PokerWinning PokerPlayer::getWinning() { struct PokerWinning PokerPlayer::getWinning() {
struct PokerWinning winning; struct PokerWinning winning;
uint8_t i, j, l;
// Get the full poker hand (should be a 7 card hand, but MAY not be) // Get the full poker hand (should be a 7 card hand, but MAY not be)
auto itHand = this->hand.begin(); auto itHand = this->hand.begin();
@ -260,78 +261,89 @@ struct PokerWinning PokerPlayer::getWinning() {
auto it = winning.full.begin(); auto it = winning.full.begin();
while(it != winning.full.end()) { while(it != winning.full.end()) {
auto number = it->getValue(); auto number = it->getValue();
auto suit = it->getSuit();
if(number < CARD_FIVE) { if(number < CARD_FIVE) {
++it; ++it;
continue; continue;
} }
auto suit = it->getSuit(); winning.set.clear();
winning->setSize = 1; winning.set.push_back(*it);
// 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->fullSize, cardGet(l, suit)); auto c = Card(suit, (CardValue)l);
if(index == -1) break; if(!Card::contains(&winning.full, c)) break;
winning->set[j] = winning->full[index]; winning.set.push_back(c);
winning->setSize++;
} }
// Check if has all necessary cards. // Check if has all necessary cards.
if(winning->setSize < POKER_WINNING_SET_SIZE) continue; if(winning.set.size() != 5) {
++it;
continue;
}
// Add self to array // Add self to array
winning->set[0] = winning->full[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
); );
pokerWinnerFillRemaining(winning); pokerWinnerFillRemaining(winning);
return; return winning;
} }
// Four of a kind. // Four of a kind.
winning->setSize = 0; it = winning.full.begin();
for(i = 0; i < winning->fullSize; i++) { while(it != winning.full.end()) {
card = winning->full[i]; auto pairs = Card::countPairs(&winning.full, it->getValue());
number = cardGetNumber(card); if(pairs.size() < CARD_SUIT_COUNT) {
pairCount = cardCountPairs(winning->full, winning->fullSize, number, pairs); ++it;
if(pairCount < CARD_SUIT_COUNT) continue;
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;
pokerWinnerFillRemaining(winning);
return;
}
// Full House
winning->setSize = 0;
for(i = 0; i < winning->fullSize; i++) {
// Check we haven't already added this card.
card = winning->full[i];
if(winning->setSize > 0 && (winning->set, winning->setSize, card) != -1) {
continue; continue;
} }
number = cardGetNumber(card); winning.set = pairs;
pairCount = cardCountPairs(winning->full, winning->fullSize, number, pairs); winning.type = POKER_WINNING_TYPE_FOUR_OF_A_KIND;
pokerWinnerFillRemaining(winning);
return winning;
}
// Full House
winning.set.clear();
it = winning.full.begin();
while(it != winning.full.end()) {
// Check we haven't already added this card.
if(Card::contains(&winning.set, *it) != -1) {
++it;
continue;
}
auto pairs = Card::countPairs(&winning.full, it->getValue());
// 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(pairs.size() != 2 && pairs.size() != 3) {
if(winning->setSize == 3) pairCount = 2;//Clamp to 5 max. ++it;
continue;
}
if(winning.set.size() == 3) {
winning.set.pop_back();
}
// Copy found pairs. // Copy found pairs.
for(j = 0; j < pairCount; j++) { auto itPairs = pairs.begin();
winning->set[winning->setSize + j] = winning->full[pairs[j]]; while(itPairs != pairs.end()) {
winning.set.push_back(*itPairs);
++itPairs;
} }
winning->setSize += pairCount;
// Winned? // Winned?
if(winning->setSize != POKER_WINNING_SET_SIZE) continue; if(winning.set.size() != 5) {
winning->type = POKER_WINNING_TYPE_FULL_HOUSE; ++it;
continue;
}
winning.type = POKER_WINNING_TYPE_FULL_HOUSE;
pokerWinnerFillRemaining(winning); pokerWinnerFillRemaining(winning);
return; return winning;
} }
// Flush (5 same suit) // Flush (5 same suit)

View File

@ -37,6 +37,7 @@ namespace Dawn {
public: public:
enum PokerWinningType type; enum PokerWinningType type;
std::vector<struct Card> full; std::vector<struct Card> full;
std::vector<struct Card> set;
static float_t getWinningTypeConfidence(enum PokerWinningType type); static float_t getWinningTypeConfidence(enum PokerWinningType type);
}; };