Search code examples
javablackjack

Blackjack Program Always forcing user to 'Hit' even if they say 'Stay'


So i'm making a blackjack program. I've successfully made the game almost fully except one bug type thing. The user gets the option to 'hit' or 'stay' as you usually would in blackjack but when it tells them their total in the end it will add the 2 'hits' even if they said stay twice. For example if I got a 4 and a 6 for a total of 10. Then I just stay twice to keep the 10. The program rolls 2 more numbers anyway and at the end will say the total of like 20 instead of the 10 I initially got. You can run my program to see more if you want so here is the code;

/////////////////////////////////////
// Name: Mackenzie Cutler
// Class: CP 12
// Date: March 28th, 2018
/////////////////////////////////////

import java.util.Scanner;
import java.util.Random;
public class MCproject3
{
  public static void main(String[] args)
  {
    Scanner k = new Scanner(System.in);
    Random ran = new Random();

    //Welcoming user & choosing their initial cards
    System.out.println("Welcome to the Cutler Casino Program. Currently playing Blackjack!");
    int a1 = ran.nextInt(11) + 1;
    int a2 = ran.nextInt(10) + 1;
    int a3 = ran.nextInt(11) + 1;
    int a4 = ran.nextInt(11) + 1;
    System.out.println ("\nYou get a " + a1 + " and a " + a2);
    System.out.println ("Your total is " + (a1+a2));

    //Choosing dealers initial cards and telling user
    int b1 = ran.nextInt(11) + 1;
    int b2 = ran.nextInt(10) + 1;
    int b3 = ran.nextInt(11) + 1;
    int b4 = ran.nextInt(11) + 1;
    System.out.println("\nThe dealer has a " + b1 + " showing, and a hidden card.");
    System.out.println("His total is hidden, too.");

    //User chooses to 'Hit' or 'Stay'
    System.out.print("\nWould you like to 'Hit' or 'Stay'?");
    String choice = k.nextLine();
    if(choice.equalsIgnoreCase ("hit"))
    {
      System.out.println("You drew a " + a3);
      System.out.println("Your total is " + (a1+a2+a3));
      if(a1+a2+a3 > 21)
      {
        System.out.println("You busted! Since you exceeded 21 the dealer wins, sorry.");
        return;
      }     
    }
    else if(choice.equalsIgnoreCase ("stay"))
    {
      System.out.println(" ");
    }
    else
    {
      System.out.println("Error. Make sure you typed either 'Stay' or 'Hit'. Please re-run the program :)");
    }

    //Second time user chooses to 'Hit' or 'Stay'
    System.out.print("\nWould you like to 'Hit' or 'Stay'?");
    String choice2 = k.nextLine();
    if(choice2.equalsIgnoreCase ("hit"))
    {
      System.out.println("You drew a " + a4);
      System.out.println("Your total is " + (a1+a2+a3+a4));
      if(a1+a2+a3+a4 > 21)
      {
        System.out.println("You busted! Since you exceeded 21 the dealer wins, sorry.");
        return;
      }     
    }
    else if(choice2.equalsIgnoreCase ("stay"))
    {
      System.out.println(" ");
    }
    else
    {
      System.out.println("Error. Make sure you typed either 'Stay' or 'Hit'. Please re-run the program :)");
    }

    //Dealers reveal and is his turn to choose 'Hit' and 'Stay'
    System.out.println("\nOkay, Dealers turn.");
    System.out.println("His hidden card was " + b2);
    System.out.println("His total was " + (b1+b2));
    int dchoice = ran.nextInt(2) + 1;

    if(dchoice == 1)
    {
      System.out.println("\nDealder chooses to hit.");
      System.out.println("He draws a " + b3);
      System.out.println("His total is now " + (b1+b2+b3));
      if(b1+b2+b3 > 21)
      {
        System.out.println("Dealer busted! Since he exceeded 21 you WIN!!");
        return;
      }     
    }
    else if(dchoice == 2)
    {
      System.out.println("\nDealer chooses to stay.");
    }
    else
    {
      System.out.println("Error 404. Program Failed, We are sorry. Please restart.");
    }

    //Dealers second 'Hit' or 'Stay' random choice
    int dchoice2 = ran.nextInt(2) + 1;
    if(dchoice2 == 1)
    {
      System.out.println("\nDealder chooses to hit.");
      System.out.println("He draws a " + b4);
      System.out.println("His total is now " + (b1+b2+b3+b4));
      if(b1+b2+b3+b4 > 21)
      {
        System.out.println("Dealer busted! Since he exceeded 21 you WIN!!");
        return;
      } 
    }
    else if(dchoice == 2)
    {
      System.out.println("\nDealer chooses to stay.");
    }
    else
    {
      System.out.println(" ");
    }


    //Ending
    int totala = (a1+a2+a3+a4);
    int totalb = (b1+b2+b3+b4);

    System.out.println("\nDealers total is " + (b1+b2+b3+b4));
    System.out.println("Your total is " + (a1+a2+a3+a4));
    if(totala > totalb)
    {
      if(totala <= 21)
      {
      System.out.println("\nYou WIN!");
      }
      else if(totala > 21)
      {
        System.out.println("\nYou busted so you wont win :(");
      }
    }
    else if(totala < totalb)
    {
      if(totalb <= 21)
      {
        System.out.println("\nSorry, Dealer Wins.");
      }
      else if(totalb > 21)
      {
        System.out.println("Dealer busted so you win!");
      }
    }
    else
    {
      System.out.println("\nError 405. Program Failed, We are sorry. Please restart.");
    }
  }
}

I just want to know if you think there is anything I did wrong or should do differently to make it work correctly.


Solution

  • The problem is very simple yet elusive.

    You have displayed a1+a2 for the first total. However, while displaying the results, you show a1+a2+a3+a4. Now, a3 and a4 are already initialized to some random numbers, therefore the final result will always be more than the initial, even if you 'stay' twice.

    Solution -

    1. To fix this, you can create a variable called int UserTotal = a1+a2, and add a3 to it if the user 'hits' the first time, and a4 to it if the user 'hits' the second time. Then, initialize totala to UserInput.
    2. Or, you can set a3 and a4 to 0 at the very start of the program, and if the user 'hits' the first time, you can set a3 to a random value, and if user hits the second time, a4 can be set to a random value.