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 keep track of how many wrong guesses have been made when attempting to solve the puzzle, and display the amount of tries remaining. We will wrap up the user story: As a guesser, I should know how many remaining tries I have left, so I am encouraged to be cautious.
Learn more
-
0:00
All right.
-
0:01
So our next ticket in order of priority is this, as a guesser I should
-
0:06
know how many remaining tries I have left so I am encouraged to be cautious.
-
0:11
So basically this implies that we're going to need to allow multiple guesses through
-
0:15
some sort of loop until they run out of tries.
-
0:18
Now in order to do that we'll have to set a limit.
-
0:22
That limit seems like something we want to make publicly available and
-
0:26
it's related to all games and it shouldn't change.
-
0:29
We know how to do that.
-
0:30
Let's make a constant.
-
0:32
Let's sketch out the remaining tries logic real quick.
-
0:36
So a standard hangman game allows you to miss seven times.
-
0:40
So let's look at a game in progress.
-
0:42
So a t and an h were a hit and z and w were incorrect guesses.
-
0:48
So therefore if we count how many misses they've made and subtract that
-
0:52
from the total amount of tries, we should know how many tries remain.
-
0:56
Here there are seven misses allowed and there have been two bad guess so
-
1:00
seven minus two is five.
-
1:02
So there are five tries remaining and if we guess why it appends the misses and
-
1:07
now there are three misses and 7- 3 = 4.
-
1:10
So there is four guesses left.
-
1:12
All right let's do this.
-
1:14
Okay so let's move our remaining tries take it into in progress.
-
1:21
All right, so let's go and set our guess limit.
-
1:26
This is definitely public and we want it to be true of all games.
-
1:31
So let's expose it off of the class.
-
1:33
So we'll make that static.
-
1:35
And we definitely don't want to change, right.
-
1:38
I don't want anybody cheating.
-
1:39
So that's final.
-
1:40
It's an integer and because it's a constant,
-
1:42
let's make the name all capitals.
-
1:45
So we'll do max under misses.
-
1:48
So like we said seven is the standard number of tries.
-
1:52
Now let's write some code that exposes how many tries are left.
-
1:56
I'm gonna start Jshell up down here.
-
1:57
So again that code was max misses allowed minus how many misses they have made.
-
2:05
But, how do we count the amount of misses?
-
2:08
So, because it is a string
-
2:09
we can use a new method that you might not have seen before.
-
2:11
So we'll say string.example = 'hello'.
-
2:19
You want .example.
-
2:27
So now if we do example.link, we'll see that there were 5.
-
2:33
So that will work because we'll know how many misses are there because our string
-
2:37
will be that long.
-
2:39
Cool.
-
2:39
So in the game object let's go ahead and make a new computed property right.
-
2:45
So we want to be public because we want people to use it.
-
2:49
And it's going to return a number and
-
2:52
let's call it getRemainingTtries and this lines up with the user story.
-
2:56
And so what do we wanna do?
-
2:58
We wanna return here,
-
2:59
we wanna return that max misses that we just made, that's how many were there,
-
3:04
minus the misses and because it's a string we'll just use that length property there.
-
3:09
Cool.
-
3:10
Okay so now that we have it, let's make our display progress get upgraded.
-
3:15
Let's put it in there.
-
3:17
So let's say you have %d.
-
3:22
And now %d is where you wanna show numbers in a string formatter.
-
3:27
I put some more in the teacher's notes about this.
-
3:29
But, you want %D, and D stands for decimal there.
-
3:32
So, you have %D tries left to solve and
-
3:38
then here we go and then %S will still stay there.
-
3:41
So, if there's two string formatters, the first will say game.getRemainingTries and
-
3:48
we'll call that into a comma.
-
3:49
There we go.
-
3:50
So now it will push that remaining tries count that we have in there.
-
3:54
So now let's build a loop that will just keep asking
-
3:58
while there are remaining attempts.
-
4:01
Now the specific story doesn't talk about what to do with a win.
-
4:06
So let's not handle that case yet.
-
4:08
Let's let the next person who picks up the next story deal with that.
-
4:11
Now this is a good development practice.
-
4:13
Limit your scope to the story that you're working on.
-
4:15
This way you don't have to run into other developers who might pick up the next
-
4:18
ticket and implement things differently than you might have.
-
4:22
Let's see what does that code look like.
-
4:23
Let's go ahead and let's get rid of this testing code that we're doing here before.
-
4:29
I gonna get rid of story aim and story in the variable.
-
4:32
So we have these two things and we wanna loop.
-
4:34
So what can we say, we said while we've got remaining tries,
-
4:41
so while game.getRemainingTries is greater than zero.
-
4:49
Right.
-
4:49
As long as there is more than zero tries left you can still keep playing.
-
4:53
So we'll go ahead and
-
4:57
move these in here.
-
5:08
Here we go, that should do it.
-
5:09
Let's bring this up, let's hit CTRL D, and let's see if it's here.
-
5:17
It's not, okay.
-
5:17
So, we'll say clear & javaC
-
5:22
Hangman.java & java hangman.
-
5:28
Okay here we go.
-
5:31
Awesome so it says we have seven tries left to solve which is right.
-
5:34
And let's see, let's guess T.
-
5:37
Cool so it didn't go down because we didn't miss but let's miss.
-
5:40
And then let's miss again.
-
5:42
And let's miss again, and see they're going down and it's right, so
-
5:46
now if we guess it should stay at four, it's that.
-
5:49
I just can't get this one.
-
5:50
What is it?
-
5:51
It's a Q, it's probably a Q.
-
5:52
You always guess Q, right?
-
5:54
So, uh-oh, oop there you go.
-
5:59
So it ran out.
-
5:59
So the loop finish when we're done.
-
6:01
I think we can go ahead and consider this one done-zo.
-
6:05
Hey, it's starting to look like a game and we're so close, only two stories left.
-
6:12
A good lesson that we learned here is that by only completing the task as required by
-
6:17
the story we had a good stopping point.
-
6:19
In case project scope or requirements for our NVP changed.
-
6:22
We're still on target, but
-
6:24
we could in fact bring in more people if we hit a time crunch.
-
6:27
Breaking down stories in the sane technical tasks takes a certain amount of
-
6:30
effort and finesse.
-
6:31
But it almost always pays off in the end.
-
6:34
Speaking of a stopping point, you've done an awful lot in this stage and
-
6:38
I think we should wrap this one up.
-
6:39
We've got an app-ish looking app and you deserve to take a break.
-
6:42
Give yourself a huge pat on the back.
-
6:44
You're mastering using Java objects and picking up some more core Java skills.
-
6:48
You're doing great.
-
6:50
Way to stick with it.
-
6:51
I've thrown a lot of information at you and you're processing it.
-
6:54
I am super impressed.
-
6:55
Of course though, you don't need me to tell you all that, because you've been
-
6:59
proving it yourself by rocking these exercises I keep throwing at you.
-
7:02
Like this final one for this stage.
You need to sign up for Treehouse in order to download course files.
Sign up