Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Blackjack game question.

Options
  • 18-04-2012 5:20am
    #1
    Registered Users Posts: 130 ✭✭


    Hey guys, basically i'm doing a simple blackjack game with standard rules (no betting system ). I'm happy with the majority of my code, just the bit that's getting me is, regardless if I get >5 cards to the total of less than 21, or if I get blackjack with less than 5 cards it doesn't prompt me with my blackjack message. Any help would be greatly appreciated :)
    Here's my code here anyway if anyone's interested:


    import java.util.Random;
    import java.util.Scanner;
    import static java.lang.System.out;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;

    public class BlackJackJava{


    public static void main(String args[]) throws IOException
    {
    BufferedReader br = new BufferedReader(
    new InputStreamReader(System.in));

    // Here i am declaring the usernames and passwords for entry to the program aswell as declaring a greeting string.
    String User1 = new String("Gerry");
    String User2 = new String("Evelyn");
    String User3 = new String("Joan");
    String Pass1 = new String("poker");
    String Pass2 = new String("bonus");
    String Pass3 = new String("spintown");
    String Username;
    String Password;
    Random randomizer = new Random();
    Scanner s = new Scanner (System.in);
    int firstcard = 0;
    int secondcard = 0;
    int dealerfirstcard = 0;
    int dealersecondcard = 0;
    int nextcard = 0;
    int dealernextcard = 0;
    int totalamount = 0;
    int dealer = 0;
    int play = 1;
    int reset = 10;
    boolean won = false;


    System.out.println("Welcome to BLACKJACK. Please enter your login info below to begin!");

    System.out.println("Please enter your Username:");
    Username = br.readLine();
    System.out.println("Welcome" + " " + Username);

    System.out.println("Please enter your Password:");
    Password = br.readLine();
    System.out.println("Thank you" + " " + Username);


    if ((Username.equals(User1)) && (Password.equals(Pass1)))
    System.out.println("Your Password Has Been Accepted");

    else if ((Username.equals(User2)) && (Password.equals(Pass2)))
    System.out.println("Your Password Has Been Accepted");

    else if ((Username.equals(User3)) && (Password.equals(Pass3)))
    System.out.println("Your Password Has Been Accepted");

    else

    {
    System.out.println("Username or Password is Incorrect");
    System.exit(0);
    }

    //The welcome message

    System.out.println ("Welcome to BLACKJACK!");
    System.out.println ("Dealing Cards...");

    //Your First hand is automatically dealt to you

    firstcard = (randomizer.nextInt(10) +1 );
    secondcard = (randomizer.nextInt(10) +1 );
    dealerfirstcard = (randomizer.nextInt(10) +1 );
    dealersecondcard = (randomizer.nextInt(10) +1 );

    // If statements for resetting the value of a piture card to 10

    if ((firstcard) > 10)
    (firstcard) = (reset);

    else if ((firstcard) > 10)
    (firstcard) = (reset);

    else if ((secondcard) > 10)
    (secondcard) = (reset);

    else if ((secondcard) > 10)
    (secondcard) = (reset);

    else if ((dealerfirstcard) > 10)
    (dealerfirstcard) = (reset);

    else if ((dealerfirstcard) > 10)
    (dealerfirstcard) = (reset);

    else if ((dealersecondcard) > 10)
    (dealerfirstcard) = (reset);

    else if ((dealersecondcard) > 10)
    (dealerfirstcard) = (reset);

    dealer = (dealer + dealerfirstcard + dealersecondcard);
    totalamount = (firstcard + secondcard + totalamount);

    System.out.println ("Your first card was a " + (firstcard) + " your second card was a " + (secondcard));
    System.out.println ("Your total score is " + (totalamount));

    //If statement for if the user gets dealt blackjack

    if ((firstcard) > (10) && (secondcard) > (9) && (secondcard) < (11) )

    {
    System.out.println ("You got a blackjack! You win!");
    System.exit(0);
    }

    else if ((secondcard) > (10) && (firstcard) > (9) && (firstcard) < (11) )

    {
    System.out.println ("You got a blackjack! You win!");
    System.exit(0);
    }



    System.out.println ("Press 1 to hit again, or 0 if you would like to stand: ");
    play = Integer.parseInt(br.readLine());

    //The main stand/hit loop

    while ((play) == 1 && (totalamount) <= 21)
    {
    nextcard = (randomizer.nextInt(12) +1 ) ;
    dealernextcard = (randomizer.nextInt(12) +1 ) ;
    if ((dealer) < 17)
    (dealer) = ((dealer) + (dealernextcard));
    if ((nextcard) > 10)
    (nextcard) = (reset);
    else if ((nextcard) > 10)
    (nextcard) = (reset);
    else if ((dealernextcard) > 10)
    (dealernextcard) = (reset);
    else if ((dealernextcard) > 10)
    (dealernextcard) = (reset);
    totalamount = (nextcard + totalamount);
    System.out.println ("You picked up a card with the value of " + (nextcard));
    System.out.println ("Your total amount is " + (totalamount));
    if ((totalamount) > 21)
    {
    System.out.println("You've went bust!");
    System.exit(0);
    }
    else {System.out.println ("Press 1 to hit or 0 to stand."); }

    play = (s.nextInt());
    }
    //If statements to determine the outcome of the game
    if ((totalamount) > 21) {
    out.println ("You've went bust!");
    }else {
    System.out.print ("Your final amount was " + (totalamount));
    System.out.print ("The dealers amount was " + (dealer));
    if ((totalamount) > (dealer) && (totalamount) < 22) {
    out.println ("You've won!");
    }
    else if ((totalamount) < (dealer) && (dealer) < 22) {
    System.out.println ("You lose, sorry!");
    } else if ((totalamount) < (dealer) && (dealer) > 21) {
    System.out.println ("Dealer went bust!"); }
    else if ((totalamount) == (dealer)) {
    System.out.println ("You've drawn!");
    }
    }






    }
    }


