need help with Card Dealing Program

This is a discussion on need help with Card Dealing Program within the C++ Programming forums, part of the General Programming Boards category; I'm currently making a card dealing program, it is suppose to display a list of cards like this: "Ace of ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    need help with Card Dealing Program

    I'm currently making a card dealing program, it is suppose to display a list of cards like this:
    "Ace of Heart, is red"
    "Two of Heart, is red"
    .
    .
    "Ace of Spade, is black"
    and so on for all suits and numbers.

    here is my current code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct card { 
     const char *face;
     const char *suit;
     const char *color;
    };
    
    typedef struct card Card;
    typedef unsigned char pairs;
    
    const char *face[] = { "Ace", "Two", "Three", "Four", "Five","Six", "Seven",
                                      "Eight", "Nine", "Ten","Jack", "Queen", "King"};
    const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
    const char *color[]= {"Black","Red"};
    void printdeck( const Card * const );
    void fillDeck( Card * const, const char *[], const char *[] ,const char *[]);
    void deal(card cards[52], card hands[5][5]);
    int main()
    {
    int hand,cd,winner;
    card hands[5][5],handssorted[5][5];
    pairs numpairs[5],highest;
    Card deck[52];
    srand( time( NULL) );
    fillDeck( deck, face, suit, color );
    printdeck(deck);
    printf("\n ----------------------------------------------------------\n");
    
    system("pause");
    return 0;
    
    }
    //------------------------------------------------------------------------------------------------------
    void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[], const char * wColor[])
    {
    		   int i;
    		   for ( i = 0; i <= 51; i++ ) { 
    		      wDeck[i].face  = wFace[ i % 13 ];
    		      wDeck[i].suit  = wSuit[ i / 13 ];
    		      wDeck[i].color = wColor[i%2];
            }
    }
    //-----------------------------------------------------------------------------------------------------
    void shuffle( Card * const wDeck )
    {
    		   int i, j;
    		   Card temp;
    		   for ( i = 0; i <= 51; i++ ) { 
    		      j = rand() % 52;
    		      temp = wDeck[ i ];
    		      wDeck[ i ] = wDeck[ j ];
    		      wDeck[ j ] = temp;
    }
    }
    //-----------------------------------------------------------------------------------------------------------
    void printdeck( const Card * const wDeck )
    {
               int i;
    		   for ( i = 0; i <= 51; i++ ){
    		      printf( "\t%s\t of \t%-8s is \t%s \n \t", wDeck[i].face, 
    		              wDeck[i].suit,wDeck[i].color,
    		             ( i + 1 ) % 2 ? '\t' : '\n' );}
    }
    but the output i get it wrong because it shows:
    "Ace of Hearts is Black"
    "Two of Hearts is Red"
    "Three of Hearts is Black"
    .
    .
    the color displayed is wrong. Im not sure what im doing wrong, can anyone pls help.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Think again about the sequences generated by your fillDeck function:
    Code:
    a 2 3 4 5 6 7 8 9 10 j q k a 2 ...
    h h h h h h h h h  h h h h d d ...
    b r b r b r b r b  r b r b r b ...

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Hi thanks for the reply. I tried to change the fill deck function to:
    Code:
    void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[], const char * wColor[])
    {
    		   int i;
    		   for ( i = 0; i <= 51; i++ ) { 
    		      wDeck[i].face  = wFace[ i % 13 ];
    		      wDeck[i].suit  = wSuit[ i / 13 ];
    		      wDeck[i].color = wColor[i / 13];
    }
    but the result gives null for Clubs and Spades, can you give me some ideas of what to do pls.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Why are you trying to store the colour seperately? The suit infers the colour.

    Code:
    const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
    const char *color[]= {"Black","Red"};
    Look, H and D are always red, (but you put black first)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Or if you insist:
    Code:
    wDeck[i].color = i/26;
    will give 0 when i is less than 26 (hearts & diamonds) and 1 otherwise.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    Consider not spreading this question everywhere on the net:
    Senior Advisor - http://www.unix.com
    Need help with Card Dealing Program - Dev Shed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM
  2. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 03:26 PM
  3. card game
    By rugger78 in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2004, 03:50 PM
  4. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21