二十一点游戏的逻辑

Logic for Blackjack Game

我正在编写一个 21 点游戏,我已经玩得很远了。然而,我只是到了将每手牌后的分数相加的地步(我认为这很容易),但事实证明,A 不断地绞尽脑汁。由于赌场玩的是多副牌,因此从数学上讲,同一手牌可能 最多获得 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

    }

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

对您的 "Hand" ArrayList 求和,对于您看到 "Ace" 的任何位置,以 1 或 11 作为加法来计算该总和。如果你的点数大于 21,你就爆牌了。如果没有,继续。如果恰好是 21,则将那只手加到您的成功数中,然后停止击球。

解决这个问题的暴力方法是实现一个"look-ahead"函数,你可以看一遍你的整手牌,然后计算你手牌提供的所有可能组合,包括你手牌中的A作为 1 或 11。生成此可能性列表后,您可以查看哪种可能性具有最少数量的牌来创建二十一点或最高手牌价值,然后选择该可能性。这是一个常见的算法问题,可能有非常有效的解决方案供您查看。

请注意,一手牌中只有一张 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

只有一张 A 可以是 11。一手牌的总和如下所示:

public int calculateScore(Hand hand) {
    int total = 0;
    bool hasAce = false;

    for (int i = 0; i < hand.size(); i++) {
        int r = hand.get(i);
        total += r;

        if (1 == r) { hasAce = true; }
    }
    if (hasAce && total < 12) { total += 10; }
    return total;
}

在真正的二十一点游戏中,您可能还想 return 手牌总数是软还是硬的事实。

如果手牌总数超过21,但手牌中有一张=11,则使11 = 1。