我想填写并打印出52张卡片,然后成功打印5手5张非重复卡片,但之后它无法正常工作。我怎么能解决这个问题?
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* handy typedefs */
typedef unsigned char card;
typedef unsigned char pairs;
/* arrays for the names of things */
static char *suits[4] = {"Hearts","Diamonds","Clubs","Spades"};
static char *values[13]= {"Ace","Two","Three","Four","Five","Six",/
"Seven","Eight","Nine","Ten","Jack","Queen","King"};
static char *colour[]= {"Black","Red"};
int main()
{
card deck[52][24],*deckp;
int s, c, a;
for (s = 0; s < 4; s++)
{
for(c = 0; c < 13; c++)
{
sprintf(deck[(s * c) + c], "%s of %s", values[c], suits[s]);
}
}
for(a = 0; a < 52; a++)
{
printf("%s\n", deck[a]);
}
int hand,cd,winner;
int iRand;
int i;
int irand;
srand(time(NULL)); /* seed the random number generator */
for(cd=0;cd<5;cd++)
{
for(hand=0;hand<5;hand++)
{
/* deal the hands here */
}
}
for (hand=0;hand<5;hand++)
{
printf("Hand %i:\n",hand+1 );
for ( i = 0; i < 5; i++) {
irand = (rand() % 52);
printf(" %s \n ", deck[irand]);
}
}
/* determine the winner and print it */
return 0;
}
void filldeck(card deck[52])
{
return;
}
void shuffle(card deck[52])
{
int i,rnd;
card c;
for(i=0;i<52;i++)
{
/* generate a random number between 0 & 51 */
rnd=rand() * 52.0 / RAND_MAX;
c = deck[i];
deck[i] = deck[rnd];
deck[rnd] = c;
}
}
答案 0 :(得分:0)
将deck[(s * c) + c]
替换为deck[s * 13 + c]
。