Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Arrays;
  3.  
  4. public class Main
  5. {
  6. public static void main(String[] args) {
  7.  
  8.  
  9. int[] playerCards = {2, 2, 3, 3, 4};
  10. int[] playerSuits = new int[5];
  11.  
  12. String[] suits = { "Spades", "Clubs", "Hearts", "Diamonds" };
  13. String[] cards = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
  14.  
  15. Random rand = new Random();
  16.  
  17. for(int i = 0; i < playerCards.length; i++)
  18. {
  19. int randomCard = rand.nextInt(13) + 2;
  20. int randomSuit = rand.nextInt(4) + 1;
  21.  
  22. // playerCards[i] = randomCard;
  23. playerSuits[i] = randomSuit;
  24.  
  25. }
  26.  
  27. Arrays.sort(playerCards);
  28. Arrays.sort(playerSuits);
  29.  
  30. int highestCard = 0;
  31. boolean isSuitAllSame = false;
  32. boolean isRoyalFlush = false;
  33. boolean isStraightFlush = false;
  34. boolean isFlush = false;
  35. boolean isStraight = false;
  36.  
  37. int pairs = 0;
  38. boolean isThree = false;
  39. boolean isFour = false;
  40.  
  41. //--------------------------------------------------
  42. int firstSuit = playerSuits[0];
  43.  
  44. int k;
  45.  
  46. for(k = 0; k < playerSuits.length; k++)
  47. {
  48. if(firstSuit != playerSuits[k])
  49. {
  50. break;
  51. }
  52. }
  53.  
  54. if(k == playerSuits.length) isSuitAllSame = true;
  55.  
  56.  
  57. int[] temp = new int[5];
  58. int tempCount = 0;
  59.  
  60. for(int i = 0; i < playerCards.length; i++)
  61. {
  62. if(playerCards[i] >= 10 && playerCards[i] <= 14)
  63. {
  64. for(int j = 0; j < temp.length; j++)
  65. {
  66. if(temp[j] == 0)
  67. {
  68. temp[j] = playerCards[i];
  69. tempCount++;
  70. break;
  71. }
  72.  
  73. if(temp[j] == playerCards[i])
  74. break;
  75. }
  76. }
  77. }
  78.  
  79. if(tempCount == 5 && isSuitAllSame)
  80. {
  81. isRoyalFlush = true;
  82. }
  83. else
  84. {
  85. int l;
  86.  
  87. for(l = 0; l < playerCards.length; l++)
  88. {
  89. if(l != (playerCards.length - 1) && playerCards[l] != playerCards[l + 1] - 1)
  90. {
  91. break;
  92. }
  93. }
  94.  
  95. if(l == playerCards.length)
  96. {
  97. if(isSuitAllSame)
  98. isStraightFlush = true;
  99. else
  100. isStraight = true;
  101. }
  102. else if(isSuitAllSame)
  103. {
  104. isFlush = true;
  105. }
  106. else
  107. {
  108. int highestCardCount = 0;
  109. int currentRank = playerCards[0];
  110.  
  111. for(int i = 0; i < playerCards.length; i++)
  112. {
  113. if(playerCards[i] == currentRank)
  114. {
  115. highestCardCount++;
  116.  
  117. if(highestCardCount == 2)
  118. {
  119. pairs++;
  120. }
  121. else if(highestCardCount == 3)
  122. {
  123. isThree = true;
  124. pairs--;
  125. }
  126. else if(highestCardCount == 4)
  127. {
  128. isThree = false;
  129. pairs = 0;
  130. isFour = true;
  131. break;
  132. }
  133. }
  134. else
  135. {
  136. currentRank = playerCards[i];
  137. highestCardCount = 1;
  138. }
  139. }
  140. }
  141. }
  142.  
  143.  
  144. //----------------------------------------------------
  145.  
  146. if(!isSuitAllSame && !isStraight && pairs == 0 && !isThree && !isFour)
  147. {
  148. for(int i = 0; i < playerCards.length; i++)
  149. {
  150. if(playerCards[i] > highestCard)
  151. {
  152. highestCard = playerCards[i];
  153. }
  154. }
  155. }
  156.  
  157.  
  158. for(int i = 0; i < playerCards.length; i++)
  159. {
  160. String printedRank = cards[playerCards[i] - 2];
  161. String printedSuit = suits[playerSuits[i] - 1];
  162.  
  163.  
  164. System.out.println(printedRank + " of " + printedSuit);
  165. }
  166.  
  167.  
  168.  
  169. System.out.println();
  170.  
  171. if(isRoyalFlush)
  172. {
  173. System.out.println("It's a royal flush!");
  174. }
  175. else if(isStraightFlush)
  176. {
  177. System.out.println("It's a straight flush!");
  178. }
  179. else if(isFour)
  180. {
  181. System.out.println("It's a four of a kind!");
  182. }
  183. else if(isThree && pairs == 1)
  184. {
  185. System.out.println("It's a full house!");
  186. }
  187. else if(isStraight)
  188. {
  189. System.out.println("It's a straight!");
  190. }
  191. else if(isThree)
  192. {
  193. System.out.println("It's a three of a kind!");
  194. }
  195. else if(pairs == 2)
  196. {
  197. System.out.println("It's two pairs!");
  198. }
  199. else if(pairs == 1)
  200. {
  201. System.out.println("It's a pair!");
  202. }
  203. else
  204. {
  205. String printedHighCard = cards[highestCard - 2];
  206. System.out.println("High card is a(n) " + printedHighCard);
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement