我正在为扑克(5张牌)做一个手牌评估员。首先我交易手(不是来自同一套牌),然后我检查每个人是否有一对,三种等等,一旦找到匹配就说一对A,手牌等级将被设置为2(1 -10,10是最好的)卡的价值也将设置为14,对于ace(1-14,ace最高)。
我的问题是尝试对吸盘进行排序,我试图重载运算符,然后我尝试使用sort并制作一个检查排名的bool函数。我至少走在正确的道路上吗?使用sort didnt实际上似乎对它们进行排序它只是将它们打印出hand1,hand2,hand3等,而没有任何指示甚至检查handrank和值(我使用bool func作为第3个参数..
我已经环顾了一个小时左右,这是我的最后一招,所以任何帮助都会非常感激。
此外,Hand是一个生成5张牌的矢量的类,检查是否在生成时手中完成了对等,手牌等级和值是私人类成员。
谢谢!
bool operator > (const Hand &h1) const
{
return !((*this) < h1.handRank);
}
bool operator < (const Hand &h1) const
{
if(handRank < h1.handRank)
{
return handRank < h1.handRank;
}
if(handRank == h1.handRank)
{
return highestValue < h1.highestValue;
}
}
在main中创建指针为hand1,2等。
每只手包含5张牌的矢量,每张牌都有一个值和一套西装。
在每手牌中的int handrank和int highestvalue(当前获胜牌中最高牌的值,例如2对包含aces和6,aces将是最高值)
我已经发现一只手有一双还是一个同花顺等我只需按顺序对所有的手进行排序,我会坚持如何比较所有的手,看看是否一个赢或是否例如两个有皇家同花顺
答案 0 :(得分:1)
这是一个测试工具的片段,可以处理并获得5张牌。也许它会帮助你做你想做的事。
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <tuple>
#include <vector>
enum HandScore
{
high_card,
one_pair,
two_pair,
three_of_a_kind,
straight,
flush,
full_house,
four_of_a_kind,
straight_flush,
royal_flush,
num_scores,
};
const char* const HandScoreNames[num_scores] =
{
"High Card",
"One Pair",
"Two Pair",
"Three of a Kind",
"Straight",
"Flush",
"Full House",
"Four of a Kind",
"Straight Flush",
"Royal Flush",
};
const int kStartingRank = 2;
const int kNumRanks = 13;
const int kNumSuits = 4;
const int kCardsPerHand = 5;
const int kCardsPerDeck = 52;
struct Card
{
Card(int suit_ = 0, int rank_ = kStartingRank) : rank(rank_), suit(suit_) {}
bool operator<(const Card& other) const
{
return std::tie(rank, suit) < std::tie(other.rank, other.suit);
}
int rank;
int suit;
};
struct Hand
{
Hand() : cards(kCardsPerHand) {}
HandScore GetScore()
{
HandScore score = high_card;
std::sort(cards.begin(), cards.end());
int counts[kNumRanks] = {};
int suits[kNumSuits] = {};
for(size_t i = 0; i < cards.size(); ++i)
{
++counts[cards[i].rank - kStartingRank];
++suits[cards[i].suit];
}
int pair_count = 0;
int three_count = 0;
int four_count = 0;
for(int i = 0; i < kNumRanks; ++i)
{
if(counts[i] == 2)
{
++pair_count;
}
else if(counts[i] == 3)
{
++three_count;
}
else if(counts[i] == 4)
{
++four_count;
}
}
bool is_flush = false;
for(int i = 0; i < kNumSuits; ++i)
{
if(suits[i] == kCardsPerHand)
{
is_flush = true;
break;
}
}
const int spread5 = cards[cards.size() - 1].rank - cards[0].rank;
const int spread4 = cards[cards.size() - 2].rank - cards[0].rank;
if(is_flush)
{
score = flush;
if(spread5 == 4)
{
if(cards[0].rank == 10)
{
score = royal_flush;
}
else
{
score = straight_flush;
}
}
//special check for 2345A
else if(spread5 == 12 && spread4 == 3 && cards[0].rank == 2 && cards[cards.size() - 1].rank == 14)
{
score = straight_flush;
}
}
else
{
if(spread5 == 4)
{
score = straight;
}
//special check for 2345A
else if(spread5 == 12 && spread4 == 3 && cards[0].rank == 2 && cards[cards.size() - 1].rank == 14)
{
score = straight;
}
else if(four_count == 1)
{
score = four_of_a_kind;
}
else if(three_count == 1)
{
if(pair_count == 1)
{
score = full_house;
}
else
{
score = three_of_a_kind;
}
}
else if(pair_count == 2)
{
score = two_pair;
}
else if(pair_count == 1)
{
score = one_pair;
}
}
return score;
}
std::vector<Card> cards;
};
struct Deck
{
Deck() : cards(kCardsPerDeck)
{
for(int s = 0; s < kNumSuits; ++s)
{
for(int r = 0; r < kNumRanks; ++r)
{
cards[s * kNumRanks + r] = Card(s, r + kStartingRank);
}
}
}
void shuffle()
{
std::random_shuffle(cards.begin(), cards.end());
}
void deal(Hand& h)
{
for(size_t i = 0; i < h.cards.size(); ++i)
{
h.cards[i] = cards[i];
}
}
std::vector<Card> cards;
};
int main()
{
std::srand(static_cast<unsigned int>(std::time(0)));
Deck deck;
Hand hand;
int scores[num_scores] = {};
const int num_hands = 1000;
const int num_width = static_cast<int>(std::log10(num_hands) + 1);
for(int i = 0; i < num_hands; ++i)
{
deck.shuffle();
deck.deal(hand);
++scores[hand.GetScore()];
}
std::cout << "Results for " << num_hands << " Hands:" << std::endl;
for(int i = 0; i < num_scores; ++i)
{
std::cout << std::setw(num_width) << std::right << scores[i] << " - " << HandScoreNames[i] << std::endl;
}
return 0;
}
答案 1 :(得分:-1)
你可能需要自己动手来对卡片进行排序。
准备每个功能以找到手牌所需的牌,从皇家同花顺到一对。然后,逐个迭代每个函数,直到找到匹配的手。
您还需要存储套装以便能够检测到冲洗。