# Card Dealing Program

Page 1 of 1

## 4 Replies - 13457 Views - Last Post: 24 February 2009 - 06:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=88843&amp;s=5168d17ebf36bcefcd573695ce90021d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sean123

Reputation: 0
• Posts: 5
• Joined: 24-February 09

# Card Dealing Program

Posted 24 February 2009 - 05:29 PM

Ok the program is supposed to deal 5 cards out to 4 players. I'm just beginning so try to keep that in mind when giving advice that things like 2D arrays which would be extremely useful in this scenario are not an option. So I first filled an array with 0-51 to make the deck. I then shuffled the deck and handed it out to the 4 players. Now I have some numbers printed out as cards in the players hand. I need to make it so those numbers are somehow changed to suits/and numbered cards. To do this I plan to use card[n]/13 to give me 0,1,2,3 then each of those numbers will signify a suit. Then do (card[n]%13)+1 to determine what card it is...(1=ace, 11=Jack, 12 = Queen, 13 = King). The problem I am having is implementing this into the program. If someone could point me in the direction I would be very appreciative.

```#
#include <iostream>
#
#include <cmath>
#
#include <cstdlib>
#
#include <ctime>
#

#
using namespace std;
#

#
int main(void)
#
{
#
int card[52], player1[4], player2[4], player3[4], player4[4], temp;//Declare variables and set up arrays
#
srand((unsigned)time(0));//Time function to use assure a random shuffle
#
//Set up deck
#
for (int i=0; i<52; i++)
#
{
#
card[i] = i;
#
}
#
//Shuffle deck by swapping the first card with a random card
#
for (int j=0; j<52;j++)
#
{
#
int swap = rand()%52;
#
temp = card[j];
#
card[j] = card[swap];
#
card[swap] = temp;
#
}
#
//Deal out hands
#
for (int k=0; k<5; k++)
#
{
#
player1[k]=card[k];
#
cout<<"p1 "<<player1[k]<<endl;
#
}
#

#
for (int l=5; l<10; l++)
#
{
#
player2[l]=card[l];
#
cout<<"p2 "<<player2[l]<<endl;
#
}
#

#
for (int m=10; m<15; m++)
#
{
#
player3[m]=card[m];
#
cout<<"p3 "<<player3[m]<<endl;
#
}
#
for (int n=15; n<20; n++)
#
{
#
player4[n]=card[n];
#
cout<<"p4 "<<player4[n]<<endl;
#
}
#
return 0;
#
}
```

Sorry about the number signs the only access I had to my source was from pastebin. http://www.seanhelp....n.com/m2b2a34b6

This post has been edited by sean123: 24 February 2009 - 05:45 PM

Is This A Good Question/Topic? 0

## Replies To: Card Dealing Program

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Card Dealing Program

Posted 24 February 2009 - 06:14 PM

Does it have to be this way? Any chance we could make Card objects etc...?

### #3 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: Card Dealing Program

Posted 24 February 2009 - 06:16 PM

Given all the #'s in the code you posted I assume you copied it from another site and are trying to alter it to fit your needs?

### #4 sean123

Reputation: 0
• Posts: 5
• Joined: 24-February 09

## Re: Card Dealing Program

Posted 24 February 2009 - 06:16 PM

Yeah unfortunately it has to be this way as we haven't learned very much yet.

And psycho coder that is incorrect the # signs are because I wrote most of the program in the lab at my university and then put it on pastebin so I could have it anywhere I went. Kind of pathetic if I had to copy that code from someone...

```	  #include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void)
{
int card[52], player1[4], player2[4], player3[4], player4[4], temp, number, suit;//Declare variables and set up arrays
srand((unsigned)time(0));//Time function to use assure a random shuffle
//Set up deck
for (int i=0; i<52; i++)
{
card[i] = i;
}
//Shuffle deck by swapping the first card with a random card
for (int j=0; j<52;j++)
{
int swap = rand()%52;
temp = card[j];
card[j] = card[swap];
card[swap] = temp;
}
//Deal out hands
for (int k=0; k<5; k++)
{
player1[k]=card[k];
suit=card[k]/13;
number=(card[k] % 13) + 1;
}
if (number == 11)
{
cout<<"Player's card is a Jack";
}
else if (number == 12)
{
cout<<"Player's card is a Queen";
}
else if (number == 12)
{
cout<<"Player's card is a King";
}
else if (number == 1)
{
cout<<"Player's card is an Ace";
}
else
{
cout<<"Player's card is "<<number;
}
for (int l=5; l<10; l++)
{
player2[l]=card[l];
cout<<"p2 "<<player2[l]<<endl;
}
for (int m=10; m<15; m++)
{
player3[m]=card[m];
cout<<"p3 "<<player3[m]<<endl;
}
for (int n=15; n<20; n++)
{
player4[n]=card[n];
cout<<"p4 "<<player4[n]<<endl;
}
return 0;
}
```

Here's what I just have been adding and realizing very quickly that it's inefficient, a huge waste of time and not working...Someone has to know a better way to do this. Also can't figure out how to make it say the suit as well.

So then I moved to using a case/switch and have come up with...
```switch(card/13):
{
case 1: return "Diamonds";
case 2: return "Hearts";
case 3: return "Clubs";
default: return "Impossible";
}

switch(card%13):
{
case 0: return "Ace";
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five":
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
case 10: return "Jack";
case 11: return "Queen";
case 12: return "King";
default: return "Impossible";
}
```

We barely touched these type of statements so I'm not not quite sure how I'm doing.

This post has been edited by sean123: 24 February 2009 - 06:48 PM

### #5 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: Card Dealing Program

Posted 24 February 2009 - 06:32 PM

Just making sure, you would be surprised at how many people do that. I dont want you to think I was accusing you of anything though