# Thread: Card Dealing Problem

1. ## 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

//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. I think I see the problem card is 1-5 where my hand index is 0-4 maybe if I subtract 1 doh.

3. Nope that didn't fix the main problem. It compiles but still crashes.

4. 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

//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. 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

//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. Thanks for the reply!

7. 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. i wish i knew more c++ so i could help u..........

9. 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

//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. 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. 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. 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.

13. 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. 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. 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?

Popular pages Recent additions