简体   繁体   中英

Java blackjack program

So I rewrote it with your suggestion (Todd Hopp) and updated the post in the (*) section in the BlackJack class so you can see how I have it now but I'm still getting the same errors and it is not printing the computePlayerValue for some reason....


ERROR: Exception in thread "main" java.lang.NumberFormatException: For input string: "King" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at BlackJack.dealCards(BlackJack.java:25)
at PlayCardGame.main(PlayCardGame.java:9)

PS I'm was trying to look this up in the book but couldn't find the answer...The BlackJack class is partially source code from an earlier chapter in the book and I couldn't figure out if it was necessary to have super(); in the constructor there? I thought it only had to do if the parent class constructor had an argument? Any help on if it has to be there and if so what it's doing.

BlackJack Class

 public class BlackJack extends CardGameFP{
    int computePlayerValue;
    int computeDealerValue;
    int cardValue;

    public BlackJack() {
        **super();**
        player1 = 2;
        player2 = 2;
    }

    public void display() {
        System.out.println("BlackJack");
    }
//************************************************************* 
    public int getCardValue(Card card) {
    final String rank = card.getRank();
    switch (rank) {
        case "Ace":
            return 1;
        case "King":
        case "Queen":
        case "Jack":
            return 10;
        default:
            return Integer.parseInt(rank);
    }
}

public void dealCards() {
    //Player 1
    System.out.println("Player 1:");
    for(int x = 0; x < playerHand; x++) {
        shuffle();
        System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit());
    }
    cardValue1 = getCardValue(fullDeck[0]);
    cardValue2 = getCardValue(fullDeck[1]);
    computePlayerValue = cardValue1 + cardValue2;
    System.out.println(computePlayerValue);
}

//*************************************************************


        //Dealer hand
        System.out.println("\nPlayer 2:");
        for(int x = 0; x < player2; x++) {
            System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit() );
            shuffle();
        }
    }
    }


public class Card {

protected String suit;
protected int value;
protected String rank;
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;


public String getRank() {
    return rank;
}

public int getValue() {
    return value;
}

public String getSuit() { 
    return suit;
}

public void setSuit(String st) {
    suit = st;
}

public void setValue(int val) {
    if(val >= LOW_VALUE && val <= HIGH_VALUE) {
        value = val;
    }
    else {
        value = LOW_VALUE;
    }
    if(val == 1) {
        rank = "Ace";
    }
    else if(val == 11) {
        rank = "Jack";
    }
    else if(val == 12) {
        rank = "Queen";
    }
    else if(val == 13) {
        rank = "King";
    }
    else {
        rank = Integer.toString(value);
    }           
}   
}

CardGame Class

abstract public class CardGameFP {

int suitNum = 1;
int val = 1;
int player1;
int player2;
protected final int DECK_OF_CARDS = 52;
Card fullDeck[] = new Card[DECK_OF_CARDS];
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;
protected final int HIGH_SUIT = 4;
protected final int CARDS_IN_SUIT = 13;

public abstract void display();
public abstract void dealCards();

public CardGameFP() {
    for(int i = 0; i < fullDeck.length; i++) {
        fullDeck[i] = new Card();
        if(suitNum == 1) {
            fullDeck[i].setSuit("Spades");
        }
        else if(suitNum == 2) {
            fullDeck[i].setSuit("Hearts");
        }
        else if(suitNum == 3) {
            fullDeck[i].setSuit("Diamonds");
        }
        else {
            fullDeck[i].setSuit("Clubs");
        }
        fullDeck[i].setValue(val);
        val++;
        if(val > HIGH_VALUE) {
            suitNum++;
            val = 1;
        }            
    }//end for
}   
public void shuffle() {
    for(int firstCard = 0; firstCard < DECK_OF_CARDS; firstCard++ ) {
        firstCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
        int secondCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
        Card temp = fullDeck[firstCard];
        fullDeck[firstCard] = fullDeck[secondCard];
        fullDeck[secondCard] = temp;
    }
}
}

PlayCardGame Class

PlayerCardGame

    public class PlayCardGame {

    public static void main(String[] args) {
        Card CG = new Card();
        BlackJack BJ = new BlackJack();

        BJ.display();
        BJ.dealCards();
    }
    }

Instead of this line:

cardValue = Integer.parseInt(fullDeck[0].getRank());

I suggest that you create a getCardValue(Card) method:

public int getCardValue(Card card) {
    final String rank = card.getRank();
    switch (rank) {
        case "Ace":
            return 1;
        case "King":
        case "Queen":
        case "Jack":
            return 10;
        default:
            return Integer.parseInt(rank);
    }
}

and then use:

cardValue = getCardValue(fullDeck[0]);

EDIT: Following the suggestion by @WorldSEnder, you can define a Rank enum:

public enum Rank {
    ACE("Ace", 1),
    TWO("2", 2),
    ...
    QUEEN("Queen", 10),
    KING("King", 10);

    private final String displayName;
    private final int value;

    protected Rank(String displayName, int value) {
        this.displayName = displayName;
        this.value = value;
    }

    public String displayName() {
        return displayName;
    }

    public int value() {
        return value;
    }
}

Then you can modify Card to use a Rank instead of a String for the rank and use:

cardValue = fullDeck[0].getRank().value();

When you call :

 cardValue = Integer.parseInt(fullDeck[0].getRank());    

the method Interger.parseInt() is attempting to read an int from a string . The problem is that some cards have ranks which themselves are not strings like, King . So when you return the rank of that card, parseInt() doesn't know how to handle King . You should instead be getting the cards actual value using fullDeck[0].getVaule() and parsing that instead. It will be a string between 1 and 13.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM