Bummer! You must be logged in to access this page.
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 figure out how to inform the user about the current state of the Word puzzle.
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
Okay, so the next story is, as a guesser,
I should be presented
0:00
with my current progress
so I can make an educated guess.
0:04
I'll move this story card
into the In Progress column.
0:08
So current progress,
that would be like blank
0:13
slots to show what has been guessed
and what needs to be guessed.
0:15
So for example, our game instance
has the answer treehouse right now.
0:19
So in order to show the progress
0:24
when there was no guess,
we do something like this.
0:26
And then when the guess was applied
to the game using apply
0:30
guess, it stores that in our hits
or misses variables.
0:32
So let's assume that the guess
for the letter T was applied.
0:36
It would go in our hit string.
0:39
So now,
when we were writing out these blanks,
0:41
we would just check to see if it's been
guessed.
0:44
Is it in hits?
0:46
And if so, we'll write up the letter.
0:47
Otherwise, we'll write out the blank.
0:49
Let's also guess H.
0:52
So now our hits variable has T and an H.
0:54
Alright, so we'll loop
through each of the values in the answer.
0:57
The first letter is T.
1:01
Does our hits have T?
1:02
Well, it does, so we write it.
1:04
Next in our answer is R.
1:07
Is that in hits? Nope,
so we write a blank.
1:08
Next up is E.
1:11
Is that in our hits?
1:13
And one more time, it's not in our hits.
1:14
And then finally, H.
1:17
So we'll just loop
1:19
through each of the letters in the answer
and see if it has been guessed.
1:19
If it has been, we'll show it.
1:23
Otherwise, we'll leave it blank.
1:25
First though, before we dive
in, I just wanted to reiterate that
1:28
because we are hiding the implementation
from the consumer
1:31
we can try several approaches.
As long as the results are the same
1:34
they won't even know that we made a change.
That means we can use any means necessary
1:38
to accomplish our goals,
and that's refreshing.
1:43
I mean, perhaps there is a better way
to do what we're about to do,
1:46
but we should feel empowered to innovate
and not worry about getting things
1:49
perfect, because we can always
make it better, right?
1:52
Let's focus on getting things working.
1:55
Okay, so let's go make a method
on our game class
1:59
that gets the current progress.
It's definitely public,
2:02
it's going to return a string,
and it's going to show dashes
2:06
for unguessed letters or the letter
if it's been guessed, right?
2:09
Like we just talked about.
2:12
It doesn't need to take anything
because it's using the internal state.
2:14
So let's see. Let's start
2:18
with a temporary variable.
We'll call it progress,
2:20
we'll just make an empty string
and we'll kind of add on to that. Alright,
2:24
so we need to iterate over each character
in the answer string, right?
2:28
Sounds familiar, huh?
2:31
This sounds like a great place
to flex our enhanced for loop muscles.
2:33
But if you remember, the enhanced
for loop requires an array
2:37
or collection of some kind, and oddly,
a string won't do the trick.
2:40
So we need to also bring back that
toCharArray method we toyed with before.
2:45
So let's get a for each loop going.
2:50
With for, we want the char data type.
2:52
call it letter
2:56
add our colon and we'll do
answer.toCharArray. Fantastic.
2:58
So this is going to go through
all the letters in the answer
3:10
and for each one,
we'll figure out what character to display
3:12
Let's set the default to display
an underscore.
3:16
So the display variable will hold either
an underscore
3:19
for unguessed letters
or the actual letter for guessed letters.
3:21
Now to see if we actually have a hit,
let's check if the letter has been
3:26
guessed.
3:30
Remember, we can do that
by looking in our hits string.
3:31
so if hits dot index of letter
3:34
and we'll plug in our strange
inverted logic here if it's not equal
3:40
to negative one
meaning it found our letter
3:44
inside of the hit string.
3:47
So if that's true, and it
found our current letter in hits,
3:52
we're going to go ahead
and set the display to the letter.
3:55
Then we'll use our trick here
3:59
to plus equal the display to our progress.
4:00
So we're basically appending
each one of the displays there.
4:05
And then finally, after our
for loop's all done, we'll return
4:08
the progress.
4:11
Cool.
4:16
Okay, and now
because we want to actually display
4:18
that current progress,
let's go ahead and allow our prompter
4:20
to display the progress
by adding a method in prompter.
4:23
I'll call it display progress.
4:27
and it doesn't return anything
so I'll give it the return
4:30
type of void.
4:32
Then we'll system.out.printf try to solve
4:39
percent s and a new line
4:45
and we call the new getCurrentProgress
4:51
method on our game instance
4:53
Awesome, so we'll save that.
4:59
Now we're accessing that method
that we just wrote.
5:01
Over in our wordGuesser file, let's use it.
5:04
prompter.displayProgress
5:09
Then we'll prompt for the guess,
and then finally we'll say
5:14
prompter.displayProgress again
just to make sure it's
5:17
updating correctly.
5:20
Cool, and now let's compile and test.
5:27
Alright,
so there's all of our dashes, right?
5:37
One for every letter in treehouse,
so we're going to guess T.
5:39
Cool, so we got a good guess
and now look, there's our puzzle
5:44
with T showing. So let's go ahead and do it
one more time.
5:47
Let's just put an E to make sure.
5:52
There it is, we can see
5:56
all three E's are showing up. Alright,
so I think we should be able to check off
5:57
As a guesser, I should be presented
with my current progress
6:03
so I can make an educated guess. Done!
6:06
Awesome!
6:11
We knocked out another one.
6:13
By looping through the letters
in the answer, we've gained the ability
6:15
to show off where characters live
within a series of characters.
6:17
We're on a roll,
but before we get too ahead of ourselves,
6:21
let's take a quick swing at an exercise
and make sure that all of
6:24
this is sinking in.
6:27
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