所以我试图让这段代码绘制一张卡片,然后每次调用 printcards 时都会打印前一张卡片和新卡片。现在它会打印出一张牌,然后是另一张牌,而我希望它在每次被跟注时不断更新手牌。
示例:第一次调用
印刷品:黑桃 A
第二次调用:
版画:黑桃 A,红桃 2
ETC...
为方便起见,假设手牌永远不会超过 10 张牌。
任何帮助将不胜感激。
这是代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 52
enum faces{Ace = 0, Jack = 10, Queen, King};
char * facecheck(int d);
void shuffle( int deck[]);
void draw(int deck[SIZE]);
void printcards(int hand[], int numCards);
void players(int deck[]);
int question();
int i, //numCards = 1;
int top = 52;
int main()
{
int deck[SIZE], i, a;
char suits[4][9] =
{
"Hearts",
"Diamonds",
"Clubs",
"Spades"};
srand( time( NULL ) ) ;
for(i = 0; i<SIZE; i++)
{
deck[i] = i;
};
shuffle(deck);
players(deck);
return 0;
}
char * facecheck(int d)
{
static char * face[] =
{
"Ace",
"Jack",
"Queen",
"King" };
if(d == Ace)
return face[0];
else
{
if(d == Jack)
return face[1];
else
{
if(d == Queen)
return face[2];
else
{
if(d == King)
return face[3];
}
}
}
}
void shuffle( int deck[])
{
int i, j, temp;
for(i = 0; i < SIZE; i++)
{
j = rand() % SIZE;
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
printf("The deck has been shuffled \n");
}
void draw(int deck[SIZE])
{
int numCards = 1;
int i;
int hand[numCards];
int card;
for(i = 0; i < numCards && top > 0; i++)
{
card = deck[top-1];
hand[i] = card;
top--;
}
printcards(hand, numCards);
//numCards++;
}
void printcards(int hand[], int numCard)
{
char suits[4][9] =
{
"Hearts",
"Diamonds",
"Clubs",
"Spades"};
for(i = 0; i < numCard; i++)
{
int card = hand[i];
if(card%13 == 0 || card%13 == 10 || card%13 == 11 || card%13 == 12)
printf("%s ", facecheck(card%13) );
else
printf("%d ", card%13+1);
printf("of %s \n", suits[card/13]);
}
}
void players(int deck[])
{
int x;
int a;
int yourhand[10];
a = 1;
printf("Player 1 \n");
printf("Your Hand is: \n");
draw(deck);
while(a == 1)
{
printf("What would you like to do: Press 1 to Draw. 2 to Stay. \n");
scanf("%d" , &x);
if(x == 1)
{
draw(deck);
}
else
{
a--;
}
}
}