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!