我真的希望我能在这样的问题上解决这个问题。无论如何,我正在使用java制作一个二十一点程序,并且我试图处理如何使用ACE时遇到错误。对于那些不了解游戏的人来说,ACE可以算作11或1,并且为了这个程序我将自动计为11,除非你破产(你的分数超过21)。我的问题是我有3种方法:
public int getPlayerScore() {
int pScore = 0;
for (int i = 0; i < playerHand.size(); i++) {
pScore = pScore + getCardScore(playerHand.get(i), "P");
}
return pScore;
public int getCardScore(Card c, String s) {
int cardScore = 0;
// gets the score of the card based on value
switch (c.getValue()) {
case TWO:
cardScore = 2;
break;
case THREE:
cardScore = 3;
break;
case FOUR:
cardScore = 4;
break;
case FIVE:
cardScore = 5;
break;
case SIX:
cardScore = 6;
break;
case SEVEN:
cardScore = 7;
break;
case EIGHT:
cardScore = 8;
break;
case NINE:
cardScore = 9;
break;
case TEN:
cardScore = 10;
break;
case JACK:
cardScore = 10;
break;
case QUEEN:
cardScore = 10;
break;
case KING:
cardScore = 10;
break;
case ACE:
cardScore = getAceScore(s);
break;
}
return cardScore;
}
public int getAceScore(String s) {
int aceScore = 0;
int tempScore = 0;
if(s.equals("P")){
tempScore = getPlayerScore() + 11;
}
else{
tempScore = getDealerScore() + 11;
}
// if an ace as 11 doesn't bust player
if (tempScore <= 21) {
aceScore = 11;
}
// if an ace as 11 busts player
else if (tempScore >= 21) {
aceScore = 1;
}
return aceScore;
}
我知道为什么我收到堆栈溢出错误,我的getAceScore()
正在调用getPlayerScore()
,而getAceScore()
正在调用int playerScore
等。我尝试了一个getCardScore
全局变量,在必要时进行更新,问题是它会更新并在不应该更改ACE分数时更新。例如,如果我得到ACE,NINE得分为20,则表示我的得分为10,因为它会说ACE是11,那么我的得分是31.我正在考虑另外一种方法与{{1从该方法中移除ACE案例,然后不断检查玩家手中的ACE,然后调用getAceScore
。这似乎太过分了。任何人都可以提供更简单的解决方案吗?
答案 0 :(得分:1)
摆脱递归,你会没事的。 getAceScore方法可以将玩家的得分作为参数而不用Ace。然后它将根据11是否会破坏他而返回1或11
public int getAceScore( int playerScoreWithoutAce ) {
int aceScore = ( playerScoreWithoutAce + 11 <= 21 ) ? 11 : 1;
return aceScore;
}
我认为无论如何我会以不同的方式对整个解决方案进行建模。你应该只有Player对象,并避免整个“如果经销商/如果玩家”的区别。 Player对象在某个时刻有一个计算他手牌得分的方法。这是确定Ace
值的方法public int handScore( List<Card> cards ) {
int handScore = 0;
// This assumes cards come in sorted with Ace last
for( Card card : cards ) {
int cardValue = ( card == ACE ) ? aceValue( handScore ) ? card.getValue();
handScore += cardValue;
}
return handScore;
}
private int aceValue( int handScore ) {
return ( handScore + 11 <= 21 ) ? 11 : 1;
}
答案 1 :(得分:0)
将当前得分(玩家或经销商)传递到getCardScore(),然后传递到getAceScore()方法。你应该能够完全消除P业务的String参数。
答案 2 :(得分:0)
您可以处理枚举类中的所有逻辑。当然,CardRank
将是Card
的属性。
<强>输出:强>
Ace 1: 12
Ace 11: 21
<强>代码:强>
public enum CardRank {
ACE(1),
KING(10),
QUEEN(10),
JACK(10),
TEN(10),
NINE(9),
EIGHT(8),
SEVEN(7),
SIX(6),
FIVE(5),
FOUR(4),
THREE(3),
TWO(2);
private int value;
private CardRank(int value) {
this.value = value;
}
public static int caculateScore(int currentScore, CardRank rank) {
if (currentScore <= 10 && rank == CardRank.ACE) {
return currentScore + 11;
}
return currentScore + rank.value;
}
@Override
public String toString() {
return String.format("Rank: %s, Value: %d", name(), value);
}
public static void main(String[] args) {
System.out.printf("Ace 1: %d%n", CardRank.caculateScore(11, CardRank.ACE));
System.out.printf("Ace 11: %d%n", CardRank.caculateScore(10, CardRank.ACE));
}
}