Advertisement
VKNikov

BlackJack

May 21st, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.15 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class BlackJack {
  6.  
  7.     public static String cards(int rowIndex) {
  8.  
  9.         String[] rows = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
  10.                 "Q", "K", "A"};
  11.  
  12.         String[] suit = {"♣", "♦", "♥", "♠"};
  13.  
  14.         Random randomS = new Random();
  15.         int suitIndex = randomS.nextInt(4);
  16.         String card = new String(rows[rowIndex] + suit[suitIndex]);
  17.  
  18.         return card;
  19.     }
  20.  
  21.     public static int cardValues(int cardIndex) {
  22.         int cardValue;
  23.  
  24.         switch (cardIndex) {
  25.             case 0:
  26.                 cardValue = 2;
  27.                 break;
  28.             case 1:
  29.                 cardValue = 3;
  30.                 break;
  31.             case 2:
  32.                 cardValue = 4;
  33.                 break;
  34.             case 3:
  35.                 cardValue = 5;
  36.                 break;
  37.             case 4:
  38.                 cardValue = 6;
  39.                 break;
  40.             case 5:
  41.                 cardValue = 7;
  42.                 break;
  43.             case 6:
  44.                 cardValue = 8;
  45.                 break;
  46.             case 7:
  47.                 cardValue = 9;
  48.                 break;
  49.             case 8:
  50.             case 9:
  51.             case 10:
  52.             case 11:
  53.                 cardValue = 10;
  54.                 break;
  55.             default:
  56.                 cardValue = 11;
  57.                 break;
  58.         }
  59.         return cardValue;
  60.     }
  61.  
  62.     public static Integer sum(ArrayList<Integer> list) {
  63.         Integer sum = 0;
  64.         for (Integer i : list) {
  65.             sum = sum + i;
  66.         }
  67.  
  68.         if (sum > 21) {
  69.             for (Integer i : list) {
  70.                 if (i == 11) {
  71.                     sum -= 10;
  72.                 }
  73.             }
  74.         }
  75.         return sum;
  76.     }
  77.  
  78.     public static void main(String[] args) {
  79.         System.getProperty("file.encoding","Unicode");
  80.        
  81.         System.out.println("This is BlackJack card game.");
  82.  
  83.         Scanner input = new Scanner(System.in);
  84.  
  85.         boolean noWinner = true;
  86.         boolean playerPass = false;
  87.         boolean surrender = false;
  88.  
  89.         int cash = 100;
  90.         int bet = 0;
  91.         int wincash = 0;
  92.         char[] str = new char[0];
  93.  
  94.         while (cash > 0) {
  95.             int hand = 1;
  96.             noWinner = true;
  97.             playerPass = false;
  98.             surrender = false;
  99.             ArrayList<String> dealerHand = new ArrayList<String>();
  100.             ArrayList<Integer> dealerValue = new ArrayList<Integer>();
  101.             ArrayList<String> playerHand = new ArrayList<String>();
  102.             ArrayList<Integer> playerValue = new ArrayList<Integer>();
  103.  
  104.             for (int i = 0; i < 2; i++) {
  105.  
  106.                 Random randomR = new Random();
  107.                 int dealerR = randomR.nextInt(13);
  108.                 dealerHand.add(cards(dealerR));
  109.                 dealerValue.add(cardValues(dealerR));
  110.             }
  111.  
  112.             for (int i = 0; i < 2; i++) {
  113.  
  114.                 Random randomR = new Random();
  115.                 int playerR = randomR.nextInt(13);
  116.                 playerHand.add(cards(playerR));
  117.                 playerValue.add(cardValues(playerR));
  118.             }
  119.  
  120.             //System.out.println("\033[2J");
  121.  
  122.             System.out.println("Your current cash is: " + cash);
  123.             System.out.printf("\nEnter your bet:");
  124.             bet = Integer.parseInt(input.nextLine());
  125.  
  126.             while (bet > cash) {
  127.                 System.out.println("Your current cash is: " + cash);
  128.                 System.out.printf("You do not have enough money to bet %d. Please enter again your bet:", bet);
  129.                 bet = Integer.parseInt(input.nextLine());
  130.             }
  131.             cash -= bet;
  132.  
  133.             while (noWinner) {
  134.                 wincash = 0;
  135.                 hand++;
  136.                 if (surrender == true) {
  137.                     cash += bet / 2;
  138.                     break;
  139.                 }
  140.                 // Dealer hand
  141.                 if (hand > 2) {
  142.                     if (sum(dealerValue) <= 16 && playerPass == true) {
  143.  
  144.                         System.out.println("\nDealer got a new card.");
  145.                         Random randomR = new Random();
  146.                         int dealerR = randomR.nextInt(13);
  147.                         dealerHand.add(cards(dealerR));
  148.                         dealerValue.add(cardValues(dealerR));
  149.  
  150.                     } else if (sum(dealerValue) > 21) {
  151.                         wincash = bet * 2;
  152.                         bet = 0;
  153.                         cash += wincash;
  154.                         System.out.println("\nDealer Busted!");
  155.                         System.out.println("You win " + wincash + " cash");
  156.                     }
  157.                 }
  158.  
  159.                 if (hand > 2 && playerPass == true) {
  160.                     dealerHandPrint(dealerHand, dealerValue, hand,
  161.                             playerPass);
  162.                     // Print Player Hand
  163.                     //
  164.                     playerHandPrint(playerHand, playerValue, hand);
  165.  
  166.                     while (sum(dealerValue) <= 16) {
  167.                         hand++;
  168.  
  169.                         System.out.println("\nDealer got a new card");
  170.                         Random randomR = new Random();
  171.                         int dealerR = randomR.nextInt(13);
  172.                         dealerHand.add(cards(dealerR));
  173.                         dealerValue.add(cardValues(dealerR));
  174.  
  175.                         // Print Dealer Hand
  176.                         //
  177.                         dealerHandPrint(dealerHand, dealerValue, hand,
  178.                                 playerPass);
  179.                         // Print Player Hand
  180.                         //
  181.                         playerHandPrint(playerHand, playerValue, hand);
  182.  
  183.                     }
  184.  
  185.                     if (sum(dealerValue) > 21) {
  186.                         System.out.println("\nDealer busted!");
  187.                         wincash = bet * 2;
  188.                         bet = 0;
  189.                         cash += wincash;
  190.                         System.out.println("You win " + wincash + " cash.");
  191.                        
  192.                     } else if (sum(playerValue) > sum(dealerValue)
  193.                             && sum(playerValue) <= 21) {
  194.                         System.out.println();
  195.                         System.out.println("You win!");
  196.                         wincash = bet * 2;
  197.                         bet = 0;
  198.                         cash += wincash;
  199.                         System.out.println("You win " + wincash + " cash!");
  200.                     } else if (sum(playerValue) == sum(dealerValue)) {
  201.  
  202.                         System.out.println("\nPush"); // The game is a draw
  203.                         // (i.e. No Winner)
  204.                         wincash = bet;
  205.                         bet = 0;
  206.                         cash += wincash;
  207.                         System.out.println("You get " + wincash + " cash back.");
  208.  
  209.                     } else {
  210.                         System.out.println("\nYou lost!");
  211.                         wincash = -bet;
  212.                         bet = 0;
  213.                         System.out.println("You loose: " + wincash + " cash.");
  214.                     }
  215.                     break;
  216.                 }
  217.  
  218.                 dealerHandPrint(dealerHand, dealerValue, hand, playerPass);
  219.                 // Print Player Hand
  220.                 //
  221.                 playerHandPrint(playerHand, playerValue, hand);
  222.  
  223.                 if ((sum(dealerValue) == 21) && (sum(playerValue) == 21) && hand > 2) {
  224.                     ;
  225.                     System.out.println("\nPush"); // The game is a draw (i.e. No
  226.                     // Winner)
  227.                     wincash = bet;
  228.                     bet = 0;
  229.                     cash += wincash;
  230.                     System.out.println("You get " + wincash + " cash back.");
  231.                     break;
  232.                 } else if ((sum(dealerValue) == 21) && (hand > 2)) {
  233.                     System.out.println("\nYou lost!"); // CASE 7 -dealer's
  234.                     wincash -= bet;
  235.                     bet = 0;
  236.                     System.out.println("You loose: " + wincash + " cash.");
  237.                     break;
  238.                 } else if ((sum(playerValue) == 21) && hand > 2) {
  239.                     System.out.println("\nYou win!");
  240.                     wincash = (bet*2);
  241.                     bet = 0;
  242.                     cash += wincash;
  243.                     System.out.println("You win " + wincash + " cash.");
  244.                     break;
  245.                 } else if (sum(playerValue) > 21) {
  246.                     System.out.println("\nBusted!");
  247.  
  248.                     wincash -= bet;
  249.                     bet = 0;
  250.                     System.out.println("You loose: " + wincash + " cash.");
  251.                     break;
  252.                 }
  253.  
  254.                 if ((cash - bet) < 0) {
  255.                     System.out.println("Hit(H) or Stand(S) or Surrender(U)");
  256.                     str = input.nextLine().toLowerCase().toCharArray();
  257.                     while (str[0] != 's' && str[0] != 'h' && str[0] != 'u') {
  258.                         System.out.println("You have entered incorrect input. Please try again:");
  259.                         System.out.println("Hit(H) or Stand(S) or Surrender(U)");
  260.                         str = input.nextLine().toLowerCase().toCharArray();
  261.                     }
  262.                 } else {
  263.                     System.out.println("\nHit(H) or Stand(S) or Double(D) or Surrender(U)");
  264.                     str = input.nextLine().toLowerCase().toCharArray();
  265.                     while (str[0] != 's' && str[0] != 'h' && str[0] != 'u' && str[0] != 'd') {
  266.                         System.out.println("You have entered incorrect input. Please try again:");
  267.                         System.out.println("\nHit(H) or Stand(S) or Double(D) or Surrender(U)");
  268.                         str = input.nextLine().toLowerCase().toCharArray();
  269.                     }
  270.                 }
  271.  
  272.                 if (str[0] == 'h') {
  273.  
  274.                     System.out.println("You've got a new card.");
  275.                     Random randomR = new Random();
  276.                     int playerR = randomR.nextInt(13);
  277.                     playerHand.add(cards(playerR));
  278.                     playerValue.add(cardValues(playerR));
  279.  
  280.                 } else if (str[0] == 'u') {
  281.  
  282.                     System.out.println("You surrender\nYou take back half of your bet.");
  283.                     surrender = true;
  284.  
  285.                 } else if (str[0] == 'd') {
  286.                     System.out.println("You double your bet!");
  287.                     System.out.println("You've got a new card.");
  288.                     Random randomR = new Random();
  289.                     int playerR = randomR.nextInt(13);
  290.                     playerHand.add(cards(playerR));
  291.                     playerValue.add(cardValues(playerR));
  292.  
  293.                     cash -= bet;
  294.                     bet = bet * 2;
  295.                     playerPass = true;
  296.  
  297.                 } else if (str[0] == 's') {
  298.                     playerPass = true;
  299.                 }
  300.             }
  301.         }
  302.         System.out.println("You have " + cash + " cash left.");
  303.         System.out.println("You have lost! :( ");
  304.     }
  305.  
  306.  
  307.     private static void playerHandPrint(ArrayList<String> playerHand,
  308.                                         ArrayList<Integer> playerValue, int hand) {
  309.         System.out.print("\nYou have ");
  310.         System.out.println(sum(playerValue) + " points."); // display player
  311.         // points
  312.         for (String item : playerHand) { // Display player cards
  313.             System.out.print(item + "  ");
  314.  
  315.         }
  316.         System.out.println();
  317.     }
  318.  
  319.     private static void dealerHandPrint(ArrayList<String> dealerHand,
  320.                                         ArrayList<Integer> dealerValue, int hand, boolean playerPass) {
  321.         if (hand == 2 && playerPass == false) {
  322.             System.out.print("Dealer has ");
  323.             System.out.print("\n" + dealerHand.get(0) + "  ???"); // Display dealer cards with second card hidden
  324.  
  325.         } else {
  326.             System.out.print("Dealer has ");
  327.             System.out.println(sum(dealerValue) + " points.");// Display dealer cards
  328.  
  329.             for (String item : dealerHand) {
  330.                 System.out.print(item + "  ");
  331.             }
  332.         }
  333.     }
  334.  
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement