【发布时间】:2018-03-14 15:47:04
【问题描述】:
我在二十一点游戏中的得分有问题。它可以找到正确的分数,但是当用户绘制一张新卡片时,它会错误地添加分数。
例如: 原始手牌是:4 和 5(所以得分 9) 用户抽到 10。 分数不是 19,而是 19+9 或 28。
这是我的代码: 计分方式:
public int getHandValue() {
boolean ace = false;
for (int i = 0; i < this.hand.size(); i++) {
if (this.hand.get(i).getRank().value > 10) {
points += 10;
} else if (this.hand.get(i).getRank().value == 1) {
ace = true;
} else {
points += this.hand.get(i).getRank().value;
}
if (ace == true && points + 11 <= 21) {
points += 11;
}
}
return points;
}
玩法:
public void play(Deck deck) {
boolean isDone = false;
if (this.getHandValue() > 21){
System.out.println("You have busted!");
isDone = true;
this.lose();
}
takeCard(deck.drawCard());
takeCard(deck.drawCard());
System.out.println("Here are your cards and your score:");
System.out.println(this.hand.toString());
System.out.println("Score: " + getHandValue());
ListItemInput hitOrPass = new ListItemInput();
hitOrPass.add("h", "hit");
hitOrPass.add("p", "pass");
while (!isDone){
System.out.println("Hit or pass?");
hitOrPass.run();
if (hitOrPass.getKey().equalsIgnoreCase("h")) {
String result = "";
this.takeCard(deck.drawCard());
result += "You hand is now " + this.hand.toString() + "\n";
result += "Your score is now " + this.getHandValue();
System.out.println(result);
} else {
System.out.println("You have chosen to pass.");
isDone = true;
}
}
}
【问题讨论】:
-
你的
points变量在哪里被初始化和声明?
标签: java