Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Java Objects - Word Guessing Game!
You have completed Practice Java Objects - Word Guessing Game!
Preview
We will use loops and chaining to solve the final story.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So here's our final user story
to complete the minimum viable product.
0:00
As a guesser, I should be able to know
when the game is won or lost
0:05
so that I can acknowledge completion
with celebration or tears.
0:09
Alright, so we can definitely do this.
0:13
So we should provide a method
0:16
that returns whether or not
the game was completed successfully.
0:17
So how do we do that?
0:21
Well, one way to do it is to just check
and see if there's any blanks left.
0:22
We should just start with this approach.
0:27
If there is a blank,
the game has not been won.
0:29
And then we should have the prompter
0:31
display the final outcome, spreading
the good or bad news to our users.
0:33
Here we go.
0:37
Let's open
the Game class and add a new method
0:39
that checks if the game has been won.
0:41
We need a public,
0:46
boolean, and we'll call it isWon.
0:48
Then we want to check
0:54
if there are any empty places
left in the current progress.
0:55
So I'll return the result
of our get current progress
0:59
And use index of
to see if there's still an underscore left.
1:02
Return Get current progress
1:07
Index of
1:13
And I'll add in the underscore
1:16
and equals negative one
1:19
So this time, if it finds an underscore
in our current progress,
1:23
this condition will evaluate to false,
because it'll be a zero or higher
1:26
number, right?
1:30
Okay, now we need to update our main game
loop to stop when the game is won.
1:32
Let's open up WordGuessor.java and modify
the while condition.
1:36
Currently, it only checks
if there are remaining tries,
1:41
but we also need to check
if the game hasn't been won yet.
1:44
So I'll tag on an and here
and call our new isOne method on the game.
1:47
Remember,
we want it to go as long as the game is
1:52
not won,
so we'll add the not operator in front.
1:54
So if this returns false.
1:58
So now the loop will continue
while both conditions are true.
2:00
There are tries remaining,
and the game has not been won.
2:04
Cool.
2:07
After the loop ends,
we need to tell the player what happened.
2:09
I'll add a call to display the outcome.
2:12
Promptor.displayOutcome
We haven't actually created that
2:14
method yet,
so let's move over to the prompter class.
2:19
And I'll just add it down here
at the bottom.
2:24
public
2:28
void displayOutcome
2:30
First in order to display the outcome
we need to check if they won or lost right
2:33
So let call our new IsWon
method again here here.
2:39
If this returns true,
2:52
we'll congratulate the player
with a system.out.printf
2:53
saying congratulations
you won with and I'll interpolate
2:58
the remaining tries here so %d tries
3:02
remaining.
3:06
and then call the getRemainingTries
3:12
method for the second argument.
3:14
If the game was not won, I reveal
the answer they were trying to guess.
3:21
Bummer.
3:26
The word was...
3:27
%s with a sad face.
3:28
And pass in the answer with getAnswer.
3:37
And wait a second,
I think there's a problem.
3:40
The prompter class
doesn't have any access to the answer yet.
3:43
We need to add a getter method
in the game class so the prompter
3:47
can access it
3:50
So it be a public getter
that returns a string
3:53
We'll call it getAnswer and simply return
3:58
answer.
4:01
Great!
4:08
Now let's test our completed game.
4:09
I'll compile and run it.
4:11
Let me try to win
by guessing all the letters in Treehouse.
4:15
T-R-E-H-O-U-S.
4:19
Perfect!
4:27
Congratulations,
you won with seven tries remaining.
4:28
Wow, I am good at this.
4:31
Okay, now let me test
losing by making wrong guesses.
4:34
I'll start a new game,
and let me guess some wrong letters.
4:37
A, B, C, D,
4:41
F, G, and I.
4:45
Bummer, the word was treehouse.
4:49
Ugh, that was going to be my next guess,
I swear.
4:51
Excellent.
4:55
Our word guesser game now properly
detects wins
4:56
and loses, and gives clear
feedback to the player.
4:58
The game loop stops at the right time
whether you win or run out of tries.
5:01
Nice work, everyone.
5:06
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up