首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查ace是否存在,如果分数>21

检查ace是否存在,如果分数>21
EN

Stack Overflow用户
提问于 2012-11-25 01:12:29
回答 1查看 61关注 0票数 0

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

代码语言:javascript
复制
Card c = dealCard(deck); //deals the card
updatePoints(players[i], c); // calls updatePoints below. 

updatePoints函数如下所示:

代码语言:javascript
复制
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值。任何帮助都将不胜感激。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-25 01:42:33

您的代码似乎太复杂了。不需要为每个ace设置一个变量,只需对未缩减为1的ace数量进行计数即可

代码语言:javascript
复制
private int fullAceCount;

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

代码语言:javascript
复制
if (points == 11) { // if it's an ace
    fullAceCount++; // save it for later deduction
}

代码语言:javascript
复制
if (total > 21 && fullAceCount > 0) {
    total =- 10;
    fullAceCount--; // record that you've used up one of your aces
}

你就完事了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13543516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档