【发布时间】:2013-10-12 19:47:03
【问题描述】:
到目前为止,我的代码旨在创建一副牌,每个套件的值为 9 - ACE,并向 4 位玩家发 5 张牌。这些牌是发牌3-2-3-2,然后是2-3-2-3,所以每个玩家总共有5张牌。稍后我将编写代码来洗牌,然后在 euchre 游戏中比较它们,但现在我只想发牌。无论如何,它并不完全有效,我愿意接受建议。我得到的错误是“分段错误:11”
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
int cards[24]; //array filled with card values
char DECK[24][25] =
{ //array filled with card names. Used for dealing //24elements long //25 characters in each element
"Ace of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades",
"Ace of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts",
"Ace of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs",
"Ace of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"
};
int main (void)
{
char P1[5][25];
char P2[5][25];
char P3[5][25];
char P4[5][25];
for( int i = 0 ; i < 24 ; ++i) // for the total amount of cards
{
for(int j = 0; j < 25; j++)
{
//1st deal 3-2-3-2
if(i < 3) // 0 - 2 // 3 cards
{
P1[i][j] = DECK[i][j];
}
if((i > 2) && (i < 5))// 3 - 4 // 2 cards
{
P2[i][j] = DECK[i][j];
}
if((i > 4) && (i < 8)) // 5 - 7 // 3 cards
{
P3[i][j] = DECK[i][j];
}
if((i > 7) && (i < 10))// 8 - 9 // 2 cards
{
P4[i][j] = DECK[i][j];
}
//2nd deal 2-3-2-3
if((i > 9) && (i < 12)) // 10 - 11 // 2 cards
{
P1[i][j] = DECK[i][j];
}
if((i > 11) && (i < 15))// 12 - 14 // 3 cards
{
P2[i][j] = DECK[i][j];
}
if((i > 14) && (i < 17)) // 15 - 16 // 2 cards
{
P3[i][j] = DECK[i][j];
}
if((i > 16) && (i < 20))// 17 - 19 // 3 cards
{
P4[i][j] = DECK[i][j];
}
}
}
for(int q = 0; q < 5; ++q)
{
cout << "P1 has the card " << P1[q]<< endl;
}
for(int q = 0; q < 5; ++q)
{
cout << "P2 has the card " << P2[q]<< endl;
}
//for(int q = 0; q < 5; ++q)
{
//cout << "P3 has the card " << P3[q]<< endl;
}
//for(int q = 0; q < 5; ++q)
{
//cout << "P4 has the card " << P4[q]<< endl;
}
return 0;
}
【问题讨论】:
-
您是否意识到您的
P1,P2,P3,P4的行范围为0-4?如果是,5-19ifs 是怎么回事?
标签: c++ arrays multidimensional-array segmentation-fault out-of-memory