Advertisement
Guest User

Black Jack

a guest
Nov 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. public class Blackjack {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. int money; // Amount of money the user has.
  6. int bet; // Amount user bets on a game.
  7. boolean userWins; // Did the user win the game?
  8.  
  9. TextIO.putln("Welcome to the game of blackjack.");
  10. TextIO.putln();
  11.  
  12. money = 100; // User starts with $100.
  13.  
  14. while (true) {
  15. TextIO.putln("You have " + money + " dollars.");
  16. do {
  17. TextIO.putln("How many dollars do you want to bet? (Enter 0 to end.)");
  18. TextIO.put("? ");
  19. bet = TextIO.getlnInt();
  20. if (bet < 0 || bet > money)
  21. TextIO.putln("Your answer must be between 0 and " + money + '.');
  22. } while (bet < 0 || bet > money);
  23. if (bet == 0)
  24. break;
  25. userWins = playBlackjack();
  26. if (userWins)
  27. money = money + bet;
  28. else
  29. money = money - bet;
  30. TextIO.putln();
  31. if (money == 0) {
  32. TextIO.putln("Looks like you've are out of money!");
  33. break;
  34. }
  35. }
  36.  
  37. TextIO.putln();
  38. TextIO.putln("You leave with $" + money + '.');
  39.  
  40. } // end main()
  41.  
  42.  
  43. static boolean playBlackjack() {
  44. // Let the user play one game of Blackjack.
  45. // Return true if the user wins, false if the user loses.
  46.  
  47. Deck deck; // A deck of cards. A new deck for each game.
  48. BlackjackHand dealerHand; // The dealer's hand.
  49. BlackjackHand userHand; // The user's hand.
  50.  
  51. deck = new Deck();
  52. dealerHand = new BlackjackHand();
  53. userHand = new BlackjackHand();
  54.  
  55. /* Shuffle the deck, then deal two cards to each player. */
  56.  
  57. deck.shuffle();
  58. dealerHand.addCard( deck.dealCard() );
  59. dealerHand.addCard( deck.dealCard() );
  60. userHand.addCard( deck.dealCard() );
  61. userHand.addCard( deck.dealCard() );
  62.  
  63. TextIO.putln();
  64. TextIO.putln();
  65.  
  66. /* Check if one of the players has Blackjack (two cards totaling to 21).
  67. The player with Blackjack wins the game. Dealer wins ties.
  68. */
  69.  
  70. if (dealerHand.getBlackjackValue() == 21) {
  71. TextIO.putln("Dealer has the " + dealerHand.getCard(0)
  72. + " and the " + dealerHand.getCard(1) + ".");
  73. TextIO.putln("User has the " + userHand.getCard(0)
  74. + " and the " + userHand.getCard(1) + ".");
  75. TextIO.putln();
  76. TextIO.putln("Dealer has Blackjack. Dealer wins.");
  77. return false;
  78. }
  79.  
  80. if (userHand.getBlackjackValue() == 21) {
  81. TextIO.putln("Dealer has the " + dealerHand.getCard(0)
  82. + " and the " + dealerHand.getCard(1) + ".");
  83. TextIO.putln("User has the " + userHand.getCard(0)
  84. + " and the " + userHand.getCard(1) + ".");
  85. TextIO.putln();
  86. TextIO.putln("You have Blackjack. You win.");
  87. return true;
  88. }
  89.  
  90. /* If neither player has Blackjack, play the game. First the user
  91. gets a chance to draw cards (i.e., to "Hit"). The while loop ends
  92. when the user chooses to "Stand". If the user goes over 21,
  93. the user loses immediately.
  94. */
  95.  
  96. while (true) {
  97.  
  98. /* Display user's cards, and let user decide to Hit or Stand. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement