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 set up the initial state of our Game logic here. We will perform the first half of 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
Okay,
so let's take a stab at the first story.
0:00
As a guesser, I should be able to submit
a guess so that I can play the game.
0:03
Sometimes these beginning stories
are a little obvious.
0:08
Of course, a guesser is going to guess,
0:11
but it needs to be stated
so that development will happen.
0:13
Let's think about this.
0:16
Our game object currently holds
what the answer is,
0:18
and now we want to start having it store
0:21
what guesses
have been made towards that answer.
0:22
So this story is requiring that the game
allows the submission of a guess.
0:25
Sounds like we might need to create
a method that accepts a single letter.
0:30
How about ApplyGuess?
0:34
So with that guess, we should be
able to tell if that guess is a hit,
0:36
like it's in the answer,
or if it's a miss if it isn't.
0:39
By storing this information in the game
instance, others could then apply
0:43
guesses from whatever means.
0:46
Let's get it working real quick,
and things will start to click.
0:49
Sound good? Let's go.
0:52
Okay, first, let's open up JShell
and play around with that char
0:54
data type
we introduced back in the arrays course.
0:58
Let's create one.
1:04
So char, however it's pronounced,
and notice it's lowercased.
1:05
This is another
one of those primitive data types.
1:09
Let's just name
it letter and set it equal to a.
1:12
Note that we use single quotes for char
literals.
1:16
This is important in Java.
1:19
Double quotes are for strings.
1:20
Single quotes are for individual
characters or charicters.
1:22
I think my brain is melting.
1:27
Okay, now let's see if we can check
if a character exists in a string.
1:29
So let's make a new string named example
1:34
and set it equal to hello, double quotes.
1:37
There's a
1:41
few ways to do this,
but we'll use index of.
1:42
So we can say example dot index of
and provide
1:45
the character e, single quotes.
1:48
This returns one, the position
where e appears.
1:52
But wait, shouldn't E be at position 2?
1:56
1, 2?
1:58
This is that sneaky zero-based
counting again like we learned in arrays.
2:00
Baby years, remember?
2:04
So we start at zero and then 1 then 2
and so on
2:05
So in the string hello E is at position 1.
2:10
0 1. Cool!
2:12
You can see here
if we search for capital H
2:16
there's our zero, the first position.
2:24
So what happens when a character isn't
found?
2:26
Let's try Z.
2:29
Z definitely isn't in the string hello.
2:30
It returns negative 1
when the character doesn't exist.
2:33
Interesting.
2:36
So essentially, when using index of,
if we get a return
2:37
value of zero or higher,
we know it exists in the string.
2:41
If we get a negative one,
we know it wasn't found.
2:44
So we can use this to check
if a character exists
2:47
by seeing if index of returns
negative one or not.
2:50
Let's check it out.
2:53
We can go back to our E search here
with the up arrow and say if this,
2:55
the return value from index of
2:59
is not equal to negative 1
using that not symbol we learned, right?
3:02
So that's true because this would have
returned positive 1, right?
3:07
So it's not equal to negative 1.
3:11
Let's try it with z.
3:13
That returns false.
3:16
So checking for z did return a negative 1.
3:18
This essentially checked is negative
1 not equal to negative 1?
3:21
Well, that's false, right?
3:25
I know it's a bit strange.
3:26
It'll make more sense here shortly.
3:27
We'll also need something else
that's possible
3:30
you haven't come across yet
to achieve our goal here.
3:32
So we're going to be storing each
guess that the player makes into a string.
3:34
So we need a way to add to a string.
3:38
We can use string concatenation
to build up our list of guesses.
3:41
You can add strings to strings
using the plus operator.
3:45
For example,
we can add to our example variable.
3:48
Example equals example
3:52
plus and start a new string.
3:55
Let's add a space
for separation of words and type WOR.
3:58
Cool, right?
4:04
And you can also concatenate chars.
4:06
So example equals example plus L.
4:08
Single quotes there.
4:12
And you can also use the shortcut
like we learned when adding integers.
4:15
So you can say example plus equals D.
4:18
And that will add D
to our example string. Perfect!
4:23
I think we got what we need.
Let's exit JShell
4:27
and apply this to our game.
4:30
We need a way to track hits and misses,
4:34
so let's add two string fields
to store them in our game class.
4:37
private string hits
4:42
and private
4:47
string misses.
4:49
Awesome.
4:52
You might be wondering, wouldn't
an array be better suited here?
4:53
Well, it's possible, yes,
4:56
but remember how icky it was adding
and removing from arrays?
4:58
If we used an array,
we'd have to create a new one every time
5:02
the player guesses a letter, copy
over old letters, and add the new one.
5:04
Yikes.
5:09
Kind of like building a new shelf
every time you get a new book.
5:10
Strings make it much simpler for now.
5:13
This would be ideal for lists,
but we haven't learned those yet,
5:16
so working with strings and chars
is ideal for us in this situation.
5:18
Okay, then we'll initialize them
as empty strings in our constructor.
5:22
Now let's create our apply guess method.
5:33
We're going to be calling this
from outside of this file.
5:35
Separation of concerns, right?
5:38
So it will be a public method,
and we want it to return a boolean
5:40
to let the guesser know
if they got a hit or a miss.
5:44
And it should take in the character
5:48
that they guessed.
5:49
Next, let's check
5:56
if the provided character is present
in the answer by creating an isHit
5:57
variable and using the indexOf method
like we discussed earlier.
6:01
Remember, our not equals condition
we did returned a true or false,
6:05
so this will be a Boolean, isHit, equals.
6:09
Now we check on the answer, indexOf,
6:14
and supply the passed in guest letter.
6:17
Now we can check
if this is not equal to negative 1, right?
6:20
So this should now return false
6:25
when it's a miss, or true
if the character is present in the answer.
6:26
Now we need to store the letter
in the appropriate list.
6:31
So we can create a conditional
that checks our isHit variable
6:35
If it true we add it to the hits
6:38
We can say if isHit
6:41
This is checking if isHit is true.
6:47
It's the same as saying if hit equals
true.
6:49
It's just a nifty shorthand.
6:53
Now we can use our other nifty shorthand
for addition.
6:55
So if isHit is true, we want to add
our letter to our hits variable.
6:58
Hits plus equals letter.
7:03
Nice.
7:07
Now if our condition isn't met, meaning
7:09
isHit is false,
we'll want to add the letter to the misses
7:11
variable,
and for this we can use an else block.
7:14
You may not have seen this before,
but every if statement can have
7:18
an attached else statement, which will run
if the condition above is not met.
7:21
So we say else, and open a new block.
7:25
In here we can do the same,
but adding our letter to misses
7:29
instead.
7:32
Awesome!
7:36
Finally, we'll return the boolean
that will tell the guesser
7:36
whether it was a hit or not.
7:39
Let's save
this and test our code in JShell
7:43
by opening our game.java file.
7:45
slash open
7:53
game.java Cool,
7:54
let's initialize
a new game instance by saying
7:58
game game equals new game treehouse.
8:02
Then I'll try to call our method
and provide it t,
8:11
which should return true.
8:13
And if we change it to a letter
8:22
that isn't present in our answer,
it should be false.
8:23
Perfect.
8:26
Our game can now accept guesses and
tell us whether they're hits or misses.
8:27
This separation is important.
8:31
We need to know which letters are correct
to show progress,
8:32
and which are wrong to count
towards the loss condition.
8:36
Eventually,
our prompter class will get guesses
8:39
from the user
and pass them to this method.
8:41
Great job.
8:44
We've completed the core logic
for our first user story.
8:45
Next, we'll work on the prompter class
to actually get input from users
8:48
to apply their guesses.
8:52
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