Card Dealing Problem

This is a discussion on Card Dealing Problem within the C++ Programming forums, part of the General Programming Boards category; I have attempted to incorperate all the suggestions you guys have given and will continue on with a 1 dimensional ...

  1. #16
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I have attempted to incorperate all the suggestions you guys have given and will continue on with a 1 dimensional array deviating from the book a bit. Here is my code unfortunately I can
    't spot the error(s).
    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( char *,int,int);
    void deal(char [52],int [PLAYERS][CARDS]);
    void showhand(int [PLAYERS][CARDS],char [52],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
        char deck[52];
        
        for (int i=0;i<52;i++)
            deck[i]=i;
            
        int hands[PLAYERS][CARDS]={0};
        
        
        srand((unsigned)time(NULL));
        
        shuffle(deck,52,1);
        deal(deck,hands);
        showhand(hands,deck,face,suit);
        
        system("PAUSE");	
        return 0;
    }
    
    //shuffle cards in deck
    void shuffle(char deckPtr[52],  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 = deckPtr[random_pos_0];
        deckPtr[random_pos_0] = deckPtr[random_pos_1];
        deckPtr[random_pos_1] = cTemp;
      }
    }
    
    //deal cards in deck
    void showhand(int handsPtr[PLAYERS][CARDS],char * deckPtr, const char *wFace[],const char *wSuit[])
    {
       
           for (int player=0;player<=PLAYERS-1;player++){
                  cout << "\nPlayer number "<<player<<endl;
                  for (int c=0;c<=CARDS-1;c++)
                             cout << setw(5)<<right<<wFace[handsPtr[player][c]%13]
                                                   << " of " << setw(8)<< left
                                                   << wSuit[handsPtr[player][c]/4]<<endl;
           }
    }
    
    
    void deal(char deckPtr[52],int handsPtr[PLAYERS][CARDS])
    {
        if ((PLAYERS*CARDS)>52){
                    cout << "There are only 52 cards in the deck!"<<endl;
                    return;
        }
        for (int i,j=0; i<((PLAYERS*CARDS));i++,j++)
        {
             handsPtr[i % PLAYERS][i % CARDS]=deckPtr[j];
    	}
    
    }
    Here is the output:

    Player number 0
    Ace of Hearts
    Ace of Hearts
    Ace of Hearts
    Ace of Hearts
    Ace of Hearts

    Player number 1
    Ace of Hearts
    Ace of Hearts
    Ace of Hearts
    Ace of Hearts
    Ace of Hearts
    Press any key to continue . . .

  2. #17
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Nevermind got it working, the problem is in the for loop in the deal function.

Page 2 of 2 FirstFirst 12
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