Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class BlackJack {
  6. private final Deck deck = new Deck();
  7. private final ArrayList<Card> player = new ArrayList<>();
  8. private final ArrayList<Card> dealer = new ArrayList<>();
  9.  
  10. public BlackJack() {
  11. deck.shuffle();
  12. }
  13.  
  14. public void play() {
  15. player.add(deck.dealACard());
  16. player.add(deck.dealACard());
  17. dealer.add(deck.dealACard());
  18. dealer.add(deck.dealACard());
  19.  
  20. System.out.printf("Player Cards: %s%n", player);
  21. System.out.printf("Player Value: %s%n", value(player));
  22. System.out.printf("Dealer First Card: %s%n%n", dealer.get(0));
  23.  
  24. Scanner in = new Scanner(System.in);
  25. while(value(player) <= 21 && player.size() < 5) {
  26. System.out.printf("Player Cards: %s%n", player);
  27. System.out.printf("Player Value: %s%n", value(player));
  28.  
  29. System.out.println("Hit or Stay?");
  30. String line = in.next();
  31.  
  32. if(line.equals("Hit")) {
  33. player.add(deck.dealACard());
  34. System.out.println();
  35.  
  36.  
  37. } else if (line.equals("Stay")) {
  38. System.out.println();
  39.  
  40. break;
  41. }
  42. }
  43.  
  44. System.out.printf("Player Cards: %s%n", player);
  45. System.out.printf("Player Value: %s%n", value(player));
  46.  
  47.  
  48. if(value(player) > 21) {
  49. System.out.println("The player busted and lost!");
  50. } else if (player.size() >= 5) {
  51. System.out.println("The player won with a Five Card Charlie!");
  52. } else {
  53. while(value(dealer) < 17) {
  54. dealer.add(deck.dealACard());
  55. }
  56.  
  57. System.out.printf("Dealer Cards: %s%n", dealer);
  58. System.out.printf("Dealer Value: %s%n", value(dealer));
  59.  
  60. if(value(dealer) > 21) {
  61. System.out.println("The player won as the dealer busted!");
  62. } else if(value(dealer) > value(player)) {
  63. System.out.println("The dealer won with more value!");
  64. } else if (value(dealer) == value(player)) {
  65. System.out.println("The dealer and player tied!");
  66. } else {
  67. System.out.println("The player won with more value!");
  68. }
  69. }
  70. }
  71.  
  72. private static int value(List<Card> cards) {
  73. int aces = 0;
  74. int value = 0;
  75.  
  76. for(int i = 0; i < cards.size(); i++) {
  77. Card c = cards.get(i);
  78.  
  79. if(c.getRank() == 1) {
  80. aces++;
  81. value += 1;
  82. } else if (c.getRank() >= 2 && c.getRank() <= 10) {
  83. value += c.getRank();
  84. } else {
  85. value += 10;
  86. }
  87. }
  88.  
  89. for(int i = 0; i < aces && value <= 11; i++) {
  90. value += 10;
  91. }
  92.  
  93. return value;
  94. }
  95.  
  96. public static void main(String[] args) {
  97. new BlackJack().play();
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement