简体   繁体   English

检查ace是否存在以及点是否> 21

[英]checking if ace exists and if points >21

In my blackjack program i have: 在我的二十一点程序中,我有:

Card c = dealCard(deck); //deals the card
updatePoints(players[i], c); // calls updatePoints below. 

The updatePoints function looks like this: updatePoints函数如下所示:

public static void updatePoints(Player player,  Card c){

        int point = c.getValue();
        if(player.getPoints() + point >21 && (player.ace1 == 11 || player.ace2 == 11 || player.ace3 == 11 || player.ace4 == 11)){
            player.points -= 10;
            player.setPoints(point);
            if (player.ace1 == 11){
                player.ace1 = 1;
            }else if(player.ace2 == 11){
                player.ace2 = 1;
            }else if(player.ace3 == 11){
                player.ace3 = 1;
            }else if (player.ace4 == 11){
                player.ace4 = 1;
            }
        }
        if (point == 1){
            //default value for player.ace1 .. player.ace4 is 0


            if(player.ace1 == 0){
                player.ace1 = 11;
                player.setPoints(11);
            }else if (player.ace2 == 0){
                player.ace2 = 11;
                player.setPoints(11);
            }else if (player.ace3 == 0){
                player.ace3 = 11;
                player.setPoints(11);
            }else if(player.ace4 == 0){
                player.ace4 = 11;
                player.setPoints(11);
            }

        }else{
            player.setPoints(point);
        }
        return;
    }

for some reason when the points go over 21 this doesnt change the ace value and doesnt adjust the ace. 出于某种原因,当积分超过21时,这并没有改变ace值,也没有调整ace。 any help will be appreciated. 任何帮助将不胜感激。 Thank you 谢谢

Your code seems too complicated. 你的代码看起来太复杂了。 Rather than having a variable for each ace, simply keep a count of the number of aces you have not reduced to a 1 : 而不是为每个ace设置一个变量,只需保留未减少到1 a 数的计数

private int fullAceCount;

update assuming 11 for an ace, but also update the ace count: 更新为假设11为ace,但也更新ace计数:

if (points == 11) { // if it's an ace
    fullAceCount++; // save it for later deduction
}

then after updating for any card, check if you you need to deduct 10 然后在更新任何卡后,检查您是否需要扣除10

if (total > 21 && fullAceCount > 0) {
    total =- 10;
    fullAceCount--; // record that you've used up one of your aces
}

and you're done. 你完成了

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

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