Advertisement
alexnutu

Untitled

Jul 28th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //public static void main(String[] args)
  4. //{
  5. // double number = Math.ceil(Math.random()*10);
  6. // System.out.println(number);
  7. //}
  8.  
  9. public class MyFirstClass
  10. {
  11. public static double playerDraw()
  12. {
  13. double player = Math.ceil(Math.random()*(10-2)+2); // card range Math.random()*(max-min)+min
  14. System.out.printf(">>Player draws card# %.0f\n", player);
  15. return player;
  16. }
  17.  
  18. public static double dealerDraw()
  19. {
  20. double dealer = Math.ceil(Math.random()*(10-2)+2);
  21. System.out.printf(">>Dealer draws card# %.0f\n", dealer);
  22. return dealer;
  23. }
  24.  
  25. public static void main(String[] args)
  26. {
  27. double p = 0;
  28. double d = 0;
  29.  
  30. for(int i=0; i<=21;i++) // the game starts
  31. {
  32. // p and d are used to keep track of the total for each player
  33. p = p + playerDraw();
  34. d = d + dealerDraw();
  35.  
  36. System.out.printf("Player Card Total=%.0f\n", p);
  37. System.out.printf("Dealer Card Total=%.0f\n", d);
  38.  
  39. if(p==21 || d==21) // check if we have a winner (blackjack)
  40. {
  41. if(p==21)
  42. {
  43. System.out.println("Blackjack! Player wins!");
  44. System.exit(0);
  45. }
  46. if(d==21)
  47. {
  48. System.out.println("Blackjack! Dealer wins! Player loses!");
  49. System.exit(0);
  50. }
  51. }
  52.  
  53. if(p>21 || d>21) // check if either is over 21
  54. {
  55. if(p>21)
  56. {
  57. System.out.printf("Player total: %.0f, Dealer total: %.0f; Player loses!", p, d);
  58. System.exit(0);
  59. }
  60. if(d>21)
  61. {
  62. System.out.printf("Player total: %.0f, Dealer total: %.0f; Player wins!, dealer over 21", p, d);
  63. System.exit(0);
  64. }
  65. }
  66. if((p>=18 || p==20)&&(d>=18 || d==20))//closest to 21
  67. {
  68. if(p>d)
  69. {
  70. System.out.printf("Player total: %.0f, Dealer total: %.0f; Player wins!", p, d);
  71. System.exit(0);
  72. }
  73. if(d>p)
  74. {
  75. System.out.printf("Player total: %.0f, Dealer total: %.0f; Dealer wins!", p, d);
  76. System.exit(0);
  77. }
  78. }
  79.  
  80. }
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement