Card Dealing Problem

This is a discussion on Card Dealing Problem within the C++ Programming forums, part of the General Programming Boards category; This is an exercise I am doing from C++ How to Program fourth edition by Deitel an Deitel. The problem ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Card Dealing Problem

    This is an exercise I am doing from C++ How to Program fourth edition by Deitel an Deitel.

    The problem is to take a given program and have it deal a five hand card.

    here is my code.
    Code:
    #include <iostream>
    using std::cout;
    using std::left;
    using std::right;
    using std::endl;
    
    #include <stdlib.h>
    #include <iomanip>
    using std::setw;
    
    #include <cstdlib>
    #include <ctime>
    
    void shuffle( int[][13]);
    void deal(char * [],const int[][13],const char*[],const char *[]);
    
    int main(int argc, char *argv[])
    {
        //initialize suit array
        const char *suit[4]={"Hearts","Diamonds","Clubs","Spades"};
            
        //initialize face array
        const char *face[13]={"Ace","Deuce","Three","Four","Five","Six",
            "Seven","Eight","Nine","Ten","Jack","Queen","King"};
            
        
        //initialize deck array
        int deck[4][13]={0};
        
        char * hand[5]={""};
        
        
        srand(time(0));
        
        shuffle(deck);
        deal(hand,deck,face,suit);
        
        system("PAUSE");	
        return 0;
    }
    
    //shuffle cards in deck
    void shuffle(int wDeck[][13])
    {
        int row;
        int column;
        
        //for each of the 52 cards choose a slot of the deck randomly
        for (int card=1;card<=52;card++){
            do{
                    row=rand()%4;
                    column=rand()%13;
            }while (wDeck[row][column] != 0);
            
            wDeck[row][column]=card;
        }
    }
    
    //deal cards in deck
    void deal(char * thand[],const int wDeck[][13], const char *wFace[],const char *wSuit[])
    {
        //for each of the 52 cards
        for (int card=1;card<=5;card++)
            //loop through the rows of the wDeck
            for (int row=0; row<=3;row++)
                    //loop through the columns of the deck for current row
                    for(int column=0;column<=12;column++)
                                    if(wDeck[row][column]==card){
                                         strcat(thand[card],wFace[column]);
                                         strcat(thand[card],wSuit[row]);
                                     }
    }
    
    void showhand(char * thand[],const int wDeck[][13], const char *wFace[],const char *wSuit[])
    {
        for (int card=1;card<=5;card++)
            cout << thand[card]<<endl;  
    }
    I am attempting to concatenate the suit and face number into a string called hand that I will later check for pairs, three of a kind etc, but the program crashes on me.

    ignore

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I think I see the problem card is 1-5 where my hand index is 0-4 maybe if I subtract 1 doh.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Nope that didn't fix the main problem. It compiles but still crashes.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Updated code
    Code:
    #include <iostream>
    using std::cout;
    using std::left;
    using std::right;
    using std::endl;
    
    #include <stdlib.h>
    #include <iomanip>
    using std::setw;
    
    #include <cstdlib>
    #include <ctime>
    
    void shuffle( int[][13]);
    void deal(char * [],const int[][13],const char*[],const char *[]);
    void showhand(char * []);
    
    int main(int argc, char *argv[])
    {
        //initialize suit array
        const char *suit[4]={"Hearts","Diamonds","Clubs","Spades"};
            
        //initialize face array
        const char *face[13]={"Ace","Deuce","Three","Four","Five","Six",
            "Seven","Eight","Nine","Ten","Jack","Queen","King"};
            
        
        //initialize deck array
        int deck[4][13]={0};
        
        char * hand[5]={""};
        
        
        srand(time(0));
        
        shuffle(deck);
        deal(hand,deck,face,suit);
        showhand(hand);
        
        system("PAUSE");	
        return 0;
    }
    
    //shuffle cards in deck
    void shuffle(int wDeck[][13])
    {
        int row;
        int column;
        
        //for each of the 52 cards choose a slot of the deck randomly
        for (int card=1;card<=52;card++){
            do{
                    row=rand()%4;
                    column=rand()%13;
            }while (wDeck[row][column] != 0);
            
            wDeck[row][column]=card;
        }
    }
    
    //deal cards in deck
    void deal(char * thand[],const int wDeck[][13], const char *wFace[],const char *wSuit[])
    {
        //for each of the 52 cards
        for (int card=1;card<=5;card++)
            //loop through the rows of the wDeck
            for (int row=0; row<=3;row++)
                    //loop through the columns of the deck for current row
                    for(int column=0;column<=12;column++)
                                    if(wDeck[row][column]==card){
                                         strcat(thand[card-1],wFace[column]);
                                         strcat(thand[card-1],wSuit[row]);
                                     }
    }
    
    void showhand(char * thand[])
    {
        for (int card=0;card<=4;card++)
            cout << thand[card]<<endl;  
    }

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    66
    Code:
    #include <iostream>
    using std::cout;
    using std::left;
    using std::right;
    using std::endl;
    
    #include <stdlib.h>
    #include <iomanip>
    using std::setw;
    
    #include <cstdlib>
    #include <ctime>
    
    
    //shuffle cards in deck
    void shuffle(int wDeck[][13])
    {
        int row;
        int column;
        
        //for each of the 52 cards choose a slot of the deck randomly
        for (int card=1;card<=52;card++){
            do{
                    row=rand()%4;
                    column=rand()%13;
            }while (wDeck[row][column] != 0);
            
            wDeck[row][column]=card;
        }
    }
    
    //deal cards in deck
    void deal(char thand[5][20],const int wDeck[][13], const char *wFace[],const char *wSuit[])
    {
        //for each of the 52 cards
        for (int card=1;card<=5;card++)
            //loop through the rows of the wDeck
            for (int row=0; row<=3;row++)
                    //loop through the columns of the deck for current row
                    for(int column=0;column<=12;column++)
                                    if(wDeck[row][column]==card){
                                         strcat(thand[card-1],wFace[column]);
                                         strcat(thand[card-1],wSuit[row]);
                                     }
    }
    
    void showhand(char thand[5][20])
    {
        for (int card=0;card<=4;card++)
            cout << thand[card] << endl;  
    }
    
    int main(int argc, char *argv[])
    {
        //initialize suit array
        const char *suit[4]={"Hearts","Diamonds","Clubs","Spades"};
            
        //initialize face array
        const char *face[13]={"Ace","Deuce","Three","Four","Five","Six",
            "Seven","Eight","Nine","Ten","Jack","Queen","King"};
            
        
        //initialize deck array
        int deck[4][13]={0};
        
        char hand[5][20] = {"", "", "", "", ""};
        
        
        srand(time(0));
        
        shuffle(deck);
        deal(hand,deck,face,suit);
        showhand(hand);
        
        system("PAUSE");	
        return 0;
    }
    This should get you moving in the right direction. You allocated an array for you hand but forgot to allocate the actual contents...

    char *hand[5] allocates array of 5 pointers...

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks for the reply!

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Have rethought the problem a bit and see I can't use char * [5] for the hand.
    For these reasons
    if I set it to "" the size is set and strcat doesn't allocate for the new space needed hence the crash (guess you have to make half the mistakes you read about to learn the lesson).

    two it won't help me to determine the hand if i create the hand this way so it's back to the drawing board.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    i wish i knew more c++ so i could help u..........

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    My newly revised code works this way.
    Code:
    #include <iostream>
    using std::cout;
    using std::left;
    using std::right;
    using std::endl;
    
    #include <stdlib.h>
    #include <iomanip>
    using std::setw;
    
    #include <cstdlib>
    #include <ctime>
    
    const int PLAYERS=2;
    const int CARDS=5;
    
    void shuffle( int[][13]);
    void deal(int,int,int [PLAYERS][CARDS]);
    void showhand(int [PLAYERS][CARDS],const int [][13], const char *[],const char *[]);
    
    int main(int argc, char *argv[])
    {
        
       
        //initialize suit array
        
        const char *suit[4]={"Hearts","Diamonds","Clubs","Spades"};
            
        //initialize face array
        const char *face[13]={"Ace","Deuce","Three","Four","Five","Six",
            "Seven","Eight","Nine","Ten","Jack","Queen","King"};
            
        
        //initialize deck array
        int deck[4][13]={0};
        
        int hands[PLAYERS][CARDS]={0};
        
        
        srand(time(0));
        
        shuffle(deck);
        deal(PLAYERS,CARDS,hands);
        showhand(hands,deck,face,suit);
        
        system("PAUSE");	
        return 0;
    }
    
    //shuffle cards in deck
    void shuffle(int wDeck[][13])
    {
        int row;
        int column;
        
        //for each of the 52 cards choose a slot of the deck randomly
        for (int card=1;card<=52;card++){
            do{
                    row=rand()%4;
                    column=rand()%13;
            }while (wDeck[row][column] != 0);
            
            wDeck[row][column]=card;
        }
    }
    
    //deal cards in deck
    void showhand(int tPlayers[PLAYERS][CARDS],const int wDeck[][13], const char *wFace[],const char *wSuit[])
    {
        int card=0;
        //for each of the 52 cards
        for (int player=0;player<=1;player++){
           cout << "\nPlayer number "<<player<<endl;
           for (int c=0;c<=4;c++)
               for (int row=0; row<=3;row++)
                        for(int column=0;column<=12;column++)
                                                    if (wDeck[row][column]==tPlayers[player][c]) {
                                                        cout << setw(5)<<right<<wFace[column]
                                                        << " of " << setw(8)<<left
                                                        << wSuit[row]<<endl;
                    }
                                         
        }
    }
    
    void deal(int tPlayers, int number_of_cards,int tHands[PLAYERS][CARDS])
    {
        if ((tPlayers*number_of_cards)>52){
            cout << "There are only 52 cards in the deck!"<<endl;
            return;
            }
        
        for (int i=0; i<((tPlayers*number_of_cards));i++){
            tHands[i % tPlayers][i % number_of_cards]=i+1;
                 
            
        }
    }

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    curlious : Im not sure I understand your program. You shuffle function place the number 1 to 52 randomly in a two dimentional array. If at position wDeck[2][7] you find the value 36, what card is that ? I just dont follow.

    In the deal function you send the number of card, the number of players and a empty hands[][] array but you dont seem to send the newly shuffled deck ... confussed.

    There cant be a return in the code below coz it's void function.
    Code:
    void deal(int tPlayers, int number_of_cards,int tHands[PLAYERS][CARDS])
    {
        if ((tPlayers*number_of_cards)>52){
            cout << "There are only 52 cards in the deck!"<<endl;
            return;
            }
        
           for (int i=0; i<((tPlayers*number_of_cards));i++){
            tHands[i % tPlayers][i % number_of_cards]=i+1;    
        }
    }
    Dont understand your for loop either. It will produce the same array each time, so in other words every player will get the same card everytime.

    Not sure this helped you much.

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    66
    Can I recommend changing the card deck from a 2 dimensional array to a simple char array.


    For example:

    char Deck[52];

    Now just define the value of the character in the deck as:
    Hearts 1-14
    Clubs 15-28, etc...

    It is much easier to suffle a char array also.

    If you want to find out what card it is:

    char temp = Deck[i] - 1; // You want zero in array to be used for NULL termination if you want to debug

    char cCard = Card % 4;
    char cSuit = Card / 4; (or Deck[i] >> 2 if you want to be clever)

    Now cChar is a value 0-13 and cSuit is a value 0-3... Now you don't have to deal with 2 dimensional arrays and can make shuffling a trivial algorithm...

    Code:
    void shuffle(char *deck,  int deck_size, int shuffle_count)
    {
      char cTemp;
      int random_pos_0, random_pos_1;
    
      srand( (unsigned)time( NULL ) );
    
      for (int i=0; i<shuffle_count; ++i)
      {
        random_pos_0 = rand() * deck_size / RAND_MAX;
        random_pos_1 = rand() * deck_size / RAND_MAX;
        cTemp = deck[random_pos_0];
        deck[random_pos_0] = deck[random_pos_1];
        deck[random_pos_1] = cTemp;
      }
    }

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    curlious: Are you still having problems? Looks like it works to me...even though it seems a little backwards. What about this?
    Code:
    //pseudo
    init:
    int deck[52];
    int deckPtr = 0;
    for(int i = 0; i < 52; i++)
    	deck[i] = i;
    
    shuffle:
    for(int i = 0; i<52; i++)
    {
    	randInt = rand()%52; // I wouldn't normally generate random numbers this way
    	swap(deck[i], deck[randInt]);
    }
    
    deal:
    for (int i=0; i<((tPlayers*number_of_cards));i++,deckPtr++)
    {
    	tHands[i % tPlayers][i % number_of_cards]=deck[deckPtr];
    }
    
    show:
    for(each player p)
    	for(each card in hand c)
    		cout << setw(5)<<right<<wFace[tHands[p][c]%13]
    						<< " of " << setw(8)<<left
    						<< wSuit[tHands[p][c]/4]<<endl;
    laasunde: There can be a return in a void function...you just can't return a value.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by pianorain
    laasunde: There can be a return in a void function...you just can't return a value.
    So whats the point of having a return if it doesnt return a value ? Does the functin end when you do a return; ?

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    66
    This is off the current topid btw...

    Code:
    void function_that_returns_nothing()
    {
      return;   // you can have a return statement
    }
    or a more complex reseason for having a return statement:

    Code:
    void foo(const char *bar)
    {
      if (!bar)
        return;
    
      // do some useful stuff here
    }

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Sorry it has taken so long to reply!

    The reason I represent the desk with a two demensional array is because that was the given in the problem that is from Dietel and Dietels C++ How to Program Fourth Edition.

    Card [2][7] = 32 for instance would mean the seven of Diamonds is the thirty second card in the deck. By dealing the first ten cards to my hand array so to speak, it is taking the cards off the top of the deck.

    Admitidly as was pointed out I do not return the remainder of the deck or alter the array to reflect only a partial deck remaining, but as to returning a shuffled deck after dealing, that is not done.

    Finally because of the two-d deck representation I have been considering skipping the problem as I think it an inefficient way of dealing with the data when determining hands.

    What do you think continue on or complete the remaining exercises dealing with this piece of code?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Critical problem dealing with a copy and pasted version
    By DivingPhoenix in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2007, 02:41 AM
  3. Freecell game
    By SmellsLikeToast in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2006, 03:01 PM
  4. A retrun card problem
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 01:01 PM
  5. Wierd problem
    By Olidivera in forum C++ Programming
    Replies: 17
    Last Post: 05-06-2005, 04:50 AM

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