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

Problems with my workspace

import java.io.Console;

public class Prompter { private Game mGame;

public Prompter(Game game) { mGame = game; }

public void play() { while (mGame.getRemainingTries() > 0 && !mGame.isSolved()) { displayProgress(); promptForGuess(); } if (mGame.isSolved()) { System.out.printf("Congratulations you won with %d tries remaining.\n", mGame.getRemainingTries()); } else { System.out.printf("Bummer the word was %s. :(\n", mGame.getAnswer()); }

public boolean promptForGuess() { Console console = System.console(); boolean isHit = false; boolean isValidGuess = false; while (! isValidGuess) { String guessAsString = console.readLine("Enter a letter: "); try { isHit = mGame.applyGuess(guessAsString); isValidGuess = true; } catch (IllegalArgumentException iae) { console.printf("%s. Please try again.\n", iae.getMessage()); } }
return isHit; }

public void displayProgress() { System.out.printf("You have %d tries left to solve: %s\n", mGame.getRemainingTries(), mGame.getCurrentProgress()); } }

1 Answer

Matthew, to get this to compile I had to make a few changes:

import java.io.Console;

public class Prompter {
   private Game mGame;

   public Prompter(Game game) {
      mGame = game;
   }

   public void play() {
      while (mGame.getRemainingTries() > 0 && !mGame.isSolved()) {
         displayProgress(); 
         promptForGuess();
      }

      if (mGame.isSolved()) {
         System.out.printf("Congratulations you won with %d tries remaining.\n", mGame.getRemainingTries());
      } else { 
         System.out.printf("Bummer the word was %s. :(\n", mGame.getAnswer());
      }
   }

   public boolean promptForGuess() {
      Console console = System.console();
      boolean isHit = false; 
      boolean isValidGuess = false;
      while (!isValidGuess) {
         String guessAsString = console.readLine("Enter a letter: ");
         try {
            isHit = mGame.applyGuess('a');  //you weren't passing in a char, which applyGuess() requires
                               //so I just hard-coded a char to get it to compile
            isValidGuess = true;
         } catch (IllegalArgumentException iae) {
            console.printf("%s. Please try again.\n", iae.getMessage());
         }
      }
      return isHit;
   }

   public void displayProgress() {
      System.out.printf("You have %d tries left to solve: %s\n", mGame.getRemainingTries(), mGame.getCurrentProgress());
   } 

}

And I had to add some methods to the Game class, methods you call in Prompter. Obviously the return values are ones I made up just to get it to compile. You'll need to change them as needed:

 public int getRemainingTries() {
   return 0;
 }

 public boolean isSolved() {
   return false;
 }

 public String getAnswer() {
   return "answer";
 }