好吧我正在制作一张Deck of Cards,正如许多其他人所要求的那样,我可以将所有卡片显示在我的代码中,但问题在于它们每次都处于相同的顺序。我已经得到了我的教授想要的卡片课程,但是Deck课程是我的问题。一些事情,她希望我们使用枚举的套装,她希望我们将设定的方法私下放在一起,看看为什么我们不应该这样做。 (不要问我这有什么意义,但这是她想要的)其上的说明说:
Deck构造函数将构建一个包含所有52个Card对象的数组。也就是说,它将构建Card对象。它应该有一个与数组平行的数组,指示卡是否已从卡组中移除。它应该有一种方法从牌组中挑选一张随机牌。此方法将删除并返回一张卡片。
我只是停留在并行数组部分,如何检查是否从原始数组中删除了某些内容?我应该如何选择随机卡?
任何反馈都将不胜感激!
这是我的代码:
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
enum suit { CLUBS, DIAMONDS, HEARTS, SPADES };
class Card
{
friend class Deck;
private:
string rankname[14];
string suitname;
int rank;
int suitnum;
suit s;
void privatesetsuit(suit arg_suit)
{
s = arg_suit;
}
void privatesetrank(int arg_rank)
{
rank = arg_rank;
}
public:
Card()
{
}
void setsuit(suit arg_suit)
{
privatesetsuit(arg_suit);
}
void setrank(int arg_rank)
{
privatesetrank(arg_rank);
}
int getsuit()
{
return s;
}
int getrank()
{
return rank;
}
void toString()
{
rankname[1] = "ACE";
rankname[2] = "TWO";
rankname[3] = "THREE";
rankname[4] = "FOUR";
rankname[5] = "FIVE";
rankname[6] = "SIX";
rankname[7] = "SEVEN";
rankname[8] = "EIGHT";
rankname[9] = "NINE";
rankname[10] = "TEN";
rankname[11] = "JACK";
rankname[12] = "QUEEN";
rankname[13] = "KING";
if (suitnum == 1)
{
suitname = "DIAMONDS";
}
if (suitnum == 3)
{
suitname = "SPADES";
}
if (suitnum == 2)
{
suitname = "HEARTS";
}
if (suitnum == 0)
{
suitname = "CLUBS";
}
cout << rankname[rank] << " OF " << suitname << endl;
}
};
void privatesetsuit(int s);
void setsuit(int s);
void privatesetrank(int arg_rank);
void setrank(int arg_rank);
void toString();
class Deck
{
public:
Deck()
{
Card currentcard[52];
Card parallel[52];
srand(time(NULL));
for (int i = 0; i < 52; i++)
{
currentcard[i].rank = i % 52;
currentcard[i].rank = currentcard[i].rank % 13 + 1;
parallel[i].rank = currentcard[i].rank;
currentcard[i].suitnum = i % 4;
currentcard[i].toString();
}
}
};
int main()
{
Deck d;
d;
// Test for Class!//
//c.setrank(rank);
//c.setsuit(s);
//c.toString();
// Test for Class!//
}
输出:
ACE OF CLUBS
TWO OF DIAMONDS
THREE OF HEARTS
FOUR OF SPADES
FIVE OF CLUBS
SIX OF DIAMONDS
SEVEN OF HEARTS
EIGHT OF SPADES
NINE OF CLUBS
TEN OF DIAMONDS
JACK OF HEARTS
QUEEN OF SPADES
KING OF CLUBS
ACE OF DIAMONDS
TWO OF HEARTS
THREE OF SPADES
FOUR OF CLUBS
FIVE OF DIAMONDS
SIX OF HEARTS
SEVEN OF SPADES
EIGHT OF CLUBS
NINE OF DIAMONDS
TEN OF HEARTS
JACK OF SPADES
QUEEN OF CLUBS
KING OF DIAMONDS
ACE OF HEARTS
TWO OF SPADES
THREE OF CLUBS
FOUR OF DIAMONDS
FIVE OF HEARTS
SIX OF SPADES
SEVEN OF CLUBS
EIGHT OF DIAMONDS
NINE OF HEARTS
TEN OF SPADES
JACK OF CLUBS
QUEEN OF DIAMONDS
KING OF HEARTS
ACE OF SPADES
TWO OF CLUBS
THREE OF DIAMONDS
FOUR OF HEARTS
FIVE OF SPADES
SIX OF CLUBS
SEVEN OF DIAMONDS
EIGHT OF HEARTS
NINE OF SPADES
TEN OF CLUBS
JACK OF DIAMONDS
QUEEN OF HEARTS
KING OF SPADES
Press any key to continue . . .
答案 0 :(得分:0)
它应该有一个与数组平行的数组,指示卡是否已从卡组中移除。
短语“表示”是否是需要布尔值的赠品。在您的情况下,这是一个包含52个布尔值的数组,因此bool is_removed[52];
。
[...]如何检查是否从原始数组中删除了某些内容?
您不想检查是否从原始数组中删除了某些内容,因为您实际上并不想从原始数组中删除任何内容。永远。相反,要从卡片中删除卡片,您只需将is_removed[i]
设置为true
(其中i
是合适的索引);并检查卡片是否已从卡座中移除,您只需检查is_removed[i]
。