Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. import java.util.LinkedList;
  4.  
  5. public class PokerV1 {
  6. static Scanner scanner = new Scanner(System.in);
  7.  
  8. public static void main(String[] args){
  9. String cont = "y";
  10. int total = 100;
  11. while(cont.equals("y")){
  12.  
  13. int[] cards = cards(5);
  14. int[] suits = suits(5);
  15.  
  16. int[] compCards = cards(5);
  17. int[] compSuits = suits(5);
  18.  
  19. int[] tempCards = new int[5];
  20. int[] tempCompCards = new int[5];
  21.  
  22. for(int i = 0; i < cards.length; i++){
  23. tempCards[i] = cards[i];
  24. tempCompCards[i] = compCards[i];
  25. }
  26.  
  27. int playerScore = score(tempCards, suits);
  28. int compScore = score(tempCompCards, compSuits);
  29.  
  30. int bet = pollInteger("Please enter a bet, $25 Max");
  31. while(bet > 25 || bet < 1){
  32. bet = (bet > 25) ?
  33. pollInteger("You are over the max please bet a number below $25") :
  34. pollInteger("You are under the min please bet a number below $25");
  35. }
  36. System.out.println("Your hand:");
  37. printHand(cards, suits, playerScore);
  38.  
  39. LinkedList<Integer> selection = new LinkedList<Integer>();
  40. System.out.println("Chose the number you wish to exchange(1-5) \nEnter <done> when finished");
  41.  
  42. while(true){
  43. String add = scanner.nextLine().toLowerCase();
  44. if(add.equals("done"))
  45. break;
  46. int addition = 0;
  47. try{
  48. addition = Integer.parseInt(add);
  49. }catch(NumberFormatException e){
  50. System.out.println("Error, try again: ");
  51. continue;
  52. }
  53.  
  54. if(selection.contains(addition)){
  55. selection.remove(selection.indexOf(addition));
  56. System.out.println(selection);
  57. continue;
  58. }
  59.  
  60. while(addition > 5 || addition < 1){
  61. addition = pollInteger("You already chose that or chose a number greater then 5 or less then 1! \nPlease Select another number!");
  62. }
  63. selection.add(addition);
  64. System.out.println(selection);
  65. }
  66. System.out.println("You exchanged: " + selection);
  67.  
  68. for(int choice : selection){
  69. cards[choice - 1] = (int)(Math.random() * 12) + 2;
  70. suits[choice - 1] = (int)(Math.random() * 4) + 1;
  71. }
  72. for(int i = 0; i < 5; i++){
  73. tempCards[i] = cards[i];
  74. }
  75. playerScore = score(tempCards, suits);
  76. compScore = score(tempCompCards, compSuits);
  77.  
  78. System.out.println("\nYour new hand:");
  79. printHand(cards, suits, playerScore);
  80.  
  81. System.out.println("\nComputer's hand:");
  82. printHand(compCards, compSuits, compScore);
  83. System.out.println();
  84.  
  85. if(playerScore == compScore){
  86. Arrays.sort(cards);
  87. Arrays.sort(compCards);
  88. if(cards[4] > compCards[4]){
  89. System.out.println("Your highest number <" + cards[4] + "> \nis bigger then the computer! <" + compCards[4]+ "> You win!");
  90. total += bet;
  91. System.out.println("Your money: $" + total);
  92. }else{
  93. System.out.println("Your highest number <" + cards[4] + "> \nis smaller then the computer!<" + compCards[4]+ "> You lose!");
  94. total -= bet;
  95. System.out.println("Your money: $" + total);
  96. }
  97. }
  98. else if(playerScore > compScore){
  99. System.out.println("You win!");
  100. total += bet;
  101. System.out.println("Your money: $" + total);
  102. }else{
  103. System.out.println("You lose!");
  104. total -= bet;
  105. System.out.println("Your money: $" + total);
  106. }
  107.  
  108. System.out.println("Would you like to continue? (Y/N)");
  109. cont = scanner.nextLine().toLowerCase();
  110. }
  111. System.out.println("You ran with: $" + total + "\nPlease play again!");
  112. }
  113.  
  114. private static int score(int[] cards, int[] suits){
  115. int score = 0;
  116. int pairs = 0;
  117. Arrays.sort(cards);
  118.  
  119. boolean straight = false;
  120. boolean flush = false;
  121.  
  122. for(int i = 0; i < cards.length; i++){
  123. for(int j = 0; j < cards.length; j++){
  124. if(i == j)
  125. continue;
  126. if(cards[i] == cards[j])
  127. pairs++;
  128. }
  129. }
  130. for(int i = 0; i < 4; i++){
  131. if(cards[i + 1] == cards[i] + 1){
  132. score = 7;
  133. straight = true;
  134. }else{
  135. score = 0;
  136. straight = false;
  137. break;
  138. }
  139. }
  140. for(int i = 0; i < 4; i++){
  141. if(suits[i] == suits[i+1]){
  142. score = 8;
  143. flush = true;
  144. }else{
  145. score = 0;
  146. flush = false;
  147. break;
  148. }
  149. }
  150. if(straight && flush == false) score = 7;
  151. if(straight && flush)
  152. score = 14;
  153. for(int i = 0; i < 5; i++){
  154. if(straight && flush && cards[i] > 9)
  155. score = 16;
  156. else{
  157. break;
  158. }
  159. }
  160. score = (score > pairs) ? score : pairs;
  161. if(pairs == 8) score = 9;
  162. return score;
  163. }
  164.  
  165. private static void printHand(int[] cards, int[] suits, int score){
  166. String[] suitValue = new String[5];
  167. String[] handValue = new String[5];
  168.  
  169. for(int i = 0; i < handValue.length; i++){
  170.  
  171. handValue[i] = "| " + cards[i] + "|";
  172.  
  173. if(cards[i] == 10)
  174. handValue[i] = "|" + cards[i] + "|";
  175. if(cards[i] == 11)
  176. handValue[i] = "| J|";
  177. if(cards[i] == 12)
  178. handValue[i] = "| Q|";
  179. if(cards[i] == 13)
  180. handValue[i] = "| K|";
  181. if(cards[i] == 14)
  182. handValue[i] = "| A|";
  183.  
  184. if(suits[i] == 1)
  185. suitValue[i] = "| S|";
  186. if(suits[i] == 2)
  187. suitValue[i] = "| C|";
  188. if(suits[i] == 3)
  189. suitValue[i] = "| D|";
  190. if(suits[i] == 4)
  191. suitValue[i] = "| H|";
  192. }
  193.  
  194. for(int i = 0; i < 5; i++){System.out.print(handValue[i]);}
  195. System.out.println();
  196. for(int i = 0; i < 5; i++){System.out.print(suitValue[i]);}
  197. System.out.println();
  198. if(score == 2)
  199. System.out.println("You have a pair!");
  200. if(score == 4)
  201. System.out.println("You have Two Pairs!");
  202. if(score == 6)
  203. System.out.println("You have a 3 of a kind!");
  204. if(score == 7)
  205. System.out.println("You have a Straight!");
  206. if(score == 8)
  207. System.out.println("You have a Flush!");
  208. if(score == 9)
  209. System.out.println("You have a Full House!");
  210. if(score == 12)
  211. System.out.println("You have a 4 of a Kind!");
  212. if(score == 14)
  213. System.out.println("You have a Straight Flush!");
  214. if(score == 16)
  215. System.out.println("You have a Royal Straight Flush!");
  216. }
  217.  
  218. private static int[] cards(int cardNumber){
  219. int[] cards = new int[cardNumber];
  220.  
  221. for(int i = 0; i < cards.length; i++){
  222. cards[i] = (int)(Math.random() * 12) + 2;
  223. }
  224. return cards;
  225. }
  226.  
  227. private static int[] suits(int cardNumber){
  228. int[] suit = new int[cardNumber];
  229.  
  230. for(int i = 0; i < suit.length; i++){
  231. suit[i] = (int)(Math.random() * 4) + 1;
  232. }
  233. return suit;
  234. }
  235.  
  236. private static int pollInteger(String message){
  237. System.out.println(message);
  238. int valReturn = 0;
  239. boolean Valid = true;
  240.  
  241. while(Valid){
  242. try{
  243. valReturn = Integer.parseInt(scanner.nextLine());
  244. Valid = false;
  245. }catch(NumberFormatException e){
  246. System.out.println("Error: Please enter a number");
  247. Valid = true;
  248. }
  249. }
  250. return valReturn;
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement