Advertisement
jtliao

Untitled

May 2nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include<iomanip>
  4. using namespace std;
  5. void shuffle(int[],int[]);
  6. void dealcards(int[],int[],int[],int[],int&);
  7. bool checkflush(int[]);
  8. bool checkstraight(int[]);
  9. void same(int [],bool &,bool &,bool &,bool &,bool &);
  10. void winner(int[],int[],int&);
  11. int main()
  12. {
  13. int i,j,flushes,decks[52],deckn[52],high=0,highowner=0;
  14. int hands[5],handn[5],scored,count,cards=0,score[4],winners[4],numwin=0;
  15. bool flush,straight;
  16. bool pair,pair2,three,four,nothing;
  17. string suit[4]={"hearts","diamonds","spades","clubs"};
  18. string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
  19. int deal;
  20. srand(time(0));
  21. scored=0;
  22. shuffle(decks,deckn);
  23. for(i=0;i<4;i++)
  24. {
  25. dealcards(hands,handn,decks,deckn,cards);
  26. cout<<"\nHand: "<<i+1<<"\n";
  27. for(j=0;j<5;j++)
  28. cout<<card[handn[j]]<<"\t"<<suit[hands[j]]<<endl;
  29. flush=checkflush(hands);
  30. straight=checkstraight(handn);
  31. same(handn,nothing,pair,pair2,three,four);
  32. if(handn[5]>high)
  33. {high=handn[i];
  34. highowner=i;
  35. }
  36. cout<<"scoring:\n";
  37. if(straight&&flush)
  38. {cout<<"straight flush\n";
  39. score[i]=1;
  40. }
  41. else if(pair&&three)
  42. {cout<<"full house\n";
  43. score[i]=3;
  44. }
  45. else if(straight)
  46. {cout<<"straight\n";
  47. score[i]=5;
  48. }
  49. else if(flush)
  50. {cout<<"flush\n";
  51. score[i]=4;
  52. }
  53. else if(four)
  54. {cout<<"four of a kind\n";
  55. score[i]=2;
  56. }
  57. else if(three)
  58. {cout<<"Three of a kind\n";
  59. score[i]=6;
  60. }
  61. else if(pair2)
  62. {cout<<"2 pair\n";
  63. score[i]=7;
  64. }
  65. else if(pair)
  66. {cout<<"one pair\n";
  67. score[i]=8;
  68. }
  69. else
  70. {cout<<"nothing\n";
  71. score[i]=10;
  72. }
  73. }
  74. for(i=0;i<4;i++)
  75. if(highowner==i&&score[i]==10)
  76. score[i]=9;
  77. cout<<"\nThe winner is player(s) ";
  78. winner(score,winners,numwin);
  79. for(i=0;i<numwin;i++)
  80. cout<<winners[i]+1<<" ";
  81. cout<<endl;
  82. system("pause");
  83. return 0;
  84. }
  85. void winner(int score[],int win[],int &w)
  86. {int i,winning=0;
  87. for(i=1;i<4;i++)
  88. if(score[i]<score[winning])
  89. {w=0;
  90. win[w++]=i;
  91. winning=i;
  92. }
  93. else if(score[i]==score[winning])
  94. win[w++]=i;
  95.  
  96. }
  97.  
  98. bool checkstraight(int handn[])
  99. {int i,j,t;
  100. for(i=0;i<4;i++)
  101. for(j=i+1;j<5;j++)
  102. if(handn[i]>handn[j])
  103. {t=handn[i];
  104. handn[i]=handn[j];
  105. handn[j]=t;
  106. }
  107. for(i=1;i<5;i++)
  108. if(handn[i]!=handn[i-1]+1)
  109. return false;
  110. return true;
  111. }
  112. void same(int handn[],bool &nothing,bool &pair,bool &pair2,bool &three,bool &four)
  113. {bool used[13]={false};
  114. nothing=false;
  115. pair=false;
  116. pair2=false;
  117. three=false;
  118. four=false;
  119. int b[13]={0};
  120. int i,j;
  121. for( i = 0; i < 5; i++)
  122. {if(!used[handn[i]])
  123. {int count = 1;
  124. for(int j = i+1; j < 5; j++)
  125. {
  126. if(handn[j] == handn[i])
  127. {
  128. count++;
  129.  
  130. }
  131. }
  132. used[handn[i]]=true;
  133. b[handn[i]]=count;
  134. }
  135.  
  136. }
  137.  
  138.  
  139. cout<<endl;
  140. for(i=0;i<13;i++)
  141. if(b[i]==4)
  142. {four=true;
  143. return;
  144. }
  145. else if(b[i]==3)
  146. {three=true;
  147. for(j=0;j<13;j++)
  148. if(b[j]==2)
  149. pair=true;
  150. return;
  151. }
  152. else if(b[i]==2)
  153. {pair=true;
  154. for(j=0;j<13;j++)
  155. if(b[j]==3)
  156. three=true;
  157. for(j=i+1;j<13;j++)
  158. if(b[j]==2)
  159. {pair2=true;
  160. return;
  161. }
  162. return;
  163. }
  164. nothing=true;
  165. return;
  166. }
  167. void dealcards(int hands[],int handn[],int decks[],int deckn[],int & cards)
  168. {int i,j,t;
  169. int rank[]={13,2,3,4,5,6,7,8,9,10,11,12};
  170. for(i=0;i<5;i++)
  171. {hands[i]=decks[cards];
  172. handn[i]=deckn[cards];
  173. cards++;
  174. }
  175. for(i=0;i<4;i++)
  176. for(j=i+1;j<5;j++)
  177. if(rank[handn[i]]>rank[handn[j]])
  178. {t=hands[i];
  179. hands[i]=hands[j];
  180. hands[j]=t;
  181. t=handn[i];
  182. handn[i]=handn[j];
  183. handn[j]=t;
  184. }
  185. return;
  186. }
  187.  
  188. bool checkflush(int hands[])
  189. {int j,count=0,type,handcount;
  190. type=hands[0];
  191. handcount=1;
  192. for(j=1;j<5;j++)
  193. if(hands[j]==type)
  194. handcount++;
  195. if(handcount==5)
  196. return true;
  197. else
  198. return false;
  199. }
  200.  
  201. void shuffle(int decks[],int deckn[])
  202. {bool cards[4][13];
  203. int i,j,num,type;
  204. for(i=0;i<4;i++)
  205. for(j=0;j<13;j++)
  206. cards[i][j]=false;
  207.  
  208. for(j=0;j<52;j++)
  209. {do
  210. {
  211. num=rand()%13;
  212. type=rand()%4;
  213. }while(cards[type][num]);
  214. decks[j]=type;
  215. deckn[j]=num;
  216. cards[type][num]=true;
  217. }
  218.  
  219. return;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement