简体   繁体   English

Java,Blackjack无法正确计算

[英]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. 示例:一个Jack和一个3计算到20,Ace和3计算为13.我知道我没有添加一个部分来制作Ace 1或11,我想先计算出计算结果。

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() );
    }
}
}
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. 应该在循环之前调用shuffle() ,而不是在循环期间调用。 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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