gpt4 book ai didi

java - 二十一点游戏的逻辑

转载 作者:行者123 更新时间:2023-12-01 11:43:24 24 4
gpt4 key购买 nike

我正在编写一个二十一点游戏,但我已经完成了很多工作。然而,我刚刚到了将每手牌后的分数相加的地步(我认为这很容易),但 Aces 却让我无休无止地绞尽脑汁。由于赌场使用多副牌,因此从数学上来说,同一手牌中最多 21 张 A 是可能的。

如何创建一个循环来遍历名为 Hand 的整数数组列表,其中包含与手中的牌相对应的整数。前任。一名玩家击中,现在有一张 A、一张 5、一张 2、一张 K,现在抽一张 A。代表他的手的数组列表是 [1, 10, 2, 5, 1]

我的想法:

public void calculatePlayerScore()
{
int counter = 0;
int aceCount = 0;

for (int i = 0; i < hand.size(); i++)
{
if (hand.get(i) != 1)
{
counter++;
}
else
{
aceCount++;
}
}

for (int i = 0; i < aceCount; i++)
{

//Now that we know the regular cards
//and we know how many aces there are
//we should be able to loop to find the most
//optimal score without busting, and pick the highest among those

}

如果有人对此有任何想法,请告诉我。非常感谢你的帮助。

最佳答案

请注意,一手牌中只有一张 A 可以算作 11。 (否则总数至少为 22。)

Calculate hand with all aces counting 1
if ( (hand total is <= 11) AND (there is at least one ace) )
hand total += 10 // one ace counts as 11

关于java - 二十一点游戏的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29324963/

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