Comments

  • Registered Users Posts: 8,324 ✭✭✭chrislad


    I'm only learning Java and it's been a while before that since I did any programming but after a quick skim, I don't see a variable for counting how many cards were issued?

    I think you're importing JOptionPane twice too, but I don't know if that would have a difference (import java.swing.* and import java.swing.JOptionPane are both declared)


  • Registered Users Posts: 1,079 ✭✭✭Rulmeq


    chrislad wrote: »
    I'm only learning Java and it's been a while before that since I did any programming but after a quick skim, I don't see a variable for counting how many cards were issued?

    I think you're importing JOptionPane twice too, but I don't know if that would have a difference (import java.swing.* and import java.swing.JOptionPane are both declared)

    imports in java do nothing more than allow the user to specify a class name without fully qualifying it. By convention it's considered best practice not to import *, but you aren't going to cause issues for the compiler, just yourself, and those who have to maintain the code (and if you use an IDE, it can sort out the problems for you in an instant)


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    Rulmeq wrote: »
    imports in java do nothing more than allow the user to specify a class name without fully qualifying it. By convention it's considered best practice not to import *, but you aren't going to cause issues for the compiler, just yourself, and those who have to maintain the code (and if you use an IDE, it can sort out the problems for you in an instant)

    I figured as much but no harm in pointing it out that it was declared twice :)


  • Registered Users Posts: 498 ✭✭bobbytables


    From your code it looks like you are making light use of Java's existing features, which is causing you to declare variables excessively amongst other things.

    However, this isn't always a band thing when learning a new language as you will appreciate some of the more advanced features when you get to put them in to practice, when compared to using the same primitive constructs for every job. E.g. the Collections framework or even just Arrays (for a start) would help you in this particular case.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    I didn't get through all your code but this part didn't look right. First of all you have every if statement repeated for some reason. Secondly it looks as if the code in a number of the if and else if statements should be executed but this else if branching only allows one piece of code to be executed. Lastly and although not very important you have everything surrounded in unneeded brackets. Purely for readability reasons you should only really have the if condition surrounded with a single ( and ). How you have done it doesn't actually affect the code but cleaner code is better code.
    // If statements for resetting the value of a piture card to 10
    
    if ((firstcard) > 10)
    (firstcard) = (reset);
    
    else if ((firstcard) > 10)
    (firstcard) = (reset);
    
    else if ((secondcard) > 10)
    (secondcard) = (reset);
    
    else if ((secondcard) > 10)
    (secondcard) = (reset);
    
    else if ((dealerfirstcard) > 10)
    (dealerfirstcard) = (reset);
    
    else if ((dealerfirstcard) > 10)
    (dealerfirstcard) = (reset);
    
    else if ((dealersecondcard) > 10)
    (dealerfirstcard) = (reset);
    
    else if ((dealersecondcard) > 10)
    (dealerfirstcard) = (reset);
    

    The following would make more sense (or you could use the ternary operator instead of if statements if you are familiar with it):
    // If statements for resetting the value of a piture card to 10
    
    if (firstcard > reset)
    firstcard = reset;
    
    if (secondcard > reset)
    secondcard = reset;
    
    if (dealerfirstcard > reset)
    dealerfirstcard = reset;
    
    if (dealersecondcard > reset)
    dealersecondcard = reset;
    

    The following is how you would use the ternary operator:
    firstcard = firstcard > reset ? reset : firstcard;
    
    I don't know if this is the only issue in the code. The code in the while loop looks fairly messy but it may be alright, I might have time to look through it a bit later.


  • Advertisement
Advertisement