Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Creating the MVP Remaining Tries

Remaining Tries not Decreasing

It stays at 7 tries. Tried all I could think of to make it work.

class Game{ public static final int MAX_MISSES = 7; private String answer; private String hits; private String misses;

public Game(String answer){ this.answer = answer; hits = ""; misses = "";

} boolean applyGuess(char letter){ boolean isHit = answer.indexOf(letter) != -1; if (isHit){ hits += letter; }else{ hits += misses; } return isHit; }

public int getRemainingTries(){ return MAX_MISSES - misses.length(); }

public String getCurrentProgress(){ String progress = ""; for (char letter : answer.toCharArray()){ char display = '-'; if (hits.indexOf(letter) != -1){ display = letter; } progress += display; } return progress; }

}

public class Hangman {

public static void main(String[] args) { // Your incredible code goes here...

Game game = new Game("treehouse");
Prompter prompter = new Prompter(game);
while (game.getRemainingTries() > 0){
    prompter.displayProgress();
    prompter.promptForGuess();    
}

} }

4 Answers

Steven Parker
Steven Parker
231,275 Points

The formatted code looks much better! But after the change, both conditions are doing the same thing. That's still not quite what I was suggesting:

    if (isHit){
      hits += letter;
    }else{
      hits += letter;  // should this be "misses += letter;" instead?
    }
Steven Parker
Steven Parker
231,275 Points

For "getRemainingTries()" to decrease, you must add to "misses"; but that doesn't seem to be done anywhere.

Perhaps the issue is in this section of the "applyGuess" method:

  if (isHit) {
    hits += letter; 
  } else {
    hits += misses;  // should this have been "misses += letter;" instead?
  }

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Thanks for the suggestion on formatting my code. I never knew how to do it until now. Your suggestion above I believe is in my code.

class Game{
  public static final int MAX_MISSES = 7;
  private String answer;
  private String hits;
  private String misses;

  public Game(String answer){
  this.answer = answer;
  hits = "";
  misses = "";

  }
  boolean applyGuess(char letter){
    boolean isHit = answer.indexOf(letter) != -1;
    if (isHit){
      hits += letter;
    }else{
      hits += letter;
    }
    return isHit;
  }

  public int getRemainingTries(){
    return MAX_MISSES - misses.length();
  }

  public String getCurrentProgress(){
    String progress = "";
    for (char letter : answer.toCharArray()){
      char display = '-';
      if (hits.indexOf(letter) != -1){
        display = letter;
      }
      progress += display;
    }
    return progress;
  }

}

I just saw that and fixed it. Thanks greatly appreciated.