0

I am having an issue with my scoring in my blackjack game. It works in finding the right score, but when the user draws a new card it will incorrectly add the score.

For example: Orginial hand is : 4 and 5 (so score 9) User draws a 10. Instead of score being 19 is will instaed be 19+9 or 28.

Here is my code: Scoring method:

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;
}

Play method:

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;
        }
    }
}
JetsWest
  • 21
  • 2

2 Answers2

1

You loop over the hand each time you call your method so your points should reset before doing so. Otherwise the points increase by 2x + the extra card in the hand. Reset the value before you loop your hand

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;
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0

I assume points is being declared outside of this method.

Since you are returning points it is best not to use a class-wide variable for this. You'll end up with unexpected results like this. Instead, use variable within the method scope, like this.

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;
}
adprocas
  • 1,863
  • 1
  • 14
  • 31
  • Yes that is what I did, thanks for the help i will change from class variable to method variable. – JetsWest Mar 14 '18 at 15:54
  • Yeah, if you're returning the value of a class wide variable from a method, other than its getter, then you'll likely have an opportunity to refactor to make it work more efficiently and in a more secure and reliable manner. – adprocas Mar 14 '18 at 15:55
  • I would also suggest using getters for the value of the `rank` as well. It would end up being `getRank().getValue()`. It's a good practice to start doing right away - https://stackoverflow.com/questions/10407877/what-is-the-point-of-getters-and-setters – adprocas Mar 14 '18 at 15:57
  • @JetsWest, be sure to mark one of the answers if either of them solved your issue. – adprocas Mar 14 '18 at 16:45