Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP Current Progress

Alexander Nowak
Alexander Nowak
498 Points

Taking guesses code

Hi,

Could someone please explain this line of code to me?

System.out.printf("Try to solve: %s/n", mGame.getCurrentProgress());

In particular the 'mGame.getCurrentProgres' part.

I get the the getCurrentProgress allows us to use all the code we just wrote in the Game class, but why is is mGame,getCurrentProgress ?

Thanks

3 Answers

deckey
deckey
14,629 Points

Hi, take a look at the main method in Hangman.class

Game game = new Game ("treehouse");  // creates a game object, with a secret 'treehouse' word created 
Prompter prompter = new Prompter(game); // creates a Prompter instance which requires a Game instance from above

// inside the Prompter constructor, we create mGame property which is the 'game' we passed as an argument
// later on that mGame can use all methods from Game class, like getCurrentProgress()

hope it helps

deckey
deckey
14,629 Points

Hello Alexander, getCurrentProgress() method is mainly used to visually present the current game status to the player. As you can see, most of the code is printing either "-" or characters depending on if the user made a correct guess or not. I've added comments to method lines, hope it helps

public String getCurrentProgress(){
    String progress = ""; // start with an empty string
    for (char letter:mAnswer.toCharArray()){   // for each letter in the secret word ... 
        char display = '-'; // we replace each character with a hyphen, so the user can see number of letters
        if(mHits.indexOf(letter) >= 0){ // this means that we have a hit, and in the next line ... 
            display = letter; // instead of hyphen we print actual letter
        }
        progress+=display; // we add either hyphens or letters to final display
    }
     return progress;
}

Good luck

Alexander Nowak
Alexander Nowak
498 Points

Thanks for that!

but why in the Prompter class do we use mGame.getCurrentProgress().

Why is there any need for the mGame part?

Also what does the return progress; code do? where does it return it to?

Thanks

deckey
deckey
14,629 Points

As for the return of the progress,

public String getCurrentProgress(){
     return progress;
}

we declared the method to 'return' a String, so later we can use it, like Craig is using it in 'displayProgress()' method,

good luck!