Can some1 giv me some clues to cont the deal card to players part ??? The program will deal cards once the user enter the numbers of player between 3-7, either five cards per player or seven cards per player depending upon the menu selection, to the players at the table. The dealer, the program, always is dealt to last.

#include <iostream>
#include <string> // string
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;

struct card{
       char suit ;
       char rank ;
       }deck[52];
       


void GenerateRandom (card *ptrCard, int arraySize);
void Display (card *ptrCard, int arraySize);
void dealCard (card *ptrCard, int pNum, int cardSize);


const char *wSuit [] = {"Spades", "Hearts", "Clubs", "Diamonds"};
const char *wRank []= {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};



int main ()
{

  card deck[52];
  card *ptrCard = new card [52];
  
    int num , playerNum, numSelect ;
    srand (time (NULL));
      
 
    do{
    cout<<"1. Deal Five Cards"<<endl;
    cout<<"2. Deal Seven Cards"<<endl;
    cout<<"3. Quit the Game"<<endl;
    cout<<endl;
    cout<<"Enter Selection : "<<flush;
    cin>>numSelect;
    cout<<endl;
    
    switch (numSelect){
           case 1 : cout<<"You chosed deal five cards."<<endl;
                    cout << "\n\nGenerate Random Cards :\n\n";
                    num = 5;
                    
                    break;
           case 2 : cout<<"You Chosed deal seven cards."<<endl; 
                    cout << "\n\nGenerate Random Cards :\n\n";
                    num = 7;
                    
                    break;
           case 3 : exit (1);
                    break;
           default: cout<<"   Wrong Selection. Please choose again !!! "<<endl;
                    cout<<endl;
           }
       }while (numSelect > 3 || numSelect <= 0); 
    
    
    
 
  
    GenerateRandom (ptrCard, 52);
        
    Display (ptrCard, 52);
    dealCard (ptrCard, playerNum, num);  
       
       
    delete [] ptrCard; // delete card array    
    system("pause");
    return 0;
    
}
    
    
void GenerateRandom (card *ptrCard, int arraySize){
 
// this method will generate repeated random numbers.  boolean array
// keeps track of what numbers have been picked.  Generates another if
// this number has already been picked.

  
     bool* picked =  new bool[52];
     // initialize 52 cards to unpicked
     for (int i = 0; i < 52; i++)
         picked[i] = false;
         
     int value;
     //assumes all the generated cards are different in location for every new game
     for (int i = 0; i < arraySize; i++)
     {
         value = rand () % 52;  
         if (!picked[value])
         {
             ptrCard[i].rank = value;
             ptrCard[i].suit = value;      // card hasn't been picked then  assign into array,
             picked[value] = true;        // flag as picked.
             }
         else
             i--;  // already picked.
       }

     delete [] picked; 
}

void Display (card *ptrCard, int arraySize)
{
    for (int i = 0; i < arraySize; i++)
     {
          cout <<right << setw( 3 ) <<i + 1<< ". "<< right << setw( 5 ) << wRank[ptrCard[i].rank % 13] << " of "
               << left << setw( 8 ) <<wSuit[ptrCard[i].suit % 4]<< (  (i + 1) % 2 ? '\t' : '\n' ) ;
        
     }
}

void dealCard (card *ptrCard, int pNum, int cardSize)
{
     do{
     cout<<"Enter the players Number: "<<flush;
     cin>>pNum;
     }while (pNum < 3 || pNum > 7);
     
     int cardRange = pNum * cardSize;
     int *player = new int[pNum];
     
  /****************
     for (i=0; i<pNum ; i++)
     {                             ***HOW TO PASS GENERATED RANDOM CARDS TO THE PLAYERS ???
                                 
         player[i]
         } ************/
    
     for (int i=0 ; i < cardRange; i++)
     {
         
         cout <<right << setw( 3 ) <<i + 1<< ". "<< right << setw( 5 ) << wRank[ptrCard[i].rank % 13] << " of "
               << left << setw( 8 ) <<wSuit[ptrCard[i].suit % 4]<< (  (i + 1) % cardSize ? '\t' : '\n' ) ;
        
         
         }
         
     
     
     cout<<endl;
     
     delete [] player;
  
}

Recommended Answers

All 5 Replies

You'll want to pass your deal method a reference to your player array as an argument as well as a reference to the deck array. Then you'll transfer the card pointer from the deck array to the player's hand array.

A program like this would usually be part of an OOP lesson. In light of that, you may want to take another look at your program's structure.

You'll want to pass your deal method a reference to your player array as an argument as well as a reference to the deck array. Then you'll transfer the card pointer from the deck array to the player's hand array.

A program like this would usually be part of an OOP lesson. In light of that, you may want to take another look at your program's structure.

thanks 4 ur reply. i have to say i'm a newbie to pointer n dynamic programing, would u pls giv'me some examples ???

I probably can, but before I do, I think it best you share with us a summary of your current lesson. I think you may be missing the point of the lesson.

void dealCard (card *ptrCard, int &pNum, int cardSize)
{
          
     int cardRange = pNum * cardSize;
     int *player = new int[pNum];
     
  
     for (int i=0; i < pNum ; i++)
     {


}

Is there a reason you don't want to shuffle the cards in the deck to generate a random sequence of cards and then deal cards from the deck to each hand rather than assign random cards from a standardized deck sequence to a given player? The program flow could be something like this if wanted:

create deck
determine numCards
determine numPlayers
declare group of players:  
   Player * players = new Player[numPlayers];

shuffle deck 
deal numCards to numPlayers generating a hand of numCards for each player:
 for each player
   declare a hand of numCards 
   for each card in the hand
     assign appropriate card from deck to each card in the hand

To create a card selection protocol that will work for any number of players and any number of cards per playe generalize the protocol used to generate the following table which contains the index for each card in the deck to assign to a given hand if there are 3 players with 5 cards each:

hand #0 = 0 3 6  9 12
hand #1 = 1 4 7 10 13
hand #2 = 2 5 8 11 14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.