943,013 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 244
  • C++ RSS
Sep 2nd, 2010
0

How to deal the Card ???

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string> // string
  3. #include <ctime>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. struct card{
  9. char suit ;
  10. char rank ;
  11. }deck[52];
  12.  
  13.  
  14.  
  15. void GenerateRandom (card *ptrCard, int arraySize);
  16. void Display (card *ptrCard, int arraySize);
  17. void dealCard (card *ptrCard, int pNum, int cardSize);
  18.  
  19.  
  20. const char *wSuit [] = {"Spades", "Hearts", "Clubs", "Diamonds"};
  21. const char *wRank []= {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
  22.  
  23.  
  24.  
  25. int main ()
  26. {
  27.  
  28. card deck[52];
  29. card *ptrCard = new card [52];
  30.  
  31. int num , playerNum, numSelect ;
  32. srand (time (NULL));
  33.  
  34.  
  35. do{
  36. cout<<"1. Deal Five Cards"<<endl;
  37. cout<<"2. Deal Seven Cards"<<endl;
  38. cout<<"3. Quit the Game"<<endl;
  39. cout<<endl;
  40. cout<<"Enter Selection : "<<flush;
  41. cin>>numSelect;
  42. cout<<endl;
  43.  
  44. switch (numSelect){
  45. case 1 : cout<<"You chosed deal five cards."<<endl;
  46. cout << "\n\nGenerate Random Cards :\n\n";
  47. num = 5;
  48.  
  49. break;
  50. case 2 : cout<<"You Chosed deal seven cards."<<endl;
  51. cout << "\n\nGenerate Random Cards :\n\n";
  52. num = 7;
  53.  
  54. break;
  55. case 3 : exit (1);
  56. break;
  57. default: cout<<" Wrong Selection. Please choose again !!! "<<endl;
  58. cout<<endl;
  59. }
  60. }while (numSelect > 3 || numSelect <= 0);
  61.  
  62.  
  63.  
  64.  
  65.  
  66. GenerateRandom (ptrCard, 52);
  67.  
  68. Display (ptrCard, 52);
  69. dealCard (ptrCard, playerNum, num);
  70.  
  71.  
  72. delete [] ptrCard; // delete card array
  73. system("pause");
  74. return 0;
  75.  
  76. }
  77.  
  78.  
  79. void GenerateRandom (card *ptrCard, int arraySize){
  80.  
  81. // this method will generate repeated random numbers. boolean array
  82. // keeps track of what numbers have been picked. Generates another if
  83. // this number has already been picked.
  84.  
  85.  
  86. bool* picked = new bool[52];
  87. // initialize 52 cards to unpicked
  88. for (int i = 0; i < 52; i++)
  89. picked[i] = false;
  90.  
  91. int value;
  92. //assumes all the generated cards are different in location for every new game
  93. for (int i = 0; i < arraySize; i++)
  94. {
  95. value = rand () % 52;
  96. if (!picked[value])
  97. {
  98. ptrCard[i].rank = value;
  99. ptrCard[i].suit = value; // card hasn't been picked then assign into array,
  100. picked[value] = true; // flag as picked.
  101. }
  102. else
  103. i--; // already picked.
  104. }
  105.  
  106. delete [] picked;
  107. }
  108.  
  109. void Display (card *ptrCard, int arraySize)
  110. {
  111. for (int i = 0; i < arraySize; i++)
  112. {
  113. cout <<right << setw( 3 ) <<i + 1<< ". "<< right << setw( 5 ) << wRank[ptrCard[i].rank % 13] << " of "
  114. << left << setw( 8 ) <<wSuit[ptrCard[i].suit % 4]<< ( (i + 1) % 2 ? '\t' : '\n' ) ;
  115.  
  116. }
  117. }
  118.  
  119. void dealCard (card *ptrCard, int pNum, int cardSize)
  120. {
  121. do{
  122. cout<<"Enter the players Number: "<<flush;
  123. cin>>pNum;
  124. }while (pNum < 3 || pNum > 7);
  125.  
  126. int cardRange = pNum * cardSize;
  127. int *player = new int[pNum];
  128.  
  129. /****************
  130.   for (i=0; i<pNum ; i++)
  131.   { ***HOW TO PASS GENERATED RANDOM CARDS TO THE PLAYERS ???
  132.  
  133.   player[i]
  134.   } ************/
  135.  
  136. for (int i=0 ; i < cardRange; i++)
  137. {
  138.  
  139. cout <<right << setw( 3 ) <<i + 1<< ". "<< right << setw( 5 ) << wRank[ptrCard[i].rank % 13] << " of "
  140. << left << setw( 8 ) <<wSuit[ptrCard[i].suit % 4]<< ( (i + 1) % cardSize ? '\t' : '\n' ) ;
  141.  
  142.  
  143. }
  144.  
  145.  
  146.  
  147. cout<<endl;
  148.  
  149. delete [] player;
  150.  
  151. }
Last edited by myd5258; Sep 2nd, 2010 at 6:19 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
myd5258 is offline Offline
5 posts
since Aug 2010
Sep 2nd, 2010
1
Re: How to deal the Card ???
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.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 2nd, 2010
0
Re: How to deal the Card ???
Click to Expand / Collapse  Quote originally posted by Fbody ...
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 ???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
myd5258 is offline Offline
5 posts
since Aug 2010
Sep 2nd, 2010
0
Re: How to deal the Card ???
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.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 2nd, 2010
0
Re: How to deal the Card ???
C++ Syntax (Toggle Plain Text)
  1. void dealCard (card *ptrCard, int &pNum, int cardSize)
  2. {
  3.  
  4. int cardRange = pNum * cardSize;
  5. int *player = new int[pNum];
  6.  
  7.  
  8. for (int i=0; i < pNum ; i++)
  9. {
  10.  
  11.  
  12. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
myd5258 is offline Offline
5 posts
since Aug 2010
Sep 2nd, 2010
0
Re: How to deal the Card ???
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:
C++ Syntax (Toggle Plain Text)
  1. create deck
  2. determine numCards
  3. determine numPlayers
  4. declare group of players:
  5. Player * players = new Player[numPlayers];
  6.  
  7. shuffle deck
  8. deal numCards to numPlayers generating a hand of numCards for each player:
  9. for each player
  10. declare a hand of numCards
  11. for each card in the hand
  12. 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:
C++ Syntax (Toggle Plain Text)
  1. hand #0 = 0 3 6 9 12
  2. hand #1 = 1 4 7 10 13
  3. hand #2 = 2 5 8 11 14
Last edited by Lerner; Sep 2nd, 2010 at 11:32 am.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Variable arguments to lookup table function pointers
Next Thread in C++ Forum Timeline: C++ Library Training Needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC