【问题标题】:Java blackjack scoring issueJava二十一点计分问题
【发布时间】: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


【解决方案1】:

每次调用你的方法时,你都在手上循环,所以你的积分应该在这样做之前重置。否则分数增加 2 倍 + 手中的额外牌。在你循环你的手之前重置值

public int getHandValue() {
    boolean ace = false;
    points = 0; //<--- reset the point total
    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;

【讨论】:

  • 非常感谢!
【解决方案2】:

我假设 points 是在此方法之外声明的。

由于您要返回积分,因此最好不要为此使用类范围的变量。你最终会得到这样的意想不到的结果。相反,在方法范围内使用变量,就像这样。

public int getHandValue() {
    boolean ace = false;
    int value = 0;

    for (int i = 0; i < this.hand.size(); i++) {
        if (this.hand.get(i).getRank().value > 10) {
            value += 10;
        } else if (this.hand.get(i).getRank().value == 1) {
            ace = true;
        } else {
            value += this.hand.get(i).getRank().value;
        }
        if (ace == true && points + 11 <= 21) {
            value += 11;
        }
    }

    return value;
}

【讨论】:

  • 是的,这就是我所做的,感谢您的帮助,我将从类变量更改为方法变量。
  • 是的,如果您要从方法返回类范围变量的值,而不是它的 getter,那么您可能有机会重构以使其更有效地工作更安全可靠的方式。
  • 我还建议对rank 的值使用getter。最终会是getRank().getValue()。马上开始做是个好习惯 - stackoverflow.com/questions/10407877/…
  • @JetsWest,如果其中任何一个解决了您的问题,请务必标记其中一个答案。
猜你喜欢
  • 2018-09-10
  • 2012-01-03
  • 2016-11-09
  • 2014-10-27
  • 2016-06-21
  • 2017-12-01
  • 2016-12-29
  • 2017-04-05
  • 2015-08-02
相关资源
最近更新 更多