Advertisement
Arham-4

Old Blackjack

Oct 3rd, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.96 KB | None | 0 0
  1. //Name: Darya Zykin
  2. //Period: 4A
  3.  
  4. import java.util.*;
  5.  
  6. public class FullBlackjack
  7. {
  8.    
  9.     /*
  10.     If you EVER copy and paste your code, you must realize that there is a simpler way to do it. Whether you
  11.     delegate it to a new method or use a loop, there almost is always a way to simplify it. This is an example
  12.     of it not being delegated.
  13.    
  14.     I haven't run your code, but I can tell just looking at the code that 2 player does not work. I haven't
  15.     looked at the other players, but I think the same problem exists in other sections. The fact that so many
  16.     variables exist with similar names makes one go panic mode and miss renaming those variables. That should
  17.     also be another clue that "this can be simplified."
  18.    
  19.     When code is blown to such a proportion, it becomes *unmaintainable.* Last year, our UIL state team got an
  20.     entire question wrong because somebody copy and pasted code, changed it one place and forgot to change it
  21.     in the other place. That was the entire reason.
  22.    
  23.     When doing this copy-and-pasting, you should look at your game and think, "what is the pattern here?"
  24.     In your case, you do the same exact actions *for each* player. The fact I said *for each* should make
  25.     a lightbulb in your head thinking in terms of loops.
  26.    
  27.     Go look at your code. Read each of the `players == NUMBER` if statements and tell me if you can see a
  28.     pattern. After doing that for a little, even if you don't see it, continue reading.
  29.    
  30.     If you haven't noticed, you are doing the summation, input, and computation for each player every single
  31.     if statement, for each player. Refer to my other file now.
  32.     */
  33.    
  34.     public static void main(String[] args)
  35.     {
  36.         Scanner console = new Scanner(System.in);
  37.         //Full Blackjack
  38.         Random randomGen = new Random();
  39.         System.out.print("Please enter amount of players (up to 6): ");
  40.         int players = console.nextInt();
  41.         int dealerSum = 0;
  42.         if (players == 1){
  43.             int sum1 = 0;
  44.             int cardValue = randomGen.nextInt(11);
  45.             sum1 += cardValue;
  46.             cardValue = randomGen.nextInt(11);
  47.             sum1 += cardValue;
  48.             System.out.println(sum1);
  49.             while (sum1 < 21){
  50.                 cardValue = randomGen.nextInt(11);
  51.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  52.                 int choice1 = console.nextInt();
  53.                 if (choice1 == 1){
  54.                     sum1 += cardValue;
  55.                     System.out.println(sum1);
  56.                 }
  57.                 else if (choice1 == 2){
  58.                     break;
  59.                 }
  60.             }
  61.             if (sum1 == 21){
  62.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  63.             }
  64.             else if (sum1 > 21){
  65.                 System.out.println("You bust. Restart the program to play again.");
  66.             }
  67.             dealerSum = 0;
  68.             cardValue = randomGen.nextInt(11);
  69.             dealerSum += cardValue;
  70.             cardValue = randomGen.nextInt(11);
  71.             dealerSum += cardValue;
  72.             if (dealerSum <= 15){
  73.                 dealerSum += cardValue;
  74.             }
  75.             else if (dealerSum >= 16){
  76.                 if (sum1 == 21){
  77.                     System.out.println("Dealer wins! Restart the program to play again");
  78.                 }
  79.                 else if (sum1 > 21){
  80.                     System.out.println("Dealer busts. Restart the program to play again.");
  81.                 }
  82.             }
  83.         }
  84.         else if (players == 2){
  85.             int sum1 = 0;
  86.             int cardValue = randomGen.nextInt(11);
  87.             sum1 += cardValue;
  88.             cardValue = randomGen.nextInt(11);
  89.             sum1 += cardValue;
  90.             System.out.println(sum1);
  91.             while (sum1 < 21){
  92.                 cardValue = randomGen.nextInt(11);
  93.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  94.                 int choice1 = console.nextInt();
  95.                 if (choice1 == 1){
  96.                     sum1 += cardValue;
  97.                     System.out.println(sum1);
  98.                 }
  99.             }
  100.             if (sum1 == 21){
  101.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  102.             }
  103.             else if (sum1 > 21){
  104.                 System.out.println("You bust. Restart the program to play again.");
  105.             }
  106.             int sum2 = 0;
  107.             cardValue = randomGen.nextInt(11);
  108.             sum1 += cardValue;
  109.             cardValue = randomGen.nextInt(11);
  110.             sum1 += cardValue;
  111.             System.out.println(sum2);
  112.             while (sum1 < 21){
  113.                 cardValue = randomGen.nextInt(11);
  114.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  115.                 int choice1 = console.nextInt();
  116.                 if (choice1 == 1){
  117.                     sum2 += cardValue;
  118.                     System.out.println(sum1);
  119.                 }
  120.             }
  121.             if (sum1 == 21){
  122.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  123.             }
  124.             else if (sum1 > 21){
  125.                 System.out.println("You bust. Restart the program to play again.");
  126.             }
  127.         }
  128.         else if (players == 3){
  129.             int sum1 = 0;
  130.             int cardValue = randomGen.nextInt(11);
  131.             sum1 += cardValue;
  132.             cardValue = randomGen.nextInt(11);
  133.             sum1 += cardValue;
  134.             System.out.println(sum1);
  135.             while (sum1 < 21){
  136.                 cardValue = randomGen.nextInt(11);
  137.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  138.                 int choice1 = console.nextInt();
  139.                 if (choice1 == 1){
  140.                     sum1 += cardValue;
  141.                     System.out.println(sum1);
  142.                 }
  143.             }
  144.             if (sum1 == 21){
  145.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  146.             }
  147.             else if (sum1 > 21){
  148.                 System.out.println("You bust. Restart the program to play again.");
  149.             }
  150.             int sum2 = 0;
  151.             cardValue = randomGen.nextInt(11);
  152.             sum1 += cardValue;
  153.             cardValue = randomGen.nextInt(11);
  154.             sum1 += cardValue;
  155.             System.out.println(sum1);
  156.             while (sum1 < 21){
  157.                 cardValue = randomGen.nextInt(11);
  158.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  159.                 int choice1 = console.nextInt();
  160.                 if (choice1 == 1){
  161.                     sum1 += cardValue;
  162.                     System.out.println(sum1);
  163.                 }
  164.             }
  165.             if (sum1 == 21){
  166.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  167.             }
  168.             else if (sum1 > 21){
  169.                 System.out.println("You bust. Restart the program to play again.");
  170.             }
  171.             int sum3 = 0;
  172.             cardValue = randomGen.nextInt(11);
  173.             sum1 += cardValue;
  174.             cardValue = randomGen.nextInt(11);
  175.             sum1 += cardValue;
  176.             System.out.println(sum1);
  177.             while (sum1 < 21){
  178.                 cardValue = randomGen.nextInt(11);
  179.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  180.                 int choice1 = console.nextInt();
  181.                 if (choice1 == 1){
  182.                     sum1 += cardValue;
  183.                     System.out.println(sum1);
  184.                 }
  185.             }
  186.             if (sum1 == 21){
  187.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  188.             }
  189.             else if (sum1 > 21){
  190.                 System.out.println("You bust. Restart the program to play again.");
  191.             }
  192.         }
  193.         else if (players == 4){
  194.             int sum1 = 0;
  195.             int cardValue = randomGen.nextInt(11);
  196.             sum1 += cardValue;
  197.                 cardValue = randomGen.nextInt(11);
  198.             sum1 += cardValue;
  199.             System.out.println(sum1);
  200.             while (sum1 < 21){
  201.                 cardValue = randomGen.nextInt(11);
  202.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  203.                 int choice1 = console.nextInt();
  204.                 if (choice1 == 1){
  205.                     sum1 += cardValue;
  206.                     System.out.println(sum1);
  207.                 }
  208.             }
  209.             if (sum1 == 21){
  210.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  211.             }
  212.             else if (sum1 > 21){
  213.                 System.out.println("You bust. Restart the program to play again.");
  214.             }
  215.             int sum2 = 0;
  216.             cardValue = randomGen.nextInt(11);
  217.             sum1 += cardValue;
  218.             cardValue = randomGen.nextInt(11);
  219.             sum1 += cardValue;
  220.             System.out.println(sum1);
  221.             while (sum1 < 21){
  222.                 cardValue = randomGen.nextInt(11);
  223.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  224.                 int choice1 = console.nextInt();
  225.                 if (choice1 == 1){
  226.                     sum1 += cardValue;
  227.                     System.out.println(sum1);
  228.                 }
  229.             }
  230.             if (sum1 == 21){
  231.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  232.             }
  233.             else if (sum1 > 21){
  234.                 System.out.println("You bust. Restart the program to play again.");
  235.             }
  236.             int sum3 = 0;
  237.             cardValue = randomGen.nextInt(11);
  238.             sum1 += cardValue;
  239.             cardValue = randomGen.nextInt(11);
  240.             sum1 += cardValue;
  241.             System.out.println(sum1);
  242.             while (sum1 < 21){
  243.                 cardValue = randomGen.nextInt(11);
  244.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  245.                 int choice1 = console.nextInt();
  246.                 if (choice1 == 1){
  247.                     sum1 += cardValue;
  248.                     System.out.println(sum1);
  249.                 }
  250.             }
  251.             if (sum1 == 21){
  252.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  253.             }
  254.             else if (sum1 > 21){
  255.                 System.out.println("You bust. Restart the program to play again.");
  256.             }
  257.             int sum4 = 0;
  258.             cardValue = randomGen.nextInt(11);
  259.             sum1 += cardValue;
  260.             cardValue = randomGen.nextInt(11);
  261.             sum1 += cardValue;
  262.             System.out.println(sum1);
  263.             while (sum1 < 21){
  264.                 cardValue = randomGen.nextInt(11);
  265.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  266.                 int choice1 = console.nextInt();
  267.                 if (choice1 == 1){
  268.                     sum1 += cardValue;
  269.                     System.out.println(sum1);
  270.                 }
  271.             }
  272.             if (sum1 == 21){
  273.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  274.             }
  275.             else if (sum1 > 21){
  276.                 System.out.println("You bust. Restart the program to play again.");
  277.             }
  278.         }
  279.         else if (players == 5){
  280.             int sum1 = 0;
  281.             int cardValue = randomGen.nextInt(11);
  282.             sum1 += cardValue;
  283.             cardValue = randomGen.nextInt(11);
  284.             sum1 += cardValue;
  285.             System.out.println(sum1);
  286.             while (sum1 < 21){
  287.                 cardValue = randomGen.nextInt(11);
  288.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  289.                 int choice1 = console.nextInt();
  290.                 if (choice1 == 1){
  291.                     sum1 += cardValue;
  292.                     System.out.println(sum1);
  293.                 }
  294.             }
  295.             if (sum1 == 21){
  296.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  297.             }
  298.             else if (sum1 > 21){
  299.                 System.out.println("You bust. Restart the program to play again.");
  300.             }
  301.             int sum2 = 0;
  302.             cardValue = randomGen.nextInt(11);
  303.             sum1 += cardValue;
  304.             cardValue = randomGen.nextInt(11);
  305.             sum1 += cardValue;
  306.             System.out.println(sum1);
  307.             while (sum1 < 21){
  308.                 cardValue = randomGen.nextInt(11);
  309.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  310.                 int choice1 = console.nextInt();
  311.                 if (choice1 == 1){
  312.                     sum1 += cardValue;
  313.                     System.out.println(sum1);
  314.                 }
  315.             }
  316.             if (sum1 == 21){
  317.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  318.             }
  319.             else if (sum1 > 21){
  320.                 System.out.println("You bust. Restart the program to play again.");
  321.             }
  322.             int sum3 = 0;
  323.             cardValue = randomGen.nextInt(11);
  324.             sum1 += cardValue;
  325.             cardValue = randomGen.nextInt(11);
  326.             sum1 += cardValue;
  327.             System.out.println(sum1);
  328.             while (sum1 < 21){
  329.                 cardValue = randomGen.nextInt(11);
  330.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  331.                 int choice1 = console.nextInt();
  332.                 if (choice1 == 1){
  333.                     sum1 += cardValue;
  334.                     System.out.println(sum1);
  335.                 }
  336.             }
  337.             if (sum1 == 21){
  338.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  339.             }
  340.             else if (sum1 > 21){
  341.                 System.out.println("You bust. Restart the program to play again.");
  342.             }
  343.             int sum4 = 0;
  344.             cardValue = randomGen.nextInt(11);
  345.             sum1 += cardValue;
  346.             cardValue = randomGen.nextInt(11);
  347.             sum1 += cardValue;
  348.             System.out.println(sum1);
  349.             while (sum1 < 21){
  350.                 cardValue = randomGen.nextInt(11);
  351.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  352.                 int choice1 = console.nextInt();
  353.                 if (choice1 == 1){
  354.                     sum1 += cardValue;
  355.                     System.out.println(sum1);
  356.                 }
  357.             }
  358.             if (sum1 == 21){
  359.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  360.             }
  361.             else if (sum1 > 21){
  362.                 System.out.println("You bust. Restart the program to play again.");
  363.             }
  364.             int sum5 = 0;
  365.             cardValue = randomGen.nextInt(11);
  366.             sum1 += cardValue;
  367.             cardValue = randomGen.nextInt(11);
  368.             sum1 += cardValue;
  369.             System.out.println(sum1);
  370.             while (sum1 < 21){
  371.                 cardValue = randomGen.nextInt(11);
  372.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  373.                 int choice1 = console.nextInt();
  374.                 if (choice1 == 1){
  375.                     sum1 += cardValue;
  376.                     System.out.println(sum1);
  377.                 }
  378.             }
  379.             if (sum1 == 21){
  380.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  381.             }
  382.             else if (sum1 > 21){
  383.                 System.out.println("You bust. Restart the program to play again.");
  384.             }
  385.         }
  386.         else if (players == 6){
  387.             int sum1 = 0;
  388.             int cardValue = randomGen.nextInt(11);
  389.             sum1 += cardValue;
  390.             cardValue = randomGen.nextInt(11);
  391.             sum1 += cardValue;
  392.             System.out.println(sum1);
  393.             while (sum1 < 21){
  394.                 cardValue = randomGen.nextInt(11);
  395.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  396.                 int choice1 = console.nextInt();
  397.                 if (choice1 == 1){
  398.                     sum1 += cardValue;
  399.                     System.out.println(sum1);
  400.                 }
  401.             }
  402.             if (sum1 == 21){
  403.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  404.             }
  405.             else if (sum1 > 21){
  406.                 System.out.println("You bust. Restart the program to play again.");
  407.             }
  408.             int sum2 = 0;
  409.             cardValue = randomGen.nextInt(11);
  410.             sum1 += cardValue;
  411.             cardValue = randomGen.nextInt(11);
  412.             sum1 += cardValue;
  413.             System.out.println(sum1);
  414.             while (sum1 < 21){
  415.                 cardValue = randomGen.nextInt(11);
  416.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  417.                 int choice1 = console.nextInt();
  418.                 if (choice1 == 1){
  419.                     sum1 += cardValue;
  420.                     System.out.println(sum1);
  421.                 }
  422.             }
  423.             if (sum1 == 21){
  424.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  425.             }
  426.             else if (sum1 > 21){
  427.                 System.out.println("You bust. Restart the program to play again.");
  428.             }
  429.             int sum3 = 0;
  430.             cardValue = randomGen.nextInt(11);
  431.             sum1 += cardValue;
  432.             cardValue = randomGen.nextInt(11);
  433.             sum1 += cardValue;
  434.             System.out.println(sum1);
  435.             while (sum1 < 21){
  436.                 cardValue = randomGen.nextInt(11);
  437.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  438.                 int choice1 = console.nextInt();
  439.                 if (choice1 == 1){
  440.                     sum1 += cardValue;
  441.                     System.out.println(sum1);
  442.                 }
  443.             }
  444.             if (sum1 == 21){
  445.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  446.             }
  447.             else if (sum1 > 21){
  448.                 System.out.println("You bust. Restart the program to play again.");
  449.             }
  450.             int sum4 = 0;
  451.             cardValue = randomGen.nextInt(11);
  452.             sum1 += cardValue;
  453.             cardValue = randomGen.nextInt(11);
  454.             sum1 += cardValue;
  455.             System.out.println(sum1);
  456.             while (sum1 < 21){
  457.                 cardValue = randomGen.nextInt(11);
  458.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  459.                 int choice1 = console.nextInt();
  460.                 if (choice1 == 1){
  461.                     sum1 += cardValue;
  462.                     System.out.println(sum1);
  463.                 }
  464.             }
  465.             if (sum1 == 21){
  466.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  467.             }
  468.             else if (sum1 > 21){
  469.                 System.out.println("You bust. Restart the program to play again.");
  470.             }
  471.             int sum5 = 0;
  472.             cardValue = randomGen.nextInt(11);
  473.             sum1 += cardValue;
  474.             cardValue = randomGen.nextInt(11);
  475.             sum1 += cardValue;
  476.             System.out.println(sum1);
  477.             while (sum1 < 21){
  478.                 cardValue = randomGen.nextInt(11);
  479.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  480.                 int choice1 = console.nextInt();
  481.                 if (choice1 == 1){
  482.                     sum1 += cardValue;
  483.                     System.out.println(sum1);
  484.                 }
  485.             }
  486.             if (sum1 == 21){
  487.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  488.             }
  489.             else if (sum1 > 21){
  490.                 System.out.println("You bust. Restart the program to play again.");
  491.             }
  492.             int sum6 = 0;
  493.             cardValue = randomGen.nextInt(11);
  494.             sum1 += cardValue;
  495.             cardValue = randomGen.nextInt(11);
  496.             sum1 += cardValue;
  497.             System.out.println(sum1);
  498.             while (sum1 < 21){
  499.                 cardValue = randomGen.nextInt(11);
  500.                 System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
  501.                 int choice1 = console.nextInt();
  502.                 if (choice1 == 1){
  503.                     sum1 += cardValue;
  504.                     System.out.println(sum1);
  505.                 }
  506.             }
  507.             if (sum1 == 21){
  508.                 System.out.println("Congratulations! You win! Restart the program to play again.");
  509.             }
  510.             else if (sum1 > 21){
  511.                 System.out.println("You bust. Restart the program to play again.");
  512.             }
  513.         }
  514.     }
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement