gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:26:23 25 4
gpt4 key购买 nike

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

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。任何帮助将不胜感激。谢谢

最佳答案

你的代码看起来太复杂了。无需为每个 A 分配一个变量,只需保留未减少到 1 的 A 数量计数即可:

private int fullAceCount;

更新假设 A 为 11,同时也更新 A 计数:

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
}

你就完成了。

关于java - 检查 ace 是否存在以及点数是否 >21,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13543516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com