3

所以我试图让这段代码绘制一张卡片,然后每次调用 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--;
     }
     }

}
4

1 回答 1

1

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

其次,你需要在抽牌之间保持你的手牌状态。您在播放器功能中暗示了这一点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++;
    }
}
于 2013-04-30T19:10:18.600 回答