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