int checkHighCard(const std::vector<std::string>& player1, const std::vector<std::string>& player2) { const std::vector<int> card1 = sortCards(player1); const std::vector<int> card2 = sortCards(player2); for (int i = card1.size(); i != -1; --i) { if (card1[i] < card2[i]) return 0; else if (card1[i] > card2[i]) return 1; } return -1; // draw }
bool Game::checkStraight(int i) const { bool aceAsOne = false, passed = true; int counter = 0, lowest = 0; for (int l = 0; l < 13 && passed; l++) //find lowest ranking of card for (int c = 0; c < 5 && passed; c++) if (player[i].getCards()[c].getCard() == deck->getCards()[l]) { //for to save cards in order from #7 lowest = c; passed = false; //break both for } if (lowest == 0) //check if Ace has to be used as 1 for (int l = 0; l < 5; l++) if (player[i].getCards()[l].getCard() == deck->getCards()[12]) aceAsOne = true; if (aceAsOne) { for (int sf = 0; sf < 5; sf++) if (player[i].getCards()[sf].getCard() == deck->getCards()[lowest]) { ++lowest; if (lowest == 4) { sortCards(i); player[i].setHand(Straight); return true; } sf = 0; } } else { for (int sf = 0; sf < 5 && lowest < 8; sf++) if (player[i].getCards()[sf].getCard() == deck->getCards()[lowest]) { ++lowest; ++counter; if (counter == 5) { sortCards(i); player[i].setHand(Straight); return true; } sf = 0; } } return false; }
void Dashboard::addCardItem(CardItem *card_item){ card_item->filter(filter); if(ClientInstance->getStatus() == Client::Playing) card_item->setEnabled(card_item->getFilteredCard()->isAvailable(Self)); else{ card_item->setEnabled(true); card_item->setEnabled(false); } card_item->setPos(mapFromScene(card_item->pos())); card_item->setParentItem(this); card_item->setRotation(0.0); card_item->setFlags(ItemIsFocusable); card_item->setZValue(0.1); card_items << card_item; connect(card_item, SIGNAL(clicked()), this, SLOT(onCardItemClicked())); connect(card_item, SIGNAL(thrown()), this, SLOT(onCardItemThrown())); connect(card_item, SIGNAL(enter_hover()), this, SLOT(onCardItemHover())); connect(card_item, SIGNAL(leave_hover()), this, SLOT(onCardItemLeaveHover())); sortCards(sort_type); handcard_num->setText(QString::number(Self->getHandcardNum())); //handcard_num->parentItem()->show(); }
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; }
Hand::Hand(const Card * cards, int size) { for(int i = 0; i < size; i++){ _cards.push_back(cards[i]); } sortCards(); }
std::vector<int> countCards(const std::vector<std::string>& vec) { const std::vector<int> card = sortCards(vec); std::vector<int> table(15, 0); for (size_t i = 0; i < card.size(); ++i) ++table[card[i]]; return table; }
bool isRoyalFlush(const std::vector<std::string>& vec) { const std::vector<int> card = sortCards(vec); for (size_t i = 0; i < card.size(); ++i) { if (card[i] != (int)i+10) return false; } return true; }
bool isStraight(const std::vector<std::string>& vec) { const std::vector<int> card = sortCards(vec); for (size_t i = 0; i < card.size(); ++i) { if (card[i] != card[0] + (int)i) return false; } return true; }
//get the card power, as follow: //royal flush: 900 + all the cards values //straight flush: 800 + all the cards values //four of a kind: 700 + all the cards values //full house: 600 + all the cards values //flush: 500 + all the cards values //straight: 400 + all the cards values //three of a kind: 300 + all the cards values //two pairs: 200 + all the cards values //pair: 100 + all the cards values //high card: sum of all the cards value (5 * 13 will never be more than 100 so...) int getHandPower(Card c[5]) { p = sumCardNums(c, 0); p += sumCardNums(c, 1); sortCards(c); if(royalFlush(c)) { p += 900; return p; } if(straightFlush(c)) { p += 800; return p; } if(fourOfAKind(c)) { p += 700; return p; } if(fullHouse(c)) { p += 600; return p; } if(flush(c)) { p += 500; return p; } if(straight(c)) { p += 400; return p; } if(threeOfAKind(c)) { p += 300; return p; } if(twoPairs(c)) { p += 200; return p; } if(pair(c)) { p += 100; return p; } return p; }
int getHighCard(const std::vector<std::string>& vec, const int pflag = 0) { if (pflag != 0) { const std::vector<int> table = countCards(vec); if (pflag == 1) return std::distance(table.begin(), std::find(table.begin(), table.end(), 2)); else if (pflag == 2) { const auto p1 = std::find(table.begin(), table.end(), 2); const auto p2 = std::find(p1+1, table.end(), 2); return std::max(*p1, *p2); } else if (pflag == 3) return std::distance(table.begin(), std::find(table.begin(), table.end(), 3)); else if (pflag == 4) return std::distance(table.begin(), std::find(table.begin(), table.end(), 4)); } else { const std::vector<int> card = sortCards(vec); return *std::max_element(card.begin(), card.end()); } return -1; }
void Dashboard::addCardItem(CardItem *card_item){ if(ClientInstance->getStatus() == Client::Playing) card_item->setEnabled(card_item->getCard()->isAvailable()); else card_item->setEnabled(false); card_item->setPos(mapFromScene(card_item->pos())); card_item->setParentItem(this); card_item->setRotation(0.0); card_item->setFlags(ItemIsFocusable); card_items << card_item; connect(card_item, SIGNAL(clicked()), this, SLOT(onCardItemClicked())); connect(card_item, SIGNAL(thrown()), this, SLOT(onCardItemThrown())); sortCards(); handcard_num->setText(QString::number(Self->getHandcardNum())); handcard_num->parentItem()->show(); }
void Hand::insert(Card card){ _cards.push_back(card); sortCards(); }
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; } } }