1

私の計算は正しく機能していません。コードに問題はありません。スコアが正しく計算されない場合があります。時々それは完璧になります。いつ正しく行われるのか、いつ正しく行われないのかさえ理解できません。

スコアの計算は次のようになります。

エースは合計スコアに1または11のいずれかを追加できます。スコアが21より大きい場合、エースは1として計算されます。それ以外の場合、エースは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;
  }

ここに画像の説明を入力してください

4

5 に答える 5

6

1つのキングと2つのエースを持つハンドを考えてみましょう。これは10+1 + 1として計算する必要があります。そうしないと、21より大きくなるためです。

ただし、プログラムは各エースをループし、次のようになります。

// if result is <= 21 when you count ace as 11, then ace is 11

キングと11としてカウントされる最初のエースは21未満であるため、プログラムは最初のエースを11としてカウントすることを選択しますが、これは正しくありません。

これを修正するための1つのアイデアがあります。最初のforループでは、resultエースごとに1ずつ増やし、次に2番目のforループでは、resultエースごとに10ずつ増やします(21未満)。

于 2012-12-24T05:20:59.553 に答える
2

ブラックジャックでエースに対処するためのエレガントな方法はありますか?

各エースを11として扱います。値が21を超える場合は、手にあるエースを通過してforeachし、スコアが21以下になるか、各エースを通過するまで、合計から10を引きます。これが最終スコアになります。

于 2013-11-24T21:48:27.980 に答える
2

私にとってこれを行う最良の方法は、合計2つを取得することです。

  • ハンドの合計(エースは11を考慮)
  • ハンドのエースの総数。

次に、hand_total> 21の場合、<= 21になるまですべてのエースのhand_totalから10を引き
ます。バストを防ぐために1である必要がない限り、エースは常に11としてカウントされます。

于 2014-02-16T20:00:20.070 に答える
2

これが私の試みです

public int handScore(Hand hand)
{
    int score = 0;
    int aceCount = 0;
    for (Card card : hand.getCards())
    {
        switch (Card.RANK_SYMBOLS[card.getRank()])
        {
            case "2": score += 2; break; 
            case "3": score += 3; break;
            case "4": score += 4; break;
            case "5": score += 5; break;
            case "6": score += 6; break;
            case "7": score += 7; break;
            case "8": score += 8; break;
            case "9": score += 9; break;
            case "10":
            case "j":
            case "q":
            case "k": score += 10; break;
            case "a": score += 11; aceCount++; break;
        }

        while(score>21 && (aceCount-->=0))
            score -= 10;

    }
    return score; 
}
于 2017-08-30T22:55:21.660 に答える
0

これが私が使用している.NETコードです。Handは私のカードの特殊なコレクションです...とにかく、基本的な考え方は理解でき、構文と命名規則をJavaに簡単に変換できるはずです。

protected int EvaluateHand(Hand hand)
    {
        int score = 0;
        foreach (Card currentCard in hand)
        {
            switch (currentCard.Value)
            {
                case Value.Two:
                    score += 2;
                    break; 
                case Value.Three:
                    score += 3;
                    break;
                case Value.Four:
                    score += 4;
                    break;
                case Value.Five:
                    score += 5;
                    break;
                case Value.Six:
                    score += 6;
                    break;
                case Value.Seven:
                    score += 7;
                    break;
                case Value.Eight:
                    score += 8;
                    break;
                case Value.Nine:
                    score += 9;
                    break;
                case Value.Ten:
                case Value.Jack:
                case Value.Queen:
                case Value.King:
                    score += 10;
                    break;
                case Value.Ace:
                    score += 11;
                    break; 

            }

        }

        // after evaluating with 11 for each ace, if score has busted, then change each ace value from 11 to 1
        if (score > 21)
        {   // if our evaluated score is over 21, subtract 10 foreach ace in the hand. 
            foreach (Card currentAceCheckCard in hand)
            {
                if (score <= 21)
                {   // if we've subtracted enough until we're not busted, then break and return value
                    break;
                }
                if (currentAceCheckCard.Value == Value.Ace)
                {
                    score -= 10;
                }
            }
        }
        return score; 
    } 
于 2013-11-24T22:00:26.840 に答える