Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ExerciseBlackJackDiceGame
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. Scanner console = new Scanner(System.in);
  9. Random r = new Random();
  10.  
  11. int diceThrow = (r.nextInt(12) + 1);
  12. int playerTotal = diceThrow;
  13.  
  14. System.out.println("You start out with a: " + diceThrow);
  15.  
  16. String playerChoice = "y";
  17.  
  18. //1.USER THROWS
  19. while (playerChoice.equals("y"))
  20. {
  21. System.out.println("Do you want want to roll the dice again y/n: ");
  22. playerChoice = console.next();
  23.  
  24. if (playerChoice.equals("y"))
  25. {
  26. diceThrow = (r.nextInt(12) + 1);
  27. playerTotal = playerTotal + diceThrow;
  28. System.out.println("You now have: " + playerTotal);
  29. }
  30. if (playerTotal > 21)
  31. {
  32. System.out.println("You exceeded 21. The machine wins!");
  33. return;
  34. }
  35. if (playerTotal == 21)
  36. {
  37. System.out.println("BLACKJACK!!!! YOU WIN!!!!");
  38. return;
  39. }
  40.  
  41. // 2.MACHINE THROWS
  42. // This is more or less the same procedure as above
  43. // but you do not ask the machine if it wants to go on.
  44. // You have to set up some rules (algorithm) that makes
  45. // the machine go on or stop.
  46. // And remember that if the user got 21,
  47. // then the machine has already lost.
  48. // And if the user exeeded 21, then the machine has already won
  49. if (playerChoice.equals("n"))
  50. {
  51. int machineDiceThrow = 0;
  52. int machineTotal = 0;
  53. while (machineTotal <= 21)
  54. {
  55. diceThrow = (r.nextInt(12) + 1);
  56. machineDiceThrow = diceThrow;
  57. machineTotal = machineTotal + machineDiceThrow;
  58.  
  59. if (machineTotal >= 17)
  60. {
  61. if (machineTotal > 21)
  62. {
  63. System.out.println("Machine has " + machineTotal);
  64. System.out.println("Machine exceeded 21. YOU WIN!");
  65. return;
  66. }
  67. else if (machineTotal > playerTotal)
  68. {
  69. System.out.println("Machine wins!");
  70. System.out.println("Machine has " + machineTotal);
  71. System.out.println("You have " + playerTotal);
  72. return;
  73. }
  74. else if (machineTotal < playerTotal)
  75. {
  76. System.out.println("YOU WIN!");
  77. System.out.println("Machine has " + machineTotal);
  78. System.out.println("You have " + playerTotal);
  79. return;
  80. }
  81. else if (machineTotal == playerTotal)
  82. {
  83. System.out.println("It is a draw!");
  84. System.out.println("Machine has " + machineTotal);
  85. System.out.println("You have " + playerTotal);
  86. return;
  87. }
  88. }
  89. }
  90. }
  91. }
  92.  
  93. }
  94.  
  95. }
  96.  
  97. // 3.CHECK WHO WON
  98. // Here you will have to check:
  99. // 1. Did the user get 21, then he wins
  100. // 2. did the user exeed 21, then machine wins
  101. // 3. did the machine exeed 21, then user wins
  102. // 4. is the users sum larger than the machines, the user win
  103. // 5. is the machine sum larger than users, then machine win
  104. // 6. did user and machine get the same sum, then it is a draw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement