By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

C ++ Shuffling a deck of 52 cards, and handing 5 cards to 2 players

P: 1
I'm trying to shuffle a deck of card in C ++, with out having the same card twice. And I'm supposed to give each player 5 cards. And if they want, they can replace 3 cards. Right now I can assign 5 cards to each player, but some of the cards repeat themselces (ex. I have 3 Ace of spades, or 2 king of clubs, etc). Can anyone help me fix this problem. Here is what I have so far.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <ctime> 
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6.  
  7. using namespace std;
  8. using std::cout;
  9. using std::strncpy;
  10.  
  11. //int random;
  12.  
  13. int main() 
  14.    srand((unsigned)time(0));
  15.  
  16.     string  heart[] =  {"A\3","2\3","3\3","4\3","5\3","6\3","7\3","8\3","9\3","10\3","J\3","Q\3","K\3"};
  17.     string diamond[] = {"A\4","2\4","3\4","4\4","5\4","6\4","7\4","8\4","9\4","10\4","J\4","Q\4","K\4"};
  18.     string  club[] =   {"A\5","2\5","3\5","4\5","5\6","6\5","7\5","8\5","9\5","10\5","J\5","Q\5","K\5"};
  19.     string spade[] =   {"A\6","2\6","3\6","4\6","5\6","6\6","7\6","8\6","9\6","10\6","J\6","Q\6","K\6"};
  20.  
  21.  
  22.     string general[52] ; //= new string[52];
  23.     int k=0;
  24.  
  25.         for(int j = 0; j <= 12; j++)
  26.         {
  27.               general [k++] = heart[rand()% 12];
  28.               general [k++] = diamond[rand()% 12];
  29.               general [k++] = club[rand()% 12];
  30.               general [k++] = spade[rand()% 12];
  31.         }
  32.  
  33.  
  34.   int id1, id2;  
  35.   string temp;
  36.   for (int i = 0; i < 100; i++ )
  37.   {
  38.       id1 = rand()%52;
  39.       id2 = rand()%52;
  40.       if(id1!=id2)
  41.       {
  42.       temp = general[id1];
  43.       general[id1] = general[id2];
  44.       general[id2] = temp;
  45.       }
  46.  
  47.  }
  48.  for(int i=0; i<52; i++)
  49.  {
  50.  
  51. cout<< general[i]<<"  ";
  52. }
  53. cout << endl;
  54.  
  55.  
  56. int currentId = 1;
  57. string player1 [5];
  58. string player2 [5];
  59. int currentindex =0;
  60. for (int p = 0; p < 5; p++)
  61.     {
  62.  
  63.          player1 [p] = general [currentindex]; // picks the 0th,2nd,4th,6th,8th elements
  64.  
  65.          player2 [p] = general [currentId]; // picks the 1st,3rd,5th,7th,9th elements 
  66.          currentindex = currentindex + 2;
  67.          currentId = currentId +2;
  68.     }
  69.  
  70.     for (int z = 0;z < 5;z++)
  71.     {
  72.     cout << player1[z] <<endl;
  73. }
  74. cout <<"-------------------------------"<<endl;    
  75.  
  76. for (int t = 0; t< 5; t++)
  77. cout <<player2[t]<<endl;
  78.  
  79.  
  80.  
  81. system("pause");
  82.  
  83.  return 0;
  84. }
Mar 16 '07 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
There are many approaches to this problem.....

One would be to "empty" an arrays value once it has been chosen, and set a loop around the random number generator which only exits when it finds an non-empty spot. This can, however be a little tedious and expensive.

ANother idea, might be to have a list where you keep all of the cards in some order (point to the array entry you want from the element in the list). As you choose a random card, remove it from the list, and alter your random number generator to choose a random number over the size of the reamining list eg:

if I have 10 elements in a list
Then rand % 10 and remove the element.

I now have nine elements in the list so rand % 9

etc.
Mar 16 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
If you can use classes, I would recommend making a Card class to hold the suit and value, and a Deck class to hold 52 cards. You can write functions in the Deck class to select a card from the top of the deck and remove that card from the current list of cards - this will guarantee that you on't get a repeat of a card.
Mar 16 '07 #3

