我的计算不正常。我看不出代码有什么问题。有时它不能正确计算得分。有时它完美无缺。我甚至不能理解它什么时候做得正确以及什么时候做不好。
分数计算应该是这样的:
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;
}
答案 0 :(得分:6)
考虑一手牌有一张King和两张A.这应该计算为10 + 1 + 1,否则它将大于21。
然而,程序循环遍历每个Ace,并且:
// if result is <= 21 when you count ace as 11, then ace is 11
由于King加上第一个计为11的Ace是&lt; = 21,程序选择将第一个Ace计为11,但这不正确。
以下是修改它的一个想法:在您的第一个for
循环中,为每个Ace增加result
1,然后在第二个for
循环中增加result
只要它保持&lt; = 21,每个Ace就会增加10个。
答案 1 :(得分:1)
这是我的尝试
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;
}
答案 2 :(得分:0)
Is there an elegant way to deal with the Ace in Blackjack?
只需将每个ace都视为11.如果你的值超过21,那么你手中的ace,并从你的总数中减去10,直到你的得分为21或更低,或者你已经通过每个ace。这将是你的最终得分。
答案 3 :(得分: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;
}
答案 4 :(得分:0)
对我来说,最好的办法是获得2个总数:
然后如果hand_total&gt; 21从每个ace的hand_total中减去10直到&lt; = 21
一个ace应该总是被计为11,除非它需要为1以防止摔伤。