NoobCoderChick
NoobCoderChick

Reputation: 627

Java, Blackjack not computing correctly

I'm working on turning a simple school assignment into an actual game. FYI, it's all extra credit what I'm doing.

For some reason the cards aren't computing correctly. Example: A Jack and a 3 are computing to 20, Ace and 3 as 13. I know that I haven't added a part to make an Ace 1 or 11, I want to figure out the calculations first.

I'm going to just add the class in question but if you want all of my code please let me know.

public class BlackJack extends CardGame{

int computePlayerValue, computeDealerValue;
int cardValue1, cardValue2;


public BlackJack() {
    super();
    playerHand = 2;
    dealerHand = 2;
}

public void display() {
    System.out.println("BlackJack");
}   

public void playGame() {

}

public int getCardValue(Card card) {
    final String rank = card.getRank();
    switch (rank) {
        case "Ace":
            return 1;   
        case "King":
            return 10;
        case "Queen":
            return 10;
        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("Player has: " + computePlayerValue);



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

Upvotes: 1

Views: 49

Answers (1)

John Kugelman
John Kugelman

Reputation: 361585

for(int x = 0; x < playerHand; x++) {
    shuffle();
    System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit());
}

shuffle() should be called before the loop, not during it. Shuffling the cards while you're looping over them is changing the cards after you've printed them. That's why your results are mixed up.

shuffle();
for(int x = 0; x < playerHand; x++) {
    System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit());
}

Upvotes: 3

Related Questions