gpt4 book ai didi

c - 试图让这个显示功能起作用

转载 作者:行者123 更新时间:2023-12-04 11:34:35 25 4
gpt4 key购买 nike

所以我试图让这个代码来绘制一张卡片,然后每次调用 printcards 时它都会打印以前的卡片和新卡片。现在它打印出一张卡然后打印出另一张,而我希望它在每次调用时不断更新手牌。

例子:第一次通话

打印:黑桃 A

第二次调用:

打印品:黑桃 A、红心 2

等...

为了巩固起见,假设这手牌永远不会超过 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--;
}
}

}

最佳答案

首先,一般来说,您的函数应该执行一个独特的、定义明确的工作。在这种情况下,您可能不希望您的卡片绘制功能也负责打印输出。此外,尚不清楚玩家功能在逻辑上代表什么。我建议您在重构代码时牢记这些要点。

其次,您需要在两次抽牌之间保持手牌状态。您在播放器函数中使用 int yourhand[10]; 暗示了这一点,但您从未使用过它。

我调整了抽牌功能以返回它抽到的牌,并在抽牌之间更新你的总手数:

绘图函数

int 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--;
}

return card;
//numCards++;
}

玩家功能

void players(int deck[])
{
int x;
int a;
int yourhand[10];
int handIndex = 0;
a = 1;

printf("Player 1 \n");
printf("Your Hand is: \n");
while(a == 1)
{
printf("What would you like to do: Press 1 to Draw. 2 to Stay. \n");
scanf("%d" , &x);
if(handIndex == 9)
{
break;
}
else if(x == 1)
{
yourhand[handIndex] = draw(deck);
}
else
{
a--;
}
printcards(yourhand, handIndex+1);
handIndex++;
}
}

关于c - 试图让这个显示功能起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com