结构数组初始化
嗨我正在开发一个程序,我必须初始化一副纸牌。我正在使用一个结构来表示一张卡片。然而,当我显示一副牌时,我没有正确填充它,因为我得到一堆零。我相信我的错误是在这一行,但我不知道:结构数组初始化
struct card temp = {"Clubs", value, false};
代码:
void initCards(){
int count = 0;
int location = 0;
const int hand = 12;
//add hearts
int value=2;
while(count < hand){
struct card temp = {"Hearts", value, false};
cards[location] = temp;
value++;
count++;
}
count = 0;
//add diamonts
value = 2;
while(count < hand){
struct card temp = {"Diamonds", value, false};
cards[count] = temp;
value++;
count++;
}
//add spades
count = 0;
value = 2;
while(count < hand){
struct card temp = {"Spades", value, false};
cards[count] = temp;
value++;
count++;
}
//add clubs
count = 0;
value = 2;
while(count < hand){
struct card temp = {"Clubs", value, false};
cards[count] = temp;
value++;
count++;
}
//print the deck
for(int i=0; i<52; i++){
cout << cards[i].type << " " << cards[i].rank << endl;
}
}
我不能相信我是用数作为我的迭代器...位置是什么我打算使用。而且自从我开始计算2时,手应该是13.有时候你只需要休息一下,然后回来找出错误。这工作正常:
void initCards(){
int count = 0;
int location = 0;
const int hand = 13;
//add hearts
int value=2;
while(count < hand){
struct card temp = {"Hearts", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
count = 0;
//add diamonts
value = 2;
while(count < hand){
struct card temp = {"Diamonds", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
//add spades
count = 0;
value = 2;
while(count < hand){
struct card temp = {"Spades", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
//add clubs
count = 0;
value = 2;
while(count < hand){
struct card temp = {"Clubs", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
for(int i=0; i<52; i++){
cout << cards[i].type << " " << cards[i].rank << endl;
}
}
您重置count
为零,每当你添加一个新的套装。所以,假设cards
足够大以容纳52个元素,但其中大多数不会被填充,因为您在开始时一直在写一个元素。
如果您发布了struct card
和cards
的声明,我们将更好地为您提供帮助。
+1以获取更多信息。 – 2010-05-10 21:26:06
卡片阵列总是被最后一种卡片类型覆盖。 你不应该重置计数为0,而不是你应该检查的条件时,如果低于它的价值+ 12
int temp = count + 12;
while(count < temp)
{
}
此外,我会reccommend你抱着你的卡在一个指针数组。这将使您节省大量的复制操作,因为在这种情况下,每次将卡片分配给数组索引时,都会复制整个结构对象。
除非您发现复制操作是热点,否则请勿将指针用于小型数据保持对象。在这种情况下,复制很可能可以忽略不计。 – 2010-05-10 21:24:17
下面是一些代码:
struct Card
{
virtual const std::string& get_suite_name(void) const = 0;
unsigned int value;
};
struct Spade_Card
: public Card
{
const std::string& get_suite_name(void) const
{
static const std::string name = "Spades";
return name;
}
};
struct Heart_Card
: public Card
{
const std::string& get_suite_name(void) const
{
static const std::string name = "Hearts";
return name;
}
};
struct Clubs_Card
: public Card
{
const std::string& get_suite_name(void) const
{
static const std::string name = "Clubs";
return name;
}
};
#define CARDS_IN_SUITE 13
#define NUM_SUITES 4
#define CARDS_IN_DECK ((CARDS_IN_SUITE) * (NUM_SUITES))
int main(void)
{
std::vector<Card *> deck;
// Create the hearts suite & add to the deck.
unsigned int i = 0;
for (i = 0; i < CARDS_IN_SUITE; ++i)
{
Card * p_card = new Spade_Card;
if (!p_card)
{
cerr << "Error allocating memory for a card." << endl;
return EXIT_FAILURE;
}
p_card->value = i + 1;
deck.push_back(p_card);
}
// Repeat for other suites.
for (i = 0; i < CARDS_IN_DECK; ++i)
{
delete deck[i]; // Good karma to deallocate memory.
}
return EXIT_SUCCESS;
}
该套件名称并不需要重复,每卡。 A卡分享套房名称以及其他12张卡。卡has-a套件名称。
也许这太过于OO了。 ;-)
真是一团糟...继承...滥用OOD中的概念......我的眼睛着火了!另外,你可以自动创建套装......如果你不像你那样依赖继承。一个简单的局部字符串填充名称“心”,“钻石”,“俱乐部”和“黑桃”将很好地与这样一个循环。当然,保持展开效率是一个更好的想法,但考虑到代码,我怀疑效率是否被记住。 :P +1为这样一个创造性的答案,但! :d – Dustin 2010-05-10 21:51:18
代码审查:试着看看你的代码,看看你是否可以减少重复自己。也可以在将来使用'for'循环来更容易地发现错误。编码之后,你经常停止使用'while'。 首先尝试通过每个套件的外部'for'循环。 – 2010-05-10 22:03:25