Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
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.
-
0:00
Okay.
-
0:01
Let's talk this through a bit.
-
0:02
You probably noticed that none of the stories told us about how our app
-
0:06
should be developed or even on what platform for that matter.
-
0:10
Since so far we've only gone over how to make console based applications
-
0:14
that seems like the obvious platform decision.
-
0:17
However if we do this right.
-
0:19
We should be able to reuse the game code that we write.
-
0:22
Since no matter how we end up playing the game that logic will stay the same.
-
0:25
We'll need to make sure that we separate the prompting and
-
0:29
displaying portion of the code from the game state and behavior.
-
0:33
This approach is called the separation of concerns and we'll touch on it here
-
0:37
lightly, and it will make a lot of sense after we get the code up and running.
-
0:41
But, the idea is this, we're building a console based application and
-
0:45
there is certain code that is needed for that to work.
-
0:47
But the game logic itself like the rules and
-
0:49
how you win, it pretty much stays the same.
-
0:52
By doing the separation, we could in fact end up using that game logic
-
0:57
code in like a desktop app, a mobile app, or even a web application.
-
1:01
So with that in mind let's build our structure out.
-
1:04
Let's build our game and prompter blueprints.
-
1:07
That separation should be enough to get us started.
-
1:10
Okay. So I've created a new workspace for
-
1:13
this part of the course, it has our starter file Hangman.java in it.
-
1:17
So first thing we should do is let's build our prompter class,
-
1:21
it should be pretty straightforward.
-
1:22
So I'm gonna make a new file, I'm gonna call it prompter.java.
-
1:27
Okay, so we wanna have a class.
-
1:30
And it needs to be named the same as the file that it's in, right.
-
1:34
So we're need to call it Prompter.
-
1:37
And let's open up the code block there.
-
1:40
Remember that's enough.
-
1:42
So we'll use this prompter object to do all of the IO or input output.
-
1:46
So we also need to separate out the game logic in it's own file here.
-
1:51
So we'll do new file, game.java.
-
1:54
Okay, and following the similar logic, we'll make a new class called Game and
-
1:59
I'm gonna open it up, and I'm gonna close it right away.
-
2:02
Give us some space and now, one thing that we know for
-
2:05
certain is that this class will need to know the answer to the puzzle, right?
-
2:09
So, let's say it's gonna be a string and answer.
-
2:14
Now, it should be private because that's how we always start things out, right.
-
2:19
We want to be private.
-
2:21
Okay, since a game is not really any good without an answer,
-
2:25
let's force the creation of a game to provide an answer.
-
2:29
We should use a constructor to do that.
-
2:31
So let's make a new public, and
-
2:34
a constructor Again has the same name as the class, so public game.
-
2:41
And we want to have it pass in an answer.
-
2:43
So we'll say string answer.
-
2:46
And then what we'll do is we'll take what was passed in and
-
2:50
set our private answer variable.
-
2:53
And to avoid naming collisions we'll say this.answer = answer.
-
3:01
That way, we know for sure that we're talking about
-
3:04
the instances field versus the argument that was passed in.
-
3:08
Great, that should be enough to define at the separation of concern.
-
3:12
So I'm going to save this, make sure that they're both saved.
-
3:15
I gonna come over to Hangman.
-
3:17
And let's make a brand new instance of our game.
-
3:23
So we'll say the type is game.
-
3:26
The instance of variable name is going to be Game and
-
3:29
we're going to say when you want a new Game.
-
3:33
That's gonna to call the constructor that we have there.
-
3:34
We're gonna pass in the answer.
-
3:36
Let's pass in tree house.
-
3:38
So let's do that.
-
3:39
I'm just gonna go ahead and compile and
-
3:41
make sure that we got all of our syntax correct.
-
3:43
And I just need to press to get in there to hangman.job and what that will do
-
3:48
is because game is in the same folder, it will try to compile all of this.
-
3:53
Well it'll at least compile Game.
-
3:57
Right. So I didn't compile Prompter because we
-
3:59
didn't access prompter yet.
-
4:01
But it did compile game.
-
4:02
So we know that's working.
-
4:03
So there's no errors.
-
4:04
And again, since this game class is in the same folder or package, Hangman
-
4:09
just found the proper class without us needing to specify where to find it.
-
4:14
Our separation of concerns looks great.
-
4:15
We have the prompter class which we used to deal with the IO and
-
4:19
the game class which will maintain the logic.
-
4:22
It's completely separated.
-
4:23
We also have the main executable file Hangman.Java where we'll use
-
4:28
instances of the prompter and the game classes.
-
4:31
Awesome.
-
4:32
All right.
-
4:32
Now that we have our main objects defined let's get to completing those stories.
You need to sign up for Treehouse in order to download course files.
Sign up