Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We will use loops and chaining to solve the final story: As a guesser, I should be able to know when the game is solved or failed, so that I can acknowledge completion with celebration or tears.
-
0:00
Here it comes, the last story to complete our minimum viable product.
-
0:04
If you thought it felt good to move the stories to the Done column,
-
0:07
wait until you see what it feels like to move the last one.
-
0:10
So here it is.
-
0:12
As a guesser, I should be able to know when the game is won or lost so
-
0:16
that I can acknowledge completion with celebration or tears.
-
0:19
All right, we can definitely do this.
-
0:21
So we should provide a method that returns whether or
-
0:23
not the game [SOUND] was completed successfully.
-
0:25
So how do we do that?
-
0:27
Well, one way to do it is just to check and see if there's any blanks left.
-
0:32
We should just start with this approach.
-
0:33
[SOUND] If there is a blank, the game has not been won.
-
0:35
And then we should have the prompter display the final outcome,
-
0:39
spreading the good or bad news to our users.
-
0:41
Here we go.
-
0:44
All right, let's move our final ticket over to the In Progress column.
-
0:48
We need to let people know if they won or not.
-
0:51
So, let's go to Game really quick,
-
0:54
let's stop our program from running, and let's get in here.
-
0:59
Let's say, we need to let the game know if it's won or not.
-
1:05
So let's just go ahead and let's put it here.
-
1:08
Let's say, public and its true and false, right?
-
1:12
So boolean and isWon, and remember is, is the,
-
1:16
like the getter, it's a computed getter, right?
-
1:21
So, we're gonna say isWon, where normally if it was not boolean,
-
1:24
it would have been get for the stringer and integer or something.
-
1:27
But it's is because that's the format of the reason.
-
1:30
And we're going to calculate our value, so let's just use that
-
1:34
existing method like we talked about, so as I getCurrentProgress.
-
1:39
Now, I could store this in a variable,
-
1:42
but I know that this is going to return a string.
-
1:46
And so that string has a method on it that allows us to see if a character exists.
-
1:52
What was that called again, indexOf.
-
1:54
So, what I can do is I can do a .indexOf, right?
-
1:59
So it's method chaining.
-
2:00
And we wanna see if there is in fact, any of those dashes in there.
-
2:05
It's probably a good place to make a constant,
-
2:08
if we wanted to make this a little bit better.
-
2:11
And then we want to check and see if it was, in fact, not found.
-
2:18
So we're gonna say, is it not found?
-
2:20
If that's not found, they won.
-
2:22
That's the logic theory.
-
2:23
Cool.
-
2:24
So this returns a string.
-
2:26
And then that string is checked to see if it's dashes.
-
2:29
And then we see, if there are any dashes in there.
-
2:33
Then they have, in fact, not won, cuz their stuff left to guess.
-
2:37
Okay, so let's go use it.
-
2:38
In our main program here, in Hangman.java.
-
2:41
We want the game to stop looping once it's been won.
-
2:47
Now this logic might be a little challenging here, but
-
2:50
let's think it through.
-
2:51
So currently the loop goes as long as there are remaining tries.
-
2:55
So we wanna add another condition, and it really is this.
-
2:57
As long as the game hasn't been won.
-
3:00
Okay, so while there are remaining tries,
-
3:05
and we just wrote that game is won, but
-
3:10
what we want is not won.
-
3:13
It's so hard to not do a bar impersonation here, not.
-
3:18
Sorry, so that's what we want.
-
3:21
Where both of these are true, keep going, right?
-
3:24
So we wanna keep going where both of these are true.
-
3:28
When this is checked, it will keep going and each one of these will go.
-
3:31
So when it's all done, we wanna display the outcome, right?
-
3:35
So, we would do something like this, right?
-
3:40
So it's a prompter.displayOutcome.
-
3:43
Now, we haven't written that method yet, but I bet you could.
-
3:48
Why don't you go ahead and pause me?
-
3:51
What we're gonna do before you pause, let's get the instructions.
-
3:55
What we're gonna do here is we're gonna try to show if they've won,
-
3:58
we're gonna say, congratulations.
-
3:59
And if they failed, we're gonna say, sorry, here's what the answer was.
-
4:03
So why don't you go write that method?
-
4:05
And notice, it doesn't return anything.
-
4:08
Okay, go ahead, pause me and come back when you're ready.
-
4:12
Cool, this is how I did it.
-
4:16
So, under displayProgress here, I added a new method.
-
4:19
And of course, it doesn't return anything.
-
4:21
So that's a void.
-
4:22
Display outcome.
-
4:28
All right, so if the game was won, we wanna say, congratulations.
-
4:31
So let's say, if game is won.
-
4:33
I'm gonna close my if and I'm gonna say, else and
-
4:36
then that's where we're gonna put this, add here.
-
4:39
So here we go.
-
4:41
So, we want to say system.out.printf.
-
4:51
Congratulations, you won with, let's go ahead and
-
4:55
say how many tries they had remaining.
-
4:58
With %d tries remaining.
-
5:01
Again, we'll extra feature there.
-
5:03
Wasn't asked for.
-
5:04
Maybe we shouldn't do that.
-
5:06
We'll see what they think of the demo.
-
5:09
All right, so you won with that many tries remaining,
-
5:15
and we're going to say, game.getRemainingTries.
-
5:20
Otherwise, we're
-
5:25
going to .out.printf,
-
5:31
Bummer, the word was %s.
-
5:37
And I'm gonna make a sad face there, we'll say, %n.
-
5:42
Okay, and then we need to get access to the answer, don't we?
-
5:47
Prompter doesn't have access to the answer.
-
5:49
So I'm gonna write a getAnswer method over here, sounds a little tricky wasn't it?
-
5:54
So over here in Game, I'm gonna make a getter for getAnswer, right?
-
5:59
So our answer is still private but we can add a getter,
-
6:04
want to add a getter up here at the top.
-
6:06
So we'll say our public,
-
6:11
we want this to be public.
-
6:15
And it's a string and we wanna say,
-
6:17
getAnswer, and it doesn't expect anything, and we are going to return the answer.
-
6:21
Cool, let's see how I did.
-
6:23
I'm sure you did great, clear && javac
-
6:29
Hangman.java && java Hangman.
-
6:36
Let's test it, T-R-E-H-O-U-S.
-
6:42
We did it with seven tries remaining, awesome.
-
6:45
Okay, let's try to fail really quick.
-
6:46
A-B-C-D-E-F-G-I.
-
6:54
Bummer, the word history was treehouse, sad face.
-
6:57
My gosh, we did it, the sprint is over.
-
7:03
[APPLAUSE] >> Wohoo, we just did it.
-
7:07
We got a working Hangman MVP.
-
7:09
And what a great job of separating the prompting from the game logic.
-
7:13
Now, we could do a web-based version of this, or use it in an Android app, or
-
7:16
play it on a WiFi enabled refrigerator, you get the point.
-
7:20
The other great thing about this game class that we created is that
-
7:23
the implementation details are hidden.
-
7:25
We absolutely could make this game more efficient as long as we kept up our end
-
7:29
of the deal, and we kept the interface, or the exposed methods and functionality.
-
7:33
If we kept those the same, every single one of the users of this class,
-
7:36
the console, the website, the Android app, the refrigerator, they'd all benefit.
-
7:40
Also, because you did a great job of naming your methods, almost any English
-
7:45
speaker could pick up that class and know exactly what it's expected to do without
-
7:49
ever seeing the methods code, just like that radio example we talked about.
-
7:54
Great job harnessing the power of objects.
-
7:57
One more thing I told you that I'd show you was how to pass an arguments to
-
8:00
the command line.
-
8:01
I mean after all, if you're gonna play this game,
-
8:03
you probably want to eventually use a different word than treehouse, right?
-
8:07
First though, an exercise.
You need to sign up for Treehouse in order to download course files.
Sign up