对扑克牌进行排序以找到获胜牌
Sorting hands poker to find the winning hand
我目前正在制作一个扑克(5张牌)的手牌评估器。首先,我发牌(不是来自同一副牌),然后我检查每个牌是否都有一对,三个同类等,一旦找到匹配的牌,比如说一对王牌,牌的等级将设置为2(1-10,10是最好的),牌的值也将设置为14(1-14,王牌最高)。
我的问题是试图对suckers进行排序,我试图重载运算符,然后我尝试使用sort,并生成了一个检查秩的bool函数。我至少走在正确的道路上了吗?使用sort实际上似乎并没有对它们进行排序,它只是打印出hand1、hand2、hand3等,甚至没有任何检查handrank和值的指示(我使用bool函数作为第三个参数。
我四处寻找了大约一个小时,这是我的最后手段,所以任何帮助都将不胜感激。
此外,手牌是一个生成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;
}
}
手主要作为手1、2等创建。
每只手牌包含一个5张牌的矢量,每张牌都有一个值和一套。
在每一手牌中,int手牌等级和int最高值(当前获胜手牌中最高牌的值,例如2对包含A和6的牌,A将是最高值)
我已经找到了一只手是有一对还是一个同花顺等等。我只需要按照哪只手会赢的顺序对所有的手进行排序,我一直在研究如何同时比较所有的手,看看其中一只手是否赢,或者例如两只手是否有皇家同花顺
这是一个测试工具的片段,它处理并获得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;
}
您可能需要滚动自己的函数来自己对卡片进行排序。
准备好每个功能,找到一手牌所需的牌,从皇家同花顺开始,一直到一对。然后,迭代器逐个遍历每个函数,直到找到匹配的指针。
您还需要存储西装,以便能够检测潮红。
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- std::sort()函数无法对向量的一部分进行排序
- shell排序中的交换和比较
- clang格式:禁用排序包含
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 试图在c++中对数字列表进行排序
- 对扑克牌进行排序以找到获胜牌