私の計算は正しく機能していません。コードに問題はありません。スコアが正しく計算されない場合があります。時々それは完璧になります。いつ正しく行われるのか、いつ正しく行われないのかさえ理解できません。
スコアの計算は次のようになります。
エースは合計スコアに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;
}