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
Azimjon Karim
3,757 Pointsreturn mGame.applyGuess(guess); -- Please, someone explain. Thanks!
return mGame.applyGuess(guess)
in this code (guess) takes an user input and passes it over to the applyGuess method. But what is mGame here for?
3 Answers
Azimjon Karim
3,757 PointsThanks for reply. The question belongs to this tutorial: https://teamtreehouse.com/library/java-objects/creating-the-mvp/prompting-for-guesses.
Sorry, this is my first question.
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsHey Azimjon Karim,
it seems like your question here is not linked to a challenge or video you are watching. Can you gives us more information on the code/video you are referring to?
Regards, Florian
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsAzimjon,
you can click on "Get Help" below a video or in a challenge to directly link your question to it. My answer to your question:
On creating a Game object you can choose what the answer of the Game will be. The Game object is able to receive a letter and check if this letter is part of the answer. If it is, the Game object will answer "Yes" (return true). If it is not, the Game object will answer "No" (return false).
On creating a Prompter object you have to tell it where to find the Game you have created. The Prompter stores this address in the mGame variable. Without an address to a Game object it cannot do anything. All the Prompter object is able to do, when being told so, is to ask a player for a letter and then forward this question to a Game object which will then look it up and either return true or false. It will pass the answer back to the the one who told him to question the Game object.
The Hangman class is storing instructions that you have told it (programmed into it). On executing the main method of the class it will follow these instructions which are in the following order:
- Create a Game object and give it an answer.
- Create a Prompter object and tell it where to find the created Game object.
- Tell the Prompter to ask a player for a guess. (The Prompter object is now asking the Game object if the guess of the player is right. To do so it looks up the address in the mGame variable. It returns true or false back to the Hangman class.)
- Check if the Prompter object returned true or false and print the result out to the player.
So without the mGame variable the Prompter object would not know where the Game object is and could not ask it if the guess was correct or not.
Games with different answers can be created as well as Prompters with addresses of different games.
Kind Regards, Florian