fork download
  1. #include<iostream>
  2. #include<time.h>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. using namespace std;
  6. enum CardSuit{CLUBS=1,DIAMONDS,HEARTS,SPADES};
  7. enum CardRank{ONE=1,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,
  8. QUEEN,KING,ACE};
  9. class PlayingCard{
  10. private:
  11. CardSuit suit;
  12. CardRank rank;
  13. public:
  14. PlayingCard(){
  15. suit=CLUBS;
  16. rank=ONE;
  17. }
  18. PlayingCard(CardSuit s,CardRank r){
  19. suit=s;
  20. rank=r;
  21. }
  22. void get_input(){
  23. int choice=0;
  24. do{
  25. cout<<CLUBS<<". CLUBS"<<endl;
  26. cout<<DIAMONDS<<". DIAMONDS"<<endl;
  27. cout<<HEARTS<<". HEARTS"<<endl;
  28. cout<<SPADES<<". SPADES"<<endl;
  29. cout<<"Enter the suit of the card:("<<CLUBS<<"-"<<SPADES<<")";
  30. cin>>choice;
  31. switch(choice){
  32. case CLUBS:suit=CLUBS;
  33. break;
  34. case DIAMONDS:suit=DIAMONDS;
  35. break;
  36. case SPADES:suit=SPADES;
  37. break;
  38. case HEARTS:suit=HEARTS;
  39. break;
  40. default:cout<<"Incorrect choice!"<<endl;
  41. }
  42. }while(choice>SPADES||choice<CLUBS);
  43. do{
  44. cout<<ONE<<". ONE"<<endl;
  45. cout<<TWO<<". TWO"<<endl;
  46. cout<<THREE<<". THREE"<<endl;
  47. cout<<FOUR<<". FOUR"<<endl;
  48. cout<<FIVE<<". FIVE"<<endl;
  49. cout<<SIX<<". SIX"<<endl;
  50. cout<<SEVEN<<". SEVEN"<<endl;
  51. cout<<EIGHT<<". EIGHT"<<endl;
  52. cout<<NINE<<". NINE"<<endl;
  53. cout<<TEN<<". TEN"<<endl;
  54. cout<<JACK<<". JACK"<<endl;
  55. cout<<QUEEN<<". QUEEN"<<endl;
  56. cout<<KING<<". KING"<<endl;
  57. cout<<ACE<<". ACE"<<endl;
  58. cout<<"Enter the suit of the card:("<<ONE<<"-"<<ACE<<")";
  59. cin>>choice;
  60. switch(choice){
  61. case ONE:rank=ONE;
  62. break;
  63. case TWO:rank=TWO;
  64. break;
  65. case THREE:rank=THREE;
  66. break;
  67. case FOUR:rank=FOUR;
  68. break;
  69. case FIVE:rank=FIVE;
  70. break;
  71. case SIX:rank=SIX;
  72. break;
  73. case SEVEN:rank=SEVEN;
  74. break;
  75. case EIGHT:rank=EIGHT;
  76. break;
  77. case NINE:rank=NINE;
  78. break;
  79. case JACK:rank=JACK;
  80. break;
  81. case QUEEN:rank=QUEEN;
  82. break;
  83. case KING:rank=KING;
  84. break;
  85. case ACE:rank=ACE;
  86. break;
  87. case TEN:rank=TEN;
  88. break;
  89. default:cout<<"Incorrect choice!"<<endl;
  90. }
  91. }while(choice>ACE||choice<ONE);
  92. }
  93. const CardRank get_rank(){
  94. return rank;
  95. }
  96. const CardSuit get_suit(){
  97. return suit;
  98. }
  99. void set_rank(CardRank r){
  100. if(r>ACE||r<ONE){
  101. cout<<"rank does not exist"<<endl;
  102. return;
  103. }
  104. rank=r;
  105. }
  106. void set_suit(CardSuit s){
  107. if(s>SPADES||s<CLUBS){
  108. cout<<"suit does not exist"<<endl;
  109. return;
  110. }
  111. suit=s;
  112. }
  113. void printCard(){
  114. switch(rank){
  115. case ONE:cout<<"ONE";
  116. break;
  117. case TWO:cout<<"TWO";
  118. break;
  119. case THREE:cout<<"THREE";
  120. break;
  121. case FOUR:cout<<"FOUR";
  122. break;
  123. case FIVE:cout<<"FIVE";
  124. break;
  125. case SIX:cout<<"SIX";
  126. break;
  127. case SEVEN:cout<<"SEVEN";
  128. break;
  129. case EIGHT:cout<<"EIGHT";
  130. break;
  131. case NINE:cout<<"NINE";
  132. break;
  133. case TEN:cout<<"TEN";
  134. break;
  135. case JACK:cout<<"JACK";
  136. break;
  137. case QUEEN:cout<<"QUEEN";
  138. break;
  139. case KING:cout<<"KING";
  140. break;
  141. case ACE:cout<<"ACE";
  142. break;
  143. }
  144. cout<<" of ";
  145. switch(suit){
  146. case SPADES:cout<<"SPADES";
  147. break;
  148. case HEARTS:cout<<"HEARTS";
  149. break;
  150. case DIAMONDS:cout<<"DIAMONDS";
  151. break;
  152. case CLUBS:cout<<"CLUBS";
  153. break;
  154. }
  155. cout<<endl;
  156. }
  157. };
  158. void shuffle(PlayingCard deck[],const int num){
  159. srand(time(NULL));
  160. int n;
  161. int chance;
  162. n=rand()%15+3;
  163. //cout<<"n ="<<n<<endl;
  164. PlayingCard d1[num],d2[num];
  165. int d1counter=0,d2counter=0;
  166. for(int i=0;i<n;i++){
  167. d1counter=0;
  168. d2counter=0;
  169. for(int j=0;j<num;j++){
  170. chance=rand()%2;
  171. if(chance)
  172. d1[d1counter++]=deck[j];
  173. else
  174. d2[d2counter++]=deck[j];
  175. }
  176. //cout<<"Shuffling\n";
  177. chance=rand()%2;
  178. if(chance){
  179. //cout<<"d1 first\n";
  180. for(int j=0;j<d1counter;j++){
  181. deck[j]=d1[j];
  182. }
  183. for(int j=0;j<d2counter;j++){
  184. deck[d1counter+j]=d2[j];
  185. }
  186. }else{
  187. //cout<<"d2 first\n";
  188. for(int j=0;j<d2counter;j++){
  189. deck[j]=d2[j];
  190. }
  191. for(int j=0;j<d1counter;j++){
  192. deck[d2counter+j]=d1[j];
  193. }
  194. }
  195. //cout<<"shuffled\n";
  196.  
  197. }
  198. }
  199. void printDeck(PlayingCard deck[],int size){
  200. for(int i=0;i<size;i++){
  201. deck[i].printCard();
  202. }
  203. }
  204. int main(){
  205. PlayingCard deck[52];
  206. PlayingCard player[4][13];
  207. char ch;
  208. int hand;
  209. CardSuit s;
  210. CardRank r;
  211. int x;
  212. for(int i=0;i<52;i++){
  213. x=(i/13)+1;
  214. s=(CardSuit)x;
  215. x=(i%13)+2;
  216. r=(CardRank)x;
  217. deck[i].set_suit(s);
  218. deck[i].set_rank(r);
  219. }
  220. cout<<"Original Deck:"<<endl;
  221. printDeck(deck,52);
  222. cout<<"Press enter to shuffle:";
  223. ch=getchar();
  224. shuffle(deck,52);
  225. cout<<"Deck after shuffling:"<<endl;
  226. printDeck(deck,52);
  227. cout<<"How many cards in one hand?(1-13):";
  228. cin>>hand;
  229. for(int i=0;i<4;i++){
  230. cout<<"\n\nPlayer "<<i+1<<"'s hand:"<<endl;
  231. for(int j=0;j<hand;j++){
  232. player[i][j]=deck[i*hand+j];
  233. player[i][j].printCard();
  234. }
  235. }
  236. cout<<"Press Enter to Exit"<<endl;
  237. ch=getchar();
  238. ch=getchar();
  239. return 0;
  240. }
