我如何根据下面的代码创建一副牌,然后在不改变我已经过多的情况下比较它们?我正在考虑如何解决这个问题,并考虑为每只手创建一个阵列,然后用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());
}
答案 0 :(得分:1)
所以你的语法有点偏。
int handCard[5];
应该是:
CARD handCard[5];
此外,您getACard
函数需要返回检索到的卡片,而不是无效。
handCard.getACard[5]
没有做任何事情,因为handCard是int
。将其更改为卡片并没有做任何事情,因为卡片没有功能。而且,最后,getACard
没有取值。您需要编写如下函数:
vector<CARD> Deck::getACard(int numCards);
最后,&#39;&lt;&#39;和&#39;&gt;&#39;符号对扑克牌一无所知。你想要提出一个像:
这样的功能int Deck::comparePokerHands(vector<Card> hand1, vector<Card> hand2);
如果hand1是赢家,那么该函数可以返回-1,如果hand2是赢家则返回1,如果手牌相同则返回0。
答案 1 :(得分:1)
您无法直接比较数组。无论如何,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) {
...