Advertisement
EvstatiZarkov

BlackDjack /First team work

May 18th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.87 KB | None | 0 0
  1. package BlackDjeck;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class BlackDjack {
  9.  
  10.     public static String cards(int rowIndex) {
  11.  
  12.         String[] rows = { "Two", "Three", "Four", "Five", "Six", "Seven",
  13.                 "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
  14.  
  15.         String[] suit = { "Hearts", "Diamonds", "Clubs", "Spades" };
  16.  
  17.         Random randomS = new Random();
  18.         int suitIndex = randomS.nextInt(4);
  19.         String card = new String(rows[rowIndex] + " " + suit[suitIndex]);
  20.  
  21.         return card;
  22.     };
  23.  
  24.     public static int cardValues(int cardIndex) {
  25.         int cardValue;
  26.  
  27.         switch (cardIndex) {
  28.         case 0:
  29.             cardValue = 2;
  30.             break;
  31.         case 1:
  32.             cardValue = 3;
  33.             break;
  34.         case 2:
  35.             cardValue = 4;
  36.             break;
  37.         case 3:
  38.             cardValue = 5;
  39.             break;
  40.         case 4:
  41.             cardValue = 6;
  42.             break;
  43.         case 5:
  44.             cardValue = 7;
  45.             break;
  46.         case 6:
  47.             cardValue = 8;
  48.             break;
  49.         case 7:
  50.             cardValue = 9;
  51.             break;
  52.         case 8:
  53.         case 9:
  54.         case 10:
  55.         case 11:
  56.             cardValue = 10;
  57.             break;
  58.         default:
  59.             cardValue = 11;
  60.             break;
  61.         }
  62.         return cardValue;
  63.     };
  64.  
  65.     public static Integer sum(ArrayList<Integer> list) {
  66.         Integer sum = 0;
  67.         for (Integer i : list) {
  68.             sum = sum + i;
  69.         }
  70.  
  71.         if (sum > 21) {
  72.             for (Integer i : list) {
  73.                 if (i == 11) {
  74.                     sum -= 10;
  75.                 }
  76.  
  77.             }
  78.  
  79.         }
  80.  
  81.         return sum;
  82.  
  83.     }
  84.  
  85.     public static void main(String[] args) {
  86.  
  87.        
  88.  
  89.         Scanner input = new Scanner(System.in);
  90.  
  91.         ArrayList<String> dealerHand = new ArrayList<String>();
  92.         ArrayList<Integer> dealerValue = new ArrayList<Integer>();
  93.         ArrayList<String> playerHand = new ArrayList<String>();
  94.         ArrayList<Integer> playerValue = new ArrayList<Integer>();
  95.  
  96.         for (int i = 0; i < 2; i++) {
  97.  
  98.             Random randomR = new Random();
  99.             int dealerR = randomR.nextInt(13);
  100.             dealerHand.add(cards(dealerR));
  101.             dealerValue.add(cardValues(dealerR));
  102.         }
  103.  
  104.         for (int i = 0; i < 2; i++) {
  105.  
  106.             Random randomR = new Random();
  107.             int playerR = randomR.nextInt(13);
  108.             playerHand.add(cards(playerR));
  109.             playerValue.add(cardValues(playerR));
  110.         }
  111.         boolean noWinner = true;
  112.         boolean playerWin = false; // When turn true Player win double the bet
  113.                                     // money.(for Bet system from Lubo )
  114.         boolean safeMoney = false; // Check first card of dealer is Ace and
  115.                                     // player said that want safe money.(for Bet
  116.                                     // system from Lubo )
  117.         boolean draw = false; // When game finish draw -> boolean draw = true;
  118.                                 // than money give back to player. No one
  119.                                 // win(for Bet system from Lubo )
  120.  
  121.         boolean playerPass = false;
  122.  
  123.         int hand = 1;
  124.  
  125.         System.out.println("HAND  " + hand); // Display hand and cards for test
  126.                                                 // must replace with print to console
  127.                                                 // from Sasho
  128.         System.out.println(" Dealer HAND  " + hand);// Display hand and cards
  129.                                                     // for test must replace
  130.                                                     // with print to console from Sasho
  131.  
  132.         System.out.println(dealerHand.get(0));// Display hand and cards for test
  133.                                                 // must replace with print to console
  134.                                                 // from Sasho
  135.  
  136.         System.out.println("HAND  " + hand);// Display hand and cards for test
  137.                                             // must replace with print to console from
  138.                                             // Sasho
  139.         System.out.println(" Player HAND  " + hand);// Display hand and cards
  140.                                                     // for test must replace
  141.                                                     // with print to console from Sasho
  142.  
  143.         System.out.println(playerHand.get(0));// Display hand and cards for test
  144.                                                 // must replace with print to console
  145.                                                 // from Sasho
  146.         if (dealerValue.get(0) == 11) {
  147.             System.out
  148.                     .println("Do you whant safe hand (take half bet money back)");
  149.             char[] a = input.nextLine().toCharArray();// Here you must set input
  150.                                                         // from Sasho.My input
  151.                                                         // is for test.
  152.             if (a[0] == 'c') {
  153.                 safeMoney = true;
  154.             }
  155.         }
  156.  
  157.         while (noWinner) {
  158.             hand++;
  159.             if (hand > 2) {
  160.                 if (sum(dealerValue) <= 16) {
  161.  
  162.                     System.out.println("Dealer got a new card");
  163.                     Random randomR = new Random();
  164.                     int dealerR = randomR.nextInt(13);
  165.                     dealerHand.add(cards(dealerR));
  166.                     dealerValue.add(cardValues(dealerR));
  167.  
  168.                     // Print Dealer Hand
  169.                     //
  170.                     dealerHandPrint(dealerHand, dealerValue, hand);
  171.                     // Print Player Hand
  172.                     //
  173.                     playerHandPrint(playerHand, playerValue, hand);
  174.                 }
  175.  
  176.                 if (sum(dealerValue) > 21) {
  177.  
  178.                     playerWin = true;
  179.                     System.out.println("Delar Lose");
  180.                     break;
  181.                 }
  182.             }
  183.  
  184.             if (hand > 2 && playerPass == true) {
  185.  
  186.                 while (sum(dealerValue) <= 16) {
  187.                     hand++;
  188.  
  189.                     System.out.println("Dealer got a new card");
  190.                     Random randomR = new Random();
  191.                     int dealerR = randomR.nextInt(13);
  192.                     dealerHand.add(cards(dealerR));
  193.                     dealerValue.add(cardValues(dealerR));
  194.  
  195.                     // Print Dealer Hand
  196.                     //
  197.                     dealerHandPrint(dealerHand, dealerValue, hand);
  198.                     // Print Player Hand
  199.                     //
  200.                     playerHandPrint(playerHand, playerValue, hand);
  201.  
  202.                 }
  203.  
  204.                 if (sum(dealerValue) > 21) {
  205.  
  206.                     System.out.println("Player WIN!!!!!!!!!!!!!!!!!");
  207.  
  208.                     playerWin = true;
  209.  
  210.                     break;
  211.                 }
  212.  
  213.                 if (sum(playerValue) > sum(dealerValue)) {
  214.                     System.out.println("Player WIN!!!!!!!!!!!!!!!!!");
  215.  
  216.                     playerWin = true;
  217.                     break;
  218.  
  219.                 } else if (sum(playerValue) == sum(dealerValue)) {
  220.  
  221.                     System.out.println("No Winers!!!!!!!!!!!!!!!!!!");
  222.                     draw = true;
  223.  
  224.                     break;
  225.  
  226.                 }
  227.  
  228.                 else {
  229.                     System.out.println("Player LOSE!!!!!!!!!!!!!!!!");
  230.  
  231.                     break;
  232.                 }
  233.  
  234.             }
  235.  
  236.             // Print Dealer Hand
  237.             //
  238.             System.out.println("HAND  " + hand);// Display hand and cards for
  239.                                                 // test must replace with
  240.                                                 // print to console from Sasho
  241.             dealerHandPrint(dealerHand, dealerValue, hand);
  242.             // Print Player Hand
  243.             //
  244.             playerHandPrint(playerHand, playerValue, hand);
  245.  
  246.             if (sum(dealerValue) == 21) {
  247.  
  248.                 System.out.println("Delar WIN!!!!!!!!!!!!!!!!!!!!");
  249.  
  250.                 break;
  251.             }
  252.  
  253.             if (sum(playerValue) == 21) {
  254.                 System.out.println("Player WIN!!!!!!!!!!!!!!!!!!1");
  255.  
  256.                 playerWin = true;
  257.                 break;
  258.  
  259.             }
  260.  
  261.             if (sum(playerValue) > 21) {
  262.                 System.out.println("!!!!  Player LOSE!!!!!!");
  263.  
  264.                 break;
  265.  
  266.             }
  267.  
  268.             System.out.println("Do you whant card");
  269.             char[] str = input.nextLine().toCharArray();
  270.             if (str[0] == 'c') {
  271.                 System.out.println("you got a card");
  272.                 Random randomR = new Random();
  273.                 int playerR = randomR.nextInt(13);
  274.                 playerHand.add(cards(playerR));
  275.                 playerValue.add(cardValues(playerR));
  276.  
  277.             } else {
  278.                 playerPass = true;
  279.             }
  280.  
  281.         }
  282.         ;
  283.  
  284.     }
  285.  
  286.     private static void playerHandPrint(ArrayList<String> playerHand,
  287.             ArrayList<Integer> playerValue, int hand) {
  288.         System.out.println(" Player HAND  " + hand);// Display hand and cards
  289.                                                     // for test must replace
  290.                                                     // with print to console from Sasho
  291.         System.out.println(sum(playerValue));// Display hand and cards for test
  292.                                                 // must replace with print to console
  293.                                                 // from Sasho
  294.         for (String item : playerHand) {// Display hand and cards for test must
  295.                                         // replace with print to console from Sasho
  296.             System.out.println(item);// Display hand and cards for test must
  297.                                         // replace with print to console from Sasho
  298.         }
  299.     }
  300.  
  301.     private static void dealerHandPrint(ArrayList<String> dealerHand,
  302.             ArrayList<Integer> dealerValue, int hand) {
  303.         System.out.println(" Dealer HAND  " + hand);// Display hand and cards
  304.                                                     // for test must replace
  305.                                                     // with print to console from Sasho
  306.         System.out.println(sum(dealerValue));// Display hand and cards for test
  307.                                                 // must replace with print to console
  308.                                                 // from Sasho
  309.         for (String item : dealerHand) {// Display hand and cards for test must
  310.                                         // replace with print to console from Sasho
  311.             System.out.println(item);// Display hand and cards for test must
  312.                                         // replace with print to console from Sasho
  313.         }
  314.     }
  315.  
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement