Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get this display function to work

So I'm trying to get this code to draw a card, and then each time printcards in called it prints the previous card and the new card. Right now it it prints off one card and then another while I want it to constantly update the hand every time it's called.

example: first call

prints: Ace of Spades

Second call:

Prints: Ace of Spades, 2 of Hearts

etc...

for sake of agrument assume the hand will never grow bigger than 10 cards.

Any help will be appreciated.

Here is the code:

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

}
like image 296
Cody Rogers Avatar asked Jun 07 '26 05:06

Cody Rogers


1 Answers

First, in general, your functions should perform one distinct, well-defined piece of work. In this case, you probably don't want your card drawing function to also be responsible for printing output. In addition, it's not clear what the players function logically represents. I'd recommend you refactor the code with these points in mind.

Second, you'll need to persist your hand state between card draws. You hinted at this with int yourhand[10]; in the players function, but you never used it.

I adjusted the draw function to return the card it pulled, and to update your total hand between draws:

Draw Function

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

Players Function

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++;
    }
}
like image 161
eriknelson Avatar answered Jun 10 '26 16:06

eriknelson