int* Game::checkCards() const { for (int i = 0; i < players; i++) { if (!player[i].getActive()) //if the player folded skip { player[i].setHand(-1); continue; } if (checkRoyalFlush(i)) continue; if (checkStraightFlush(i)) continue; if (checkFourOfAKind(i)) continue; if (checkFullHouse(i)) continue; if (checkFlush(i)) continue; if (checkStraight(i)) continue; if (checkThreeOfAKind(i)) continue; if (checkTwoPair(i)) continue; if (checkOnePair(i)) continue; player[i].setHand(HighCard); sortCards(i); //for high card } sameHand(); int* winners; for (int k = 9, count = 0; k >= 0; k--) for (int i = 0; i < players; i++) { if (!player[i].getActive()) //if the player folded skip continue; if (player[i].getHand() == k) //if enters here it found the highest hand among all players { for (k = 0; k < players; k++) //then lets find if there's a tie if (player[i].getHand() == player[k].getHand() && i != k) ++count; if (count == 0) //if there's only one winner { winners = new int[2]; winners[0] = 1; //send how many winners are there winners[1] = i; return winners; } winners = new int[count + 2]; winners[0] = ++count; //send how many winners are there winners[1] = i; //save first winner found for (k = 0 , count = 1; k < players; k++) //k is the index of player, count is the index of the vec for winners if (player[i].getHand() == player[k].getHand() && i != k) winners[++count] = k; return winners; } } return nullptr; }
// returns 0 if not a straight, otherwise returns the value of the highest card int checkStraight (int *ranks, int counted, int consecutive, int started) { if (counted == NUM_RANKS) { return 0; } else if (*ranks) { if (consecutive + 1 == HAND_SIZE) { return counted; } else { return checkStraight (++ranks, counted + 1, consecutive + 1, 1); } } else if (!started) { return checkStraight (++ranks, counted + 1, consecutive, 0); } else { return 0; } }
Hand CheckerPlus::checkHand(vector<Card> handcard) { sort(handcard); Hand ret(NOT_THIS_RANK); if(checkStraightFlush(handcard,ret))return ret; else if(checkFour(handcard,ret))return ret; else if(checkFullHouse(handcard,ret))return ret; else if(checkFlush(handcard,ret))return ret; else if(checkStraight(handcard,ret))return ret; else if(checkFlush(handcard,ret))return ret; else if(checkTrip(handcard,ret))return ret; else if(checkTwoPairs(handcard,ret))return ret; else if(checkPair(handcard,ret))return ret; else checkHighCard(handcard,ret); return ret; }
bool Game::checkStraightFlush(int i) const { int counter; for (int sf = 0; sf < 4; sf++) //check if all cards have the same suit { counter = 0; for (int c = 0; c < 5; c++) if (player[i].getCards()[c].getSuit() == deck->getSuits()[sf]) ++counter; if (counter == 5) //once we validate that all cards have the same suit check if they're consecutive if (checkStraight(i)) { player[i].setHand(StraightFlush); return true; } } return false; }
void checkHand (handType *hand) { int ranks[NUM_RANKS] = {0}; int suits[NUM_SUITS] = {0}; for (int i = 0; i < HAND_SIZE; i++) { ranks[hand->cards[i].rank]++; suits[hand->cards[i].suit]++; } hand->type = bust; hand->value = checkStraight (ranks, 0, 0, 0); if (hand->value) { hand->type = straight; } else { // check for pairs, three of a kinds, and four of a kinds for (int i = 0; i < NUM_RANKS; i++) { switch (ranks[i]) { case 2: if (hand->type == bust) { hand->type = pair; hand->value = i; } else if (hand->type == pair) { hand->type = twoPair; hand->value = (hand->value > i)? (hand->value * 10 + i) : (i * 10 + hand->value); } else if (hand->type == three) { hand->type = fullHouse; hand->value = hand->value * 10 + i; } break; case 3: if (hand->type == bust) { hand->type = three; hand->value = i; } else if (hand->type == pair) { hand->type = fullHouse; hand->value = i * 10 + hand->value; } break; case 4: hand->type = four; hand->value = i; break; } } } //check for flushes for (int i = 0; i < NUM_SUITS; i++) { if (suits[i] == HAND_SIZE) { if (hand->type == straight) { if (hand->value == NUM_RANKS - 1) { hand->type = royalFlush; } else { hand->type = straightFlush; } } else { hand->type = flush; } } } // calculate value if hand was a bust or a flush if (hand->type == bust || hand->type == flush) { hand->value = 0; sortCards (hand->cards, HAND_SIZE); for (int i = HAND_SIZE - 1; i >= 0; i--) { hand->value = hand->value * 10 + hand->cards[i].rank; } } }