1 00:00:00,530 --> 00:00:01,130 Okay. 2 00:00:01,130 --> 00:00:02,710 Let's talk this through a bit. 3 00:00:02,710 --> 00:00:06,740 You probably noticed that none of the stories told us about how our app 4 00:00:06,740 --> 00:00:10,780 should be developed or even on what platform for that matter. 5 00:00:10,780 --> 00:00:14,530 Since so far we've only gone over how to make console based applications 6 00:00:14,530 --> 00:00:16,420 that seems like the obvious platform decision. 7 00:00:17,550 --> 00:00:19,170 However if we do this right. 8 00:00:19,170 --> 00:00:22,270 We should be able to reuse the game code that we write. 9 00:00:22,270 --> 00:00:25,705 Since no matter how we end up playing the game that logic will stay the same. 10 00:00:25,705 --> 00:00:29,580 We'll need to make sure that we separate the prompting and 11 00:00:29,580 --> 00:00:33,900 displaying portion of the code from the game state and behavior. 12 00:00:33,900 --> 00:00:37,680 This approach is called the separation of concerns and we'll touch on it here 13 00:00:37,680 --> 00:00:41,430 lightly, and it will make a lot of sense after we get the code up and running. 14 00:00:41,430 --> 00:00:45,000 But, the idea is this, we're building a console based application and 15 00:00:45,000 --> 00:00:47,635 there is certain code that is needed for that to work. 16 00:00:47,635 --> 00:00:49,910 But the game logic itself like the rules and 17 00:00:49,910 --> 00:00:52,720 how you win, it pretty much stays the same. 18 00:00:52,720 --> 00:00:57,090 By doing the separation, we could in fact end up using that game logic 19 00:00:57,090 --> 00:01:01,390 code in like a desktop app, a mobile app, or even a web application. 20 00:01:01,390 --> 00:01:04,650 So with that in mind let's build our structure out. 21 00:01:04,650 --> 00:01:07,430 Let's build our game and prompter blueprints. 22 00:01:07,430 --> 00:01:09,750 That separation should be enough to get us started. 23 00:01:10,970 --> 00:01:13,510 Okay. So I've created a new workspace for 24 00:01:13,510 --> 00:01:17,910 this part of the course, it has our starter file Hangman.java in it. 25 00:01:17,910 --> 00:01:21,170 So first thing we should do is let's build our prompter class, 26 00:01:21,170 --> 00:01:22,670 it should be pretty straightforward. 27 00:01:22,670 --> 00:01:27,500 So I'm gonna make a new file, I'm gonna call it prompter.java. 28 00:01:27,500 --> 00:01:30,290 Okay, so we wanna have a class. 29 00:01:30,290 --> 00:01:34,000 And it needs to be named the same as the file that it's in, right. 30 00:01:34,000 --> 00:01:35,830 So we're need to call it Prompter. 31 00:01:37,110 --> 00:01:40,200 And let's open up the code block there. 32 00:01:40,200 --> 00:01:42,070 Remember that's enough. 33 00:01:42,070 --> 00:01:46,880 So we'll use this prompter object to do all of the IO or input output. 34 00:01:46,880 --> 00:01:51,130 So we also need to separate out the game logic in it's own file here. 35 00:01:51,130 --> 00:01:54,700 So we'll do new file, game.java. 36 00:01:54,700 --> 00:01:59,260 Okay, and following the similar logic, we'll make a new class called Game and 37 00:01:59,260 --> 00:02:01,170 I'm gonna open it up, and I'm gonna close it right away. 38 00:02:02,210 --> 00:02:05,180 Give us some space and now, one thing that we know for 39 00:02:05,180 --> 00:02:09,510 certain is that this class will need to know the answer to the puzzle, right? 40 00:02:09,510 --> 00:02:14,346 So, let's say it's gonna be a string and answer. 41 00:02:14,346 --> 00:02:19,290 Now, it should be private because that's how we always start things out, right. 42 00:02:19,290 --> 00:02:20,320 We want to be private. 43 00:02:21,650 --> 00:02:25,390 Okay, since a game is not really any good without an answer, 44 00:02:25,390 --> 00:02:29,130 let's force the creation of a game to provide an answer. 45 00:02:29,130 --> 00:02:31,420 We should use a constructor to do that. 46 00:02:31,420 --> 00:02:34,630 So let's make a new public, and 47 00:02:34,630 --> 00:02:41,010 a constructor Again has the same name as the class, so public game. 48 00:02:41,010 --> 00:02:43,790 And we want to have it pass in an answer. 49 00:02:43,790 --> 00:02:45,770 So we'll say string answer. 50 00:02:46,900 --> 00:02:50,480 And then what we'll do is we'll take what was passed in and 51 00:02:50,480 --> 00:02:53,140 set our private answer variable. 52 00:02:53,140 --> 00:02:59,860 And to avoid naming collisions we'll say this.answer = answer. 53 00:03:01,350 --> 00:03:04,140 That way, we know for sure that we're talking about 54 00:03:04,140 --> 00:03:08,880 the instances field versus the argument that was passed in. 55 00:03:08,880 --> 00:03:12,510 Great, that should be enough to define at the separation of concern. 56 00:03:12,510 --> 00:03:15,190 So I'm going to save this, make sure that they're both saved. 57 00:03:15,190 --> 00:03:17,650 I gonna come over to Hangman. 58 00:03:17,650 --> 00:03:23,070 And let's make a brand new instance of our game. 59 00:03:23,070 --> 00:03:26,088 So we'll say the type is game. 60 00:03:26,088 --> 00:03:29,250 The instance of variable name is going to be Game and 61 00:03:29,250 --> 00:03:33,170 we're going to say when you want a new Game. 62 00:03:33,170 --> 00:03:34,870 That's gonna to call the constructor that we have there. 63 00:03:34,870 --> 00:03:36,280 We're gonna pass in the answer. 64 00:03:36,280 --> 00:03:37,340 Let's pass in tree house. 65 00:03:38,690 --> 00:03:39,800 So let's do that. 66 00:03:39,800 --> 00:03:41,210 I'm just gonna go ahead and compile and 67 00:03:41,210 --> 00:03:43,310 make sure that we got all of our syntax correct. 68 00:03:43,310 --> 00:03:48,030 And I just need to press to get in there to hangman.job and what that will do 69 00:03:48,030 --> 00:03:52,089 is because game is in the same folder, it will try to compile all of this. 70 00:03:53,260 --> 00:03:55,607 Well it'll at least compile Game. 71 00:03:57,839 --> 00:03:59,892 Right. So I didn't compile Prompter because we 72 00:03:59,892 --> 00:04:01,300 didn't access prompter yet. 73 00:04:01,300 --> 00:04:02,600 But it did compile game. 74 00:04:02,600 --> 00:04:03,380 So we know that's working. 75 00:04:03,380 --> 00:04:04,410 So there's no errors. 76 00:04:04,410 --> 00:04:09,590 And again, since this game class is in the same folder or package, Hangman 77 00:04:09,590 --> 00:04:14,240 just found the proper class without us needing to specify where to find it. 78 00:04:14,240 --> 00:04:15,810 Our separation of concerns looks great. 79 00:04:15,810 --> 00:04:19,610 We have the prompter class which we used to deal with the IO and 80 00:04:19,610 --> 00:04:22,010 the game class which will maintain the logic. 81 00:04:22,010 --> 00:04:23,820 It's completely separated. 82 00:04:23,820 --> 00:04:28,260 We also have the main executable file Hangman.Java where we'll use 83 00:04:28,260 --> 00:04:31,480 instances of the prompter and the game classes. 84 00:04:31,480 --> 00:04:32,250 Awesome. 85 00:04:32,250 --> 00:04:32,800 All right. 86 00:04:32,800 --> 00:04:36,180 Now that we have our main objects defined let's get to completing those stories.