试图让这个显示功能工作

时间:2013-04-30 18:39:48

标签: c function printing

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

例如: 第一次打电话

打印:黑桃王牌

第二个电话:

版画:黑桃王牌,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--;
     }
     }

}

1 个答案:

答案 0 :(得分: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++;
    }
}