我将如何根据下面的代码创建一手牌,然后在不更改太多现有内容的情况下比较它们?我正在考虑如何做到这一点,并正在考虑为每手牌创建一个数组,然后用 5 张牌填充每手牌。例:
int handCard[5]; //creating the "hand of cards"
int otherHand[5];
handcard.getAcard[5]; //adding 5 cards to each hand
otherHand.getAcard[5];
if (handCard[5] < otherHand) //comparing hands
cout << "Other hand is stronger" << endl;
if (handCard[5] > otherHand)
cout << "Hand card is stronger" << endl;
else
cout << "Both hands are the same in value" << endl;
这样的事情会起作用吗?我害怕更改代码(我通常最终会弄乱所有代码并不得不重新编码整个事情)我目前拥有,因为我已经完成了作业要求的内容,但我想尝试比较两只手。我在其他网站上看到了一些用于扑克的代码并试图模仿,但我认为我没有适当的功能来了解其他网站是如何做到这一点的。我只尝试了大约 3-4 个月的 c++,所以我学得不多。如果有任何其他建议可以使我的代码更有效率,请告诉我。谢谢。
这是我的代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
const int suit1(4);
const int rank1(13);
const string SUIT[suit1] = { "S", "H", "D", "C" };
const string RANK[rank1] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
class CARD
{
friend class DECK;
public:
explicit CARD();
explicit CARD(int Suit, int Rank);
const string CardString();
const int generate_suite();
const int generate_rank();
const int get_suite();
const int get_rank();
private:
int card_suit;
int card_rank;
};
class Deck
{
public:
explicit Deck();
const void print_Deck();
void getACard();
void shuffle();
private:
vector<CARD> cards_deck;
};
int main()
{
srand(time(NULL));
Deck _deck;
cout << "****NOTE*****" << endl;
cout << " 'C' Stands for Club, 'S' Stands for Spade, " << endl;
cout << " 'D' Stands for Diamond, and 'H' Stands for Heart." << endl;
cout << endl;
cout << "After shuffling, the cards are: " << endl;
_deck.shuffle();
_deck.print_Deck();
cout << "Your cards are: " << endl;
for (int index = 0; index < 5; index++)
{
_deck.getACard();
}
cout << "The computers cards are: " << endl;
for (int index1 = 0; index1 < 5; index1++)
{
_deck.getACard();
}
system("PAUSE");
return 0;
}
CARD::CARD()
{
card_suit = generate_suite();
card_rank = generate_rank();
}
CARD::CARD(int Suit, int Rank) : card_suit(Suit), card_rank(Rank)
{
}
const int CARD::generate_suite()
{
return rand() % (suit1 - 1) + 0;
}
const int CARD::generate_rank()
{
return rand() % (rank1 - 1) + 0;
}
const string CARD::CardString()
{
return SUIT[get_suite()] + RANK[get_rank()];
}
const int CARD::get_suite()
{
return card_suit;
}
const int CARD::get_rank()
{
return card_rank;
}
Deck::Deck()
{
for (unsigned int i(0); i < suit1; i++)
{
for (unsigned int j(0); j < rank1; j++)
{
CARD Card(i, j);
cards_deck.push_back(Card);
}
}
}
const void Deck::print_Deck()
{
unsigned int count(1);
for (unsigned int i(0); i < cards_deck.size(); i++)
{
cout << cards_deck[i].CardString() << " ";
if (count = 13)
{
cout << endl;
count = 0;
}
count++;
}
}
void Deck::getACard()
{
CARD r(cards_deck.back().get_suite(), cards_deck.back().get_rank());
cards_deck.pop_back();
cout << r.CardString() << endl;
}
void Deck::shuffle()
{
srand(time(NULL));
random_shuffle(cards_deck.begin(), cards_deck.end());
}
所以你的语法有点不对劲。
int handCard[5];
应该是:
CARD handCard[5];
此外,您getACard
函数需要返回检索到的卡,而不是无效。
handCard.getACard[5]
什么都不做,因为手卡是int
。将其更改为卡片不会执行任何操作,因为卡片没有功能。最后,getACard
没有价值。您需要编写如下函数:
vector<CARD> Deck::getACard(int numCards);
最后,"<"和">"符号对扑克牌一无所知。您需要提出一个函数,例如:
int Deck::comparePokerHands(vector<Card> hand1, vector<Card> hand2);
如果 hand1 是赢家,该函数可以返回 -1,如果 hand2 是赢家,则返回 1,如果手牌相同,则该函数可以返回 0。
不能直接比较数组。无论如何,if (handCard[5] < otherHand)
是不正确的,因为您尝试将单个值(handCard[5]
)与数组(otherHand)
进行比较。
你应该创建一个新类:
class Hand {
Card cards[5];
public:
// constructors, destructors and other stuff you could need
void loadFromDeck(Deck& deck) {
// provided Deck::getACard() returs a Card - it currently does not !
for (int i=0; i<5; i++) cards[i] = deck.getACard();
}
bool operator < (const Hand& other) const {
bool cr;
// implement hand comparison
return cr;
}
}
然后你可以做:
Hand hand;
Hand otherHand;
hand.loadFromDeck(_deck);
otherHand.loadFromDeck(_deck);
if (hand < otherHand) {
...