I don’t think I’m getting that exception anymore but I’m getting some other error or something but I don’t remember what it is. I’ll see if I can post it after I get back from class.
Alright, so due to lack of time to work on this, I’m gonna be do the gui as a side project and I’m gonna do this through text. I’m having trouble figuring out how to go about adding values from the cards to get the player/computer’s score. So here’s what I have.
public class BlackJack {
Player player = new Player();
Player dealer = new Player();
Deck deck = new Deck();
boolean valid;
enum Status {WON, LOST, CONTINUE};
Status gameStatus = Status.CONTINUE;
public void run()
{
JOptionPane.showMessageDialog(null, "Welcome to the wonderful world of Black Jack!");
String temp = JOptionPane.showInputDialog("How much do you wish to"
+ " sacrifice to the BlackJack gods?");
while(!valid)
{
if( Double.parseDouble(temp) > 0.0){
valid = !valid;
player.setCredits(Double.parseDouble(temp));
System.out.println("Your credits: " + player.getCredits());
}
else
{
temp = JOptionPane.showInputDialog("Invalid, enter a positive"
+ " number or q to exit");
if(temp.equals("q"))
System.exit(0);
}
}
while(gameStatus.equals(gameStatus.CONTINUE))
{
valid = !valid;
temp = JOptionPane.showInputDialog("How much you wanna bet bro?");
while(!valid)
{
if(Double.parseDouble(temp) > 0.0 &&
Double.parseDouble(temp) <= player.getCredits())
{
valid = !valid;
player.setBet(Double.parseDouble(temp));
System.out.println("Betting: " + player.getBet());
player.setCredits(player.getCredits() - player.getBet());
}
else
{
temp = JOptionPane.showInputDialog("Invalid or insufficient"
+ " funds, enter a positive number");
}
}
System.out.println("Dealing Cards...");
deck.shuffle();
player.playerCards.add(deck.dealCard());
dealer.playerCards.add(deck.dealCard());
player.playerCards.add(deck.dealCard());
dealer.playerCards.add(deck.dealCard());
System.out.printf("Dealer shows: %s\nYou have: %s\n",
dealer.displaySecondCard(), player.displayHand());
System.out.println(player.getCardValue());
}
}
}
public class Deck {
private Card[] deck;
private int currentCard;
String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private static final int NUMBER_OF_CARDS = 52;
private static final Random myR = new Random();
public Deck()
{
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
deck = new Card[NUMBER_OF_CARDS];
currentCard = 0;
for (int count = 0; count < deck.length; count++)
deck[count] = new Card(faces[count % 13], suits[count / 13]);
}
public void shuffle()
{
currentCard = 0;
for(int first = 0; first < deck.length; first++)
{
int second = myR.nextInt(NUMBER_OF_CARDS);
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
public Card dealCard()
{
if(currentCard < deck.length)
return deck[currentCard++];
else
return null;
}
}
public class Player extends Deck{
private double bet;
private double credits;
private double insurance;
boolean hasBlackJack, hasInsurance;
public ArrayList< Card > playerCards = new ArrayList< Card >();
public double getBet()
{
return bet;
}
public void setBet(double num)
{
bet = num;
}
public double getCredits()
{
return credits;
}
public void setCredits(double num)
{
credits = num;
}
public double getInsurance()
{
return insurance;
}
public void setInsurance(double num)
{
insurance = num;
}
public String displaySecondCard()
{
return String.format("%s", playerCards.get(1));
}
public String displayHand()
{
return String.format("%s, %s", playerCards.get(0), playerCards.get(1));
}
public int getCardValue()
{
int pointValue = 0;
for(int j = 0; j < 2; j++)
{
for(int i = 0; i < faces.length; i++)
{
if(faces[i].equals("Ace"))
{
pointValue += 11;
break;
}
if(faces[i].equals("Two"))
{
pointValue += 2;
break;
}
if(faces[i].equals("Three"))
{
pointValue += 3;
break;
}
if(faces[i].equals("Four"))
{
pointValue += 4;
break;
}
if(faces[i].equals("Five"))
{
pointValue += 5;
break;
}
if(faces[i].equals("Six"))
{
pointValue += 6;
break;
}
if(faces[i].equals("Seven"))
{
pointValue += 7;
break;
}
if(faces[i].equals("Eight"))
{
pointValue += 8;
break;
}
if(faces[i].equals("Nine"))
{
pointValue += 9;
break;
}
else
pointValue += 10;
}
}
return pointValue;
}
I’ve been messing with it for around 3-4 hours and can’t figure it out. Any help is appreciated.
Instead of looking through every line of code you have, you could tell us exactly what’s not working correctly so we can find the problem easier.
public int getCardValue()
{
int pointValue = 0;
for(int j = 0; j < 2; j++)
{
for(int i = 0; i < faces.length; i++)
{
if(faces[i].equals("Ace"))
{
pointValue += 11;
break;
}
if(faces[i].equals("Two"))
{
pointValue += 2;
break;
}
if(faces[i].equals("Three"))
{
pointValue += 3;
break;
}
if(faces[i].equals("Four"))
{
pointValue += 4;
break;
}
if(faces[i].equals("Five"))
{
pointValue += 5;
break;
}
if(faces[i].equals("Six"))
{
pointValue += 6;
break;
}
if(faces[i].equals("Seven"))
{
pointValue += 7;
break;
}
if(faces[i].equals("Eight"))
{
pointValue += 8;
break;
}
if(faces[i].equals("Nine"))
{
pointValue += 9;
break;
}
else
pointValue += 10;
}
}
return pointValue;
}
This is pretty much the part I’m trying to figure out. I was wanting to maybe set a value for the card in the card class, but I couldn’t really get it working, so I tried doing this. But this just looks at the faces string array of the deck class and I want it to be looking at the cards dealt to the player. I tried using the array I’m using for the cards for the player doing playerCards.get(i).equals() but it wants an object and so I can’t do that either.
hmm it’s only me or everyone can’t see posted code above?
Should I try reposting it?
I can see the code. But I don’t see a Card class defined. I do see the use of a Card[]. So, I assume it must be around somewhere.
So, how do you assign text to the card? Why not assign a point value at the same time? I’d assume a card could easily have a private “instance variable” to hold the point value, as well as a public getter and setter for the same.
But there are a lot of other things that don’t make sense to me. Why is there an outer loop with “j” in your hand point totaller? I see no references to “j” and don’t understand why you would want to total up your points multiple times. Or do a multiplication, if you do.
More seriously, why does Player extend Deck? Usually with an “extends” you have what they refer to in Head First Java lessons as an “is a” relationship. A “player” generally isn’t thought of as a particular case/example of a deck of cards. A player might hold some cards in a hand. Just saying.
Well playerCards.get(i) would return an object of Card. As philfrei noticed, you haven’t shown us a Card class. The Card class could be useful to store the name and point value of each card. So you would be able to do:
int points = 0;
for(Card c : playerCards) {
points += c.getValue();
}
Seems only me who can’t see it so no need. maybe some problem here. All code after BlackJack class code are blank and I can’t select anything. Not happen on other threads though.
Oops, didn’t realize I didn’t put the Card class up.
public class Card{
private String face;
private String suit;
public Card(String cardFace, String cardSuit)
{
face = cardFace;
suit = cardSuit;
}
@Override
public String toString()
{
return face + " of " + suit;
}
}
I was pretty tired when I was trying to work on this last night so that’s why some parts look pretty nonsensical .
I did that and used the bit of code ra4king posted and it works, at least as far as I can tell so far Thanks guys.