Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Darya Zykin
- //Period: 4A
- import java.util.*;
- public class FullBlackjack
- {
- /*
- If you EVER copy and paste your code, you must realize that there is a simpler way to do it. Whether you
- delegate it to a new method or use a loop, there almost is always a way to simplify it. This is an example
- of it not being delegated.
- I haven't run your code, but I can tell just looking at the code that 2 player does not work. I haven't
- looked at the other players, but I think the same problem exists in other sections. The fact that so many
- variables exist with similar names makes one go panic mode and miss renaming those variables. That should
- also be another clue that "this can be simplified."
- When code is blown to such a proportion, it becomes *unmaintainable.* Last year, our UIL state team got an
- entire question wrong because somebody copy and pasted code, changed it one place and forgot to change it
- in the other place. That was the entire reason.
- When doing this copy-and-pasting, you should look at your game and think, "what is the pattern here?"
- In your case, you do the same exact actions *for each* player. The fact I said *for each* should make
- a lightbulb in your head thinking in terms of loops.
- Go look at your code. Read each of the `players == NUMBER` if statements and tell me if you can see a
- pattern. After doing that for a little, even if you don't see it, continue reading.
- If you haven't noticed, you are doing the summation, input, and computation for each player every single
- if statement, for each player. Refer to my other file now.
- */
- public static void main(String[] args)
- {
- Scanner console = new Scanner(System.in);
- //Full Blackjack
- Random randomGen = new Random();
- System.out.print("Please enter amount of players (up to 6): ");
- int players = console.nextInt();
- int dealerSum = 0;
- if (players == 1){
- int sum1 = 0;
- int cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- else if (choice1 == 2){
- break;
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- dealerSum = 0;
- cardValue = randomGen.nextInt(11);
- dealerSum += cardValue;
- cardValue = randomGen.nextInt(11);
- dealerSum += cardValue;
- if (dealerSum <= 15){
- dealerSum += cardValue;
- }
- else if (dealerSum >= 16){
- if (sum1 == 21){
- System.out.println("Dealer wins! Restart the program to play again");
- }
- else if (sum1 > 21){
- System.out.println("Dealer busts. Restart the program to play again.");
- }
- }
- }
- else if (players == 2){
- int sum1 = 0;
- int cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum2 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum2);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum2 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- }
- else if (players == 3){
- int sum1 = 0;
- int cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum2 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum3 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- }
- else if (players == 4){
- int sum1 = 0;
- int cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum2 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum3 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum4 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- }
- else if (players == 5){
- int sum1 = 0;
- int cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum2 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum3 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum4 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum5 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- }
- else if (players == 6){
- int sum1 = 0;
- int cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum2 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum3 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum4 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum5 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- int sum6 = 0;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- cardValue = randomGen.nextInt(11);
- sum1 += cardValue;
- System.out.println(sum1);
- while (sum1 < 21){
- cardValue = randomGen.nextInt(11);
- System.out.print("Will you hit or stay ( 1 for hit, 2 for stay)");
- int choice1 = console.nextInt();
- if (choice1 == 1){
- sum1 += cardValue;
- System.out.println(sum1);
- }
- }
- if (sum1 == 21){
- System.out.println("Congratulations! You win! Restart the program to play again.");
- }
- else if (sum1 > 21){
- System.out.println("You bust. Restart the program to play again.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement