Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: blackjack game using Java. It's for a computer programming class and were using BlueJ. Need help figuring out why it won't run correctly.

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default blackjack game using Java. It's for a computer programming class and were using BlueJ. Need help figuring out why it won't run correctly.

    Parent Class

    import java.util.Random;//This lets you generate random variables
    import javax.swing.JOptionPane;//This allows you to use the pop-up windows
    public class cards
    {
    public cards()//This is the default object setting.
    {
    total=0;
    }
    public cards(int n)//This is the setting for creating an object.
    {
    total=n;
    }
    public void addCardValue(int c1)//This creates a method for receiving the first two cards.
    {
    total=total+c1;//The total will now equal the value of the two cards you receive
    }
    public int getTotalCardValue()
    {
    return total;//This sends the total value to the subclass.
    }
    public void getNewCard()
    {
    Random r=new Random();
    int c1=r.nextInt(10)+1;//This generates a number between 1 and 10. This identifies what the value of your cards will be.
    String card1="0";
    if(c1==10)//If the number generated was 10.
    {
    int face=r.nextInt(4)+1;//This generates a number between 1 and 4, with the number generated defining what card you receive.
    switch (face)
    {
    case 1: card1="Jack"; break;//If it's 1, it's a Jack.
    case 2: card1="Queen"; break;//If it's 2, it's a Queen.
    case 3: card1="King"; break;//If it's 3, it's a King.
    case 4: card1="10"; break;//If it's 4, it's a 10.
    }


    }
    else if(c1==1)//If the random number generated was a 1
    {
    c1=0;//The card's value is changed to 0.
    card1="Ace";//It will be referred to as an ace.
    JOptionPane.showMessageDialog(null,"You got an Ace!");
    acetotal=acetotal+1;//The total number of aces increases by 1
    }
    else
    {
    switch (c1)
    {
    case 2: card1="2"; break;//If the random number generated was a 2, it's a 2.
    case 3: card1="3"; break;//If the random number generated was a 3, it's a 3.
    case 4: card1="4"; break;//If the random number generated was a 4, it's a 4.
    case 5: card1="5"; break;//If the random number generated was a 5, it's a 5.
    case 6: card1="6"; break;//If the random number generated was a 6, it's a 6.
    case 7: card1="7"; break;//If the random number generated was a 7, it's a 7.
    case 8: card1="8"; break;//If the random number generated was a 8, it's a 8.
    case 9: card1="9"; break;//If the random number generated was a 9, it's a 9.
    }
    }
    cardid=card1;
    cardvalue=c1;
    }
    public int getValue()
    {
    return cardvalue;//This sends the card's value to the subclass.
    }
    public void getAceValue()
    {
    ace1=11;//The total ace value will be set as 11 by default.
    ace2=0;//This is a temporary variable used to test ace values once you have more than one ace.
    if(acetotal>1)//If you have more than 1 ace.
    {
    ace2=11;
    if(total+ace1+ace2>21)//If your total card value plus the ace value plus the tester ace variable is greater than 21
    {
    total=total+1;//The second ace isn't added on, but instead, the 11 turns into a permenant 1.
    }
    }
    if(total+ace1>21)//If your total plus your ace value is greater than 21.
    {
    ace1=1;//The ace becomes a 1.
    }
    }
    public int getAce()
    {
    return ace1;//This sends the ace's value to the subclass.
    }
    public int getAceTotal()
    {
    return acetotal;//This sends the total number of aces to the subclass.
    }
    public void reset()//This resets all variables in the parent class back to their defaults, so that the game can be replayed.
    {
    total=0;
    acetotal=0;
    ace1=0;
    }
    private int total;
    private int cardvalue;
    private String cardid;
    private int ace1;
    private int ace2;
    private int acetotal;
    private int acevalue;
    }

    Subclass


    import javax.swing.JOptionPane;//This allows pop-up windows
    import java.util.Random;//This allows random variables
    public class subclass1
    {
    public static void main(String[]args)
    {
    cards cards=new cards(0);
    int next=1;
    int lose=0;
    int replay=1;
    int total=0;
    Random r=new Random();
    int opponent=r.nextInt(23+4);
    JOptionPane.showMessageDialog(null,"Welcome to Black Jack!");//This code creates a pop-up window with the message in parentheses.

    while(replay==1)//This while loop is used to repeat the entire code. If replay value is 1, the code within the brackets will repeat.
    {
    //This is the opponent's card value, and it generates a random number from 4 to 26.
    cards.reset();//This resets the total, the number of aces you have, and the total value of the aces.
    lose=0;
    next=1;

    while(next==1)//The program checks this variable every time the code within the brackets is completed. If it is equal to 1, it repeats everything within these brackets.
    {
    cards.getNewCard();//This calls the parent class and generates a new card.
    cards.addCardValue(cards.getValue());//This adds the value of the new card to your toal card value, unless it's an ace.
    Object[]options={"Hit", "Stand"};//These are the options that pop up.
    if(cards.getAceTotal()>0)//This will only run if an ace is present.
    {
    cards.getAceValue();//This calls the parent class and determines the value(s) of the ace(s) you have.
    }
    total=cards.getTotalCardValue()+cards.getAce();//This adds up your total card value and your total ace values.
    if(total>21)
    {
    Object[]options1={"Yes", "No"};//This defines the options that appear in pop-ups.
    next=0;//If next=0, the program will stop repeating the part which gets new cards to add.
    lose=1;//If lose=1, the program will skip over the pop-ups that would normally appear if you stand.
    int choice=JOptionPane.showOptionDialog(null,"You got a "+cards.getTotalCardValue()+". Your total is now "+total+". You lose! Do you want to play again?","You lose!",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null,options1,options 1[0]);
    if(choice==1)
    {
    replay=0;//If replay is set to 0,the program will end.
    }
    }
    if(lose==0)
    {
    int choice=JOptionPane.showOptionDialog(null,"You got a "+cards.getTotalCardValue()+". Your total is now "+total+". Do you hit or stand?","Hit or Stand",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
    if(choice==1)
    {
    next=0;//If next=0, the program will stop sending new card values.
    }
    }
    }
    if(lose==0)//You will only reach here if you lost without exceeding 21.
    {
    Object[]options={"Yes", "No"};//This defines the options that appear in pop-ups.
    if(total>opponent || opponent>21)//The things within these brackets will only occur if your total is greater than the opponent's OR if the opponent's total is greater than 21.
    {
    int choice=JOptionPane.showOptionDialog(null,"My card value is "+opponent+". You win! Do you want to play again?","You win!",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
    if(choice==1)
    {
    replay=0;//If replay is set to 0, the program can stop performing the while loop which starts further up and the program can reach it's end.
    }
    }
    if(total<opponent && opponent<=21)//The things within these brackets will only occur if your total is less than the opponent's AND if the opponent's total is less than or equal to 21.
    {
    int choice=JOptionPane.showOptionDialog(null,"My card value is "+opponent+". You lose! Do you want to play again?","You lose!",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
    if(choice==1)
    {
    replay=0;//If replay is set to 0, the program can stop performing the while loop which starts further up and the program can reach it's end.
    }
    }
    if(total==opponent)//The things within these brackets will only occur if your total equals the opponent's total.
    {
    int choice=JOptionPane.showOptionDialog(null,"My card value is "+opponent+". It's a tie. Do you want to play again?","Tie",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
    if(choice==1)
    {
    replay=0;//If replay is set to 0, the program can stop performing the while loop which starts further up and the program can reach it's end.
    }
    }
    }
    }
    }}

    End of Code

    I keep getting values of 0 for both the players hand and opponents hand, and i can't seem to get the whole hit or stand thing right, because when i click hit, it disregards any existing values. Any advice is greatly appreciated. Thanks in advance!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: blackjack game using Java. It's for a computer programming class and were using BlueJ. Need help figuring out why it won't run correctly.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

Similar Threads

  1. [SOLVED] Run JDK from USB for BlueJ
    By Abstract3000 in forum Java IDEs
    Replies: 2
    Last Post: January 9th, 2014, 05:45 AM
  2. Run JDK from USB for BlueJ
    By Abstract3000 in forum The Cafe
    Replies: 1
    Last Post: January 9th, 2014, 02:36 AM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. Blackjack programming error (programmer is new to programming)
    By JSingh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 09:13 PM
  5. Java BlackJack game
    By kas123 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 19th, 2012, 11:43 AM

Tags for this Thread