Savage
Expert 100+
P: 1,764
I'm trying to shuffle a deck of card in C ++, with out having the same card twice. And I'm supposed to give each player 5 cards. And if they want, they can replace 3 cards. Right now I can assign 5 cards to each player, but some of the cards repeat themselces (ex. I have 3 Ace of spades, or 2 king of clubs, etc). Can anyone help me fix this problem. Here is what I have so far.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <ctime> 
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6.  
  7. using namespace std;
  8. using std::cout;
  9. using std::strncpy;
  10.  
  11. //int random;
  12.  
  13. int main() 
  14.    srand((unsigned)time(0));
  15.  
  16.     string  heart[] =  {"A\3","2\3","3\3","4\3","5\3","6\3","7\3","8\3","9\3","10\3","J\3","Q\3","K\3"};
  17.     string diamond[] = {"A\4","2\4","3\4","4\4","5\4","6\4","7\4","8\4","9\4","10\4","J\4","Q\4","K\4"};
  18.     string  club[] =   {"A\5","2\5","3\5","4\5","5\6","6\5","7\5","8\5","9\5","10\5","J\5","Q\5","K\5"};
  19.     string spade[] =   {"A\6","2\6","3\6","4\6","5\6","6\6","7\6","8\6","9\6","10\6","J\6","Q\6","K\6"};
  20.  
  21.  
  22.     string general[52] ; //= new string[52];
  23.     int k=0;
  24.  
  25.         for(int j = 0; j <= 12; j++)
  26.         {
  27.               general [k++] = heart[rand()% 12];
  28.               general [k++] = diamond[rand()% 12];
  29.               general [k++] = club[rand()% 12];
  30.               general [k++] = spade[rand()% 12];
  31.         }
  32.  
  33.  
  34.   int id1, id2;  
  35.   string temp;
  36.   for (int i = 0; i < 100; i++ )
  37.   {
  38.       id1 = rand()%52;
  39.       id2 = rand()%52;
  40.       if(id1!=id2)
  41.       {
  42.       temp = general[id1];
  43.       general[id1] = general[id2];
  44.       general[id2] = temp;
  45.       }
  46.  
  47.  }
  48.  for(int i=0; i<52; i++)
  49.  {
  50.  
  51. cout<< general[i]<<"  ";
  52. }
  53. cout << endl;
  54.  
  55.  
  56. int currentId = 1;
  57. string player1 [5];
  58. string player2 [5];
  59. int currentindex =0;
  60. for (int p = 0; p < 5; p++)
  61.     {
  62.  
  63.          player1 [p] = general [currentindex]; // picks the 0th,2nd,4th,6th,8th elements
  64.  
  65.          player2 [p] = general [currentId]; // picks the 1st,3rd,5th,7th,9th elements 
  66.          currentindex = currentindex + 2;
  67.          currentId = currentId +2;
  68.     }
  69.  
  70.     for (int z = 0;z < 5;z++)
  71.     {
  72.     cout << player1[z] <<endl;
  73. }
  74. cout <<"-------------------------------"<<endl;    
  75.  
  76. for (int t = 0; t< 5; t++)
  77. cout <<player2[t]<<endl;
  78.  
  79.  
  80.  
  81. system("pause");
  82.  
  83.  return 0;
  84. }
You need to implement a do while loop in your for loop for generarting cards.the loop shoul serch your's arrays and reapet it self if the same card is generated

It should be like:

Expand|Select|Wrap|Line Numbers
  1. int card_type[12];
  2. do{
  3.             k=0;
  4.             for(int j = 0; j <= 12; j++)
  5.             {
  6.                   card_type[j] = rand()%12;
  7.                   general [k++] = heart[card_type[j]];
  8.                   general [k++] = diamond[card_type[j]];
  9.                   general [k++] = club[card_type[j]];
  10.                   general [k++] = spade[card_type[j]];
  11.              }  
  12.  
  13.      }while( card_type[0]==card_type[1] ............);

Savage
Mar 16 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.