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 build out the starts of our objects, thinking heavily on Separation of Concerns. We will focus on separating the display and prompting from the game logic.
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, let's talk this through a bit.
0:00
You probably noticed that
none of the stories told us about
0:02
how our app should be developed,
or even on what platform.
0:05
Since we've only covered
0:09
console applications so far,
that seems like the obvious choice.
0:10
However, if we do this right,
we should be able to reuse the game code
0:14
that we write, since
0:17
no matter how we end up playing the game,
that logic will stay the same.
0:18
We'll need to make sure
that we separate the prompting
0:23
and displaying portion of the code
from the game state and behavior.
0:26
This approach is called the separation
of concerns, and we'll touch on it here
0:30
lightly, and it'll make a lot of sense
after we get the code up and running.
0:34
But the idea is this.
0:38
We're building
a console-based application,
0:39
and there is certain code
that is needed for that to work.
0:41
But the game logic itself, like the rules
0:44
and how you win, it
pretty much stays the same.
0:47
By doing this separation,
we could in fact end up using that game
0:50
logic code in things like a desktop app,
a mobile app, or even a web application.
0:53
So we'll create two classes:
0:59
a game class that knows the rules, tracks
guesses and determines if you won or lost,
1:01
and a prompter class
1:06
that handles getting input from the player
and displaying information.
1:07
That separation should be enough
to get us started. So let's build our game
1:11
and prompter blueprints. Go ahead and open
the workspace attached to this video.
1:15
Okay, I've created a new workspace
1:21
with our starter file, WordGuesser.java.
1:23
So let's start by building our two
main classes.
1:26
First, let's create our prompter class.
1:29
I'm going to right click here
and make a new file called
1:31
Prompter.java.
1:34
And let's create our class.
1:40
It needs the same name as the file, right?
1:43
So prompter,
and we'll open up that block and close it.
1:45
The prompter class will handle
all the input and output, or I.O.
1:50
Asking the player for guesses, showing
the current progress, that sort of thing.
1:54
Cool.
1:58
Let's create our Game class
in a new file called Game.java.
1:59
Class, Game, open and close it off.
2:07
The Game class will track the game state
and contain all the rules.
2:13
Now, one thing we know for certain
is that this class
2:16
will need to know the answer to the puzzle,
right?
2:19
So let's create a String member variable
and just name
2:21
it answer.
We should make it private too, right?
2:24
Because that's how we always start.
2:27
Keep things private
until we have a reason to expose them.
2:29
Since a game isn't really good
without an answer,
2:33
let's force
the creator of a game to provide one.
2:35
So we should definitely implement
a constructor.
2:38
Let's do it.
2:41
So this is public, and remember
constructors do not have a return type,
2:42
and they must use the same name
as the class.
2:46
Cool.
2:49
We want to take in an answer,
so let's define a string parameter
2:50
and name it answer.
2:54
Inside, we need to assign
2:57
our private field up top
to what's being passed in.
2:58
And if you remember,
we can use the this keyword
3:01
to avoid naming collision between it
and this passed in value.
3:04
So this dot answer equals answer.
3:08
Awesome
3:13
Okay, let's test our setup so far.
Make sure these files are saved
3:15
and in WordGuesser,
let's create a new instance of our game.
3:18
So it's of type game,
3:24
let's just name it game,
this will be a new game instance.
3:26
We need to provide an argument
for the answer
3:31
so let's just use "treehouse".
3:33
Let's compile this to make sure everything
works.
3:36
javac WordGuesser.java. Great! Java
3:39
automatically found
3:52
and compiled our game class because it's
in the same folder or package.
3:54
And since our WordGuesser class uses
this game class,
3:58
it should have compiled that for us
as well.
4:01
Let's refresh over here.
4:03
Yep, it sure did.
4:08
Our separation of concerns looks great.
4:11
We have the prompter class,
which we use to deal with the I.O.,
4:14
and the game class,
which will maintain the logic.
4:17
It's completely separated.
4:19
We also have the main executable
file, wordguesser.java,
4:21
where we'll use the instances
of the prompter and game classes.
4:25
Awesome.
4:29
Now that we have our main objects defined,
let's get to completing those stories.
4:30
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