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 keep track of how many wrong guesses have been made when attempting to solve the puzzle and display the amount of tries remaining.
Resources
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
Alright, so our next ticket is this.
0:01
As a guesser,
I should know how many remaining tries
0:03
I have left,
so I am encouraged to be cautious.
0:06
I'll move this ticket
right into the In Progress column.
0:09
So basically, this implies
that we're going to need to allow
0:14
multiple guesses through some sort of loop
until they run out of tries.
0:17
Now in order to do
that, we'll have to set a limit.
0:21
That limit is something
we want to make publicly available
0:24
and it's related to all games,
and shouldn't change.
0:27
We know how to do that.
0:30
Let's make a constant.
0:31
Let's sketch out the remaining tries
logic real quick.
0:34
So a standard game like this
typically allows you to miss seven times.
0:37
So let's look at a game in progress.
0:40
So a T and an H were a hit, and a Z and a
W were incorrect guesses.
0:43
So therefore, if we count
how many misses they've made and subtract
0:48
that from the total amount of tries,
we should know how many tries remain.
0:51
Here there are seven misses allowed,
and there have been two bad guesses,
0:55
so 7 minus 2 is 5.
0:59
So there are five tries remaining.
1:02
And if we guess Y,
and it appends to the misses,
1:04
and now there are three misses,
and 7 minus 3 equals 4,
1:07
so there are four guesses left.
1:11
Alright, let's do this.
1:14
Alright, so let's go and
set up our guess limit.
1:16
This is definitely public
and we want it to be true of all games
1:19
so let's expose it off the class.
So we'll make it static
1:23
And we definitely don't
want it to change, right?
1:27
We don't want anybody cheating,
so it's final.
1:29
It's an integer,
and because it's a constant,
1:33
let's make the name all capitals.
1:35
So we'll do max underscore misses.
1:37
Seven is the standard number of tries.
1:40
Now let's write some code that exposes
how many tries are left.
1:44
The calculation is max misses minus
the number of misses they have made.
1:47
But how do we count the amount of misses?
1:52
Since our misses variable is a string,
we can use the length method to count
1:55
how many characters are in it.
1:59
Each character represents
one missed guess.
2:00
So in the game object, let's go ahead
and make a new computed property.
2:03
So we want it to be public
because we want people to use it.
2:08
And it's going to return a number.
2:11
And let's call it get remaining tries.
2:13
So it lines up with the user story.
2:16
So what we want to do is return max misses
2:20
minus misses dot length.
2:25
The length method tells us
how many characters are in the misses
2:28
string, which equals
the number of wrong guesses made.
2:31
Cool.
2:34
Now that we have that set up,
let's upgrade our display progress
2:35
to show the remaining tries.
2:38
We'll say, you have %d tries left to solve.
2:45
And remember, that %d is for decimal,
2:55
and that lets us plug in numbers
in our format string.
2:57
So here we're using printf
with two different format specifiers.
3:02
So the first placeholder, %d, gets filled
with the remaining tries count.
3:06
And the second placeholder, %s,
3:11
gets filled with the current progress.
3:13
Now, let's build
3:17
the loop that will keep asking for guesses
while there are remaining attempts.
3:18
The specific story doesn't talk
about what to do with a win,
3:22
so let's not handle that case yet.
3:26
We'll stay focused on
just this story's requirements.
3:28
Alright, let's update our main method.
3:31
We'll replace our single
guess logic by deleting this conditional
3:34
and display progress method call.
3:37
Then we'll wrap these method calls
in a while loop
3:41
and we'll have it run
as long as the get remaining tries
3:44
is greater than zero.
3:46
Each time
3:55
through the loop we show the progress
3:55
and get a guess from the player.
3:59
Let compile and test
4:06
it Awesome!
4:18
It says we have 7 tries left to solve,
which is right.
4:18
Let's guess T.
4:22
Cool, the count didn't go down
because we didn't miss.
4:24
Now let's try some wrong letters
to see if the count decreases.
4:28
Perfect!
4:31
We can see the tries counting down
with each wrong guess,
4:33
and when we make a correct
guess, the count stays the same.
4:36
The loop
4:40
keeps
the game going until we run out of tries.
4:41
Nice job!
4:44
I think we can go ahead
and consider this story
4:46
done.
4:48
We are so close!
4:52
There are only two stories left.
4:54
By focusing on just what this story
required, we've made solid progress
4:56
while leaving clear work
for the remaining stories to handle.
5:00
You're mastering using Java objects
and picking up some more core Java skills.
5:03
You're doing great.
5:08
Let's keep this momentum
going in the next stage.
5:10
For now, I'd like to congratulate you
all on the awesome progress you're making.
5:12
I've been throwing a lot of info your way
and you're processing it all.
5:16
But you don't need me to tell you that.
5:20
It's evident from all these challenges
you've been acing.
5:22
Just like this last one up next
5:25
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