我试图在初始化一组卡片后重新洗牌。
该数组已正确初始化,但是当我在shuffle方法中逐步通过调试器时,甚至没有初始化几张卡。
我还注意到,在调用Shuffle之后,随机会随机初始化3张随机卡。
交换/随机播放功能:
void SwapCards(Card * cardOne, Card * cardTwo) //sending in address of two cards you want to swap
{
Card temp = *cardOne;
*cardOne = *cardTwo;
*cardTwo = temp;
}
//create a new array to be filled with randomized cards
void Shuffle(Card Deck[], int iSize)
{
int i;
srand(time(NULL));
for (i = 0; i < iSize; i++)
{
int randCard = rand() % 52;
SwapCards(Deck + i, Deck + randCard);
printf("The random number is %d\n", randCard);
}
}
主要:
int main(int argc, char** argv)
{
const int SuitSize = 14;
const int iSize = 52;
int i;
Face j;
Suits k;
Card Deck[52];
printf("\nUnshuffled Deck...\n");
//outer for loop is for suits = 4 x 13 = 52
for (k = Clubs; k <= Spades; k++)
{
//inner for loop is for face
for (j = Deuce; j <= Ace; j++)
{
Deck[j - Deuce + k * SuitSize].suit = k; //j-Deuce is 2 - face pos plus k * 13 k
Deck[j - Deuce + k * SuitSize].value = j;
ShowFace(j);
printf(" of ");
ShowSuit(k);
}
}
printf("\nShuffled Deck...\n");
Shuffle(Deck, iSize);
//outer loop for suits
for (k = Clubs; k <= Spades; k++)
{
//inner loop is for face
for (j = Deuce; j <= Ace; j++)
{
ShowFace(Deck[j - Deuce + k * SuitSize].value);
printf(" of ");
ShowSuit(Deck[j - Deuce + k * SuitSize].suit);
}
}
getchar();
}
枚举/结构声明:
typedef enum { Clubs , Diamonds, Hearts, Spades } Suits;
typedef enum { Deuce = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace } Face;
typedef struct {
Suits suit;
Face value;
} Card;