Success #stdin #stdout 0s 2904KB
stdin
5

stdout
Original Deck:
TWO of CLUBS
THREE of CLUBS
FOUR of CLUBS
FIVE of CLUBS
SIX of CLUBS
SEVEN of CLUBS
EIGHT of CLUBS
NINE of CLUBS
TEN of CLUBS
JACK of CLUBS
QUEEN of CLUBS
KING of CLUBS
ACE of CLUBS
TWO of DIAMONDS
THREE of DIAMONDS
FOUR of DIAMONDS
FIVE of DIAMONDS
SIX of DIAMONDS
SEVEN of DIAMONDS
EIGHT of DIAMONDS
NINE of DIAMONDS
TEN of DIAMONDS
JACK of DIAMONDS
QUEEN of DIAMONDS
KING of DIAMONDS
ACE of DIAMONDS
TWO of HEARTS
THREE of HEARTS
FOUR of HEARTS
FIVE of HEARTS
SIX of HEARTS
SEVEN of HEARTS
EIGHT of HEARTS
NINE of HEARTS
TEN of HEARTS
JACK of HEARTS
QUEEN of HEARTS
KING of HEARTS
ACE of HEARTS
TWO of SPADES
THREE of SPADES
FOUR of SPADES
FIVE of SPADES
SIX of SPADES
SEVEN of SPADES
EIGHT of SPADES
NINE of SPADES
TEN of SPADES
JACK of SPADES
QUEEN of SPADES
KING of SPADES
ACE of SPADES
Press enter to shuffle:Deck after shuffling:
KING of DIAMONDS
SIX of SPADES
TEN of DIAMONDS
QUEEN of HEARTS
JACK of HEARTS
TWO of HEARTS
ACE of SPADES
EIGHT of CLUBS
SIX of DIAMONDS
FOUR of CLUBS
TWO of CLUBS
FIVE of SPADES
THREE of DIAMONDS
EIGHT of DIAMONDS
NINE of DIAMONDS
NINE of SPADES
ACE of CLUBS
JACK of SPADES
FIVE of DIAMONDS
TEN of CLUBS
SEVEN of HEARTS
THREE of SPADES
THREE of HEARTS
QUEEN of SPADES
FIVE of CLUBS
QUEEN of CLUBS
TWO of SPADES
JACK of CLUBS
TEN of HEARTS
KING of HEARTS
FOUR of SPADES
SEVEN of SPADES
JACK of DIAMONDS
FOUR of HEARTS
SIX of CLUBS
NINE of CLUBS
ACE of DIAMONDS
THREE of CLUBS
EIGHT of HEARTS
ACE of HEARTS
KING of CLUBS
KING of SPADES
FIVE of HEARTS
SIX of HEARTS
SEVEN of DIAMONDS
NINE of HEARTS
TWO of DIAMONDS
FOUR of DIAMONDS
SEVEN of CLUBS
QUEEN of DIAMONDS
TEN of SPADES
EIGHT of SPADES
How many cards in one hand?(1-13):

Player 1's hand:
KING of DIAMONDS
SIX of SPADES
TEN of DIAMONDS
QUEEN of HEARTS
JACK of HEARTS


Player 2's hand:
TWO of HEARTS
ACE of SPADES
EIGHT of CLUBS
SIX of DIAMONDS
FOUR of CLUBS


Player 3's hand:
TWO of CLUBS
FIVE of SPADES
THREE of DIAMONDS
EIGHT of DIAMONDS
NINE of DIAMONDS


Player 4's hand:
NINE of SPADES
ACE of CLUBS
JACK of SPADES
FIVE of DIAMONDS
TEN of CLUBS
Press Enter to Exit