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

时间:2012-11-24 17:12:29

标签: java

在我的二十一点程序中,我有:

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

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;
    }

由于某种原因,当积分超过21时,这并没有改变ace值,也没有调整ace。 任何帮助将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:2)

您的代码似乎太复杂了。而不是为每个ace设置一个变量,只需将计数保存为未减少到1的aces数:

private int fullAceCount;

更新假设11为ace,但也更新了ace计数:

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

然后在更新任何卡后,检查您是否需要扣除10

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

你已经完成了。