如何计算二十一点游戏的得分?我哪里错了?

eMR*_*MRe 1 java processing

我的计算不正常.我看不出代码有什么问题.有时它不能正确计算得分.有时它完美无缺.我甚至不能理解它什么时候做得正确以及什么时候做得不好.

分数计算应该是这样的:

Ace可以将总分增加到1或11.如果分数高于21,则ace计算为1; 否则ace是11.

这是我的代码:

  // Updates the the value of the cards the player has in their hand
  int updateValueOfHand() {
    int result = 0;                             // it will be returned
    int ace = 0;                                // value of ace

    for (int i =0; i < playerHand.size(); i++)  // loop to see players hand
    {
      int cardValue;                            // card value of hand
      Card card=(Card)playerHand.get(i);        // check the card
      cardValue = card.getRank();
      if (cardValue == 1)                       // if card value is 1 (ace)
      {
        cardValue = 0;                         // assign to 0
        ace += 1;                              // ace is 1 (if there are 2 aces ace is 2 ...)
      }
      result = result + cardValue;             // result is card value (no ace)
    }
    //return result;
    println("Number of ace: " + ace);
    if (ace!=0)                                //if there is ace
    {
      for (int j=0; j<ace; j++)                // if there is more than 1 ace
      {
        if (result+11<=21) {                   // if result is <= 21 when you count ace as 11, then ace is 11
          result+=11;
        }        
        else {
          result+=1;                          // otherwise ace is 1
        }        
      }
    }
    return result;
  }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Joe*_*ley 6

考虑一只手拿一张King和两张A. 这应该计算为10 + 1 + 1,否则它将大于21.

但是,程序循环遍历每个Ace,并且:

// if result is <= 21 when you count ace as 11, then ace is 11
Run Code Online (Sandbox Code Playgroud)

由于King加上第一个计为11的Ace <= 21,程序选择将第一个Ace计为11,但这不正确.

这是一个想法来解决它:在你的第一个for循环中,result每个Ace 增加1,然后在你的第二个for循环中,result每个Ace 增加10,只要它保持<= 21.