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 prompt for a guess from the user and use our game logic to store the value. We will complete our first user 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
Alright, so now that we have the ability
to apply a guess and keep track of hits
0:01
and misses, the time has come
to actually take some input from the user.
0:05
Now this is probably going to look
a little strange in our current state,
0:09
until we get some more detailed stories
in like the ones that show
0:12
current progress.
0:15
We haven't even shown the users
what they're attempting to guess,
0:16
but here we are asking for their guesses.
0:20
This half-baked approach is definitely
part of the development process.
0:22
Once we get some things working,
0:26
we can iterate until we get to our working
product.
0:27
Development isn't always linear
and we have to start somewhere, right?
0:31
As long as we complete all the stories
on our board, we know we'll get there.
0:34
Okay, so let's go get our prompter
able to accept guesses.
0:38
So we currently don't have anything at all
in our prompter class.
0:43
The prompter we're building is going
to use the game code that we just wrote.
0:47
Since the prompter
needs to submit guesses to the game,
0:51
it's going to need to know about our game
object.
0:53
Let's store a private variable
that is an instance of our game.
0:56
private
1:00
game and we'll just call it game.
1:02
What good is a prompter
without a reference to our game?
1:06
So let's require one
by defining it in our constructor.
1:09
So public prompter
1:13
this will take in a game
and we'll name it game
1:15
then we'll set this.game
1:20
equals the game that was passed in.
1:22
That's a lot of game.
1:25
This ensures that every prompter
has a game to work with.
1:27
We're using the same dependency pattern
we learned in the objects course.
1:30
Okay, now let's
add a method that will prompt for a guess
1:35
and return
whether or not the guess was correct.
1:38
So we want this to be public.
1:42
We want it to to return a Boolean,
1:44
and we'll call it prompt for guess.
1:47
Okay now you probably used
the console object before which happens
1:53
to be one of many ways that's used to get
input and output to and from a user.
1:56
But for the sake of learning new tricks,
let's use a handy object called Scanner.
2:01
Now a scanner lives in a different package
than where all the other things
2:05
we've been importing are.
2:08
It lives in the Java util package.
2:10
And what that means is
we need to import it.
2:12
So let's do that.
2:14
Let's go up here and we'll say import
2:15
java.util.scanner.
2:19
And what this does is it allows us
to have access to this class name, right?
2:24
So now we can create a new
one of these types.
2:28
So let's make one.
2:30
So we'll say scanner, which is the type,
2:32
and we'll name it scanner,
and we'll make a brand new one of those.
2:35
So we'll say new scanner.
2:38
So scanner expects us to define
where the input is coming from.
2:40
So much like there's a system.out,
there's also a system.in.
2:44
So let's provide our output
for the prompt.
2:48
And we know how to do that, right?
2:50
We can say system.out.print.
2:52
And I don't need to format anything here.
2:56
And I'd like for the cursor
to be on the same line.
2:58
So we'll just use print,
not print line or print f.
3:00
Cool.
3:04
Let's say enter a letter.
3:05
And leave a space or two
at the end for where they'll type.
3:07
Scanner
has helpful methods for reading input.
3:11
We'll be using NextLine,
which reads everything
3:13
the user types until they press Enter,
even including spaces.
3:16
So let's store
the returned value in a variable
3:20
called GuessInput.
3:23
Now since our ApplyGuess
method takes a char and we're currently
3:27
working with a string, we need to extract
the first character out of it.
3:33
We can do this with a handy
method called charAt.
3:37
This method will return the character
at the provided index
3:40
and remember the first position is 0
so I'll provide that as an argument. char
3:44
guess equals
3:49
guessInput.chatAt, zero.
3:52
Okay, now
3:58
we have the guess, but checking
if it matches isn't the prompter's job.
3:59
That's the game's responsibility.
4:02
Good thing we passed in our game object.
4:04
We can just return
whatever apply guess tells us.
4:07
That method will return a true or false
if it was a hit or miss, right?
4:10
And this promptForGuess is responsible
for returning the same boolean
4:14
to our users.
4:17
So let's just return
what comes back from applyGuess
4:19
and supply our guess variable here.
4:22
Perfect.
4:27
The prompter handles user interaction.
4:28
The game handles game logic.
4:30
Clean separation of concerns.
4:31
Let's test this in wordguesser.java.
4:34
So we need to create a new instance
of our prompter, right?
4:37
We know how to do that.
4:40
prompter,
4:42
prompter,
4:44
equals new prompter.
4:46
And we need to provide a game instance
for our prompter's constructor, right?
4:48
Well, we've got a game right here.
4:52
So we'll pass in game.
4:54
This is where the connection happens
between our classes.
4:57
Awesome.
5:00
Now let's write some simple test code.
5:01
Let's store the response in a variable.
5:03
So prompt for guess returns
a boolean, right?
5:05
So we'll say boolean.
5:09
Let's just call it isHit,
and we'll set this equal
5:11
to our new prompter instance
dot prompt for guess.
5:14
Cool.
5:21
Now we've got our true or false whether
they supply to correct or incorrect guess.
5:21
Next, I'll use a conditional to check that
response and print a result to our user.
5:26
If the guess is a hit, using that
shorthand again,
5:31
I'll print good guess.
5:34
Else tell them they missed
5:42
Make sure you've got all your closing
curly braces in place.
5:56
It's really easy to miss one.
5:59
For example, if I remove this and place
my cursor on this one that should be
6:01
for the main method, you'll see
that our else block is grabbing that one.
6:05
Going to the class's closing brace,
you'll see the main is grabbing it,
6:09
so this will give us an error
because our class won't be closed,
6:12
but really it's our else block
that isn't closed right?
6:16
It's a tricky one
and it's very common to make.
6:19
This is where keeping your code
cleanly indented is super helpful.
6:22
I can glance through this
and see that there's no curly brace here,
6:25
so I'll just add that back.
6:28
Okay, let's make sure
6:30
all our files are saved
and let's compile and test this.
6:31
clear and javac wordguesser.java
6:35
and java wordguesser.
6:41
Great, when we run it
we see enter a letter.
6:50
Let's try T
since we used treehouse as our answer.
6:53
Good guess, it works.
6:56
Let's run it again and try Z.
6:59
Nope, you missed.
7:06
Just as we expected. Great.
7:07
So let's check our story.
7:10
As a guesser, I should be able to submit
a guess so that I can play the game.
7:12
I think we accomplished that.
7:16
I'm going to call this Done-zo.
7:18
Wow, that feels great.
7:20
We completed our first story.
7:23
Nothing quite like that
feeling of moving something to Done.
7:25
It may not feel like much,
but now we can start on the other stories
7:28
that were essentially blocked by this one.
7:31
Let's pick up the next one
right after this quick exercise.
7:34
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