1 00:00:00,970 --> 00:00:05,320 All right, so now we have the ability to apply a guess and keep track of hits and 2 00:00:05,320 --> 00:00:09,710 misses, the time has come to actually take some input from the user. 3 00:00:09,710 --> 00:00:12,192 Now this is probably going to look a little strange in our current state, 4 00:00:12,192 --> 00:00:16,155 until we get some of the more detailed stories like the ones that shows 5 00:00:16,155 --> 00:00:17,765 the current progress for example. 6 00:00:17,765 --> 00:00:22,105 This is gonna be fairly useless except for a cool trick at the command line. 7 00:00:22,105 --> 00:00:25,195 What I mean is this we haven't even shown users of our hang man app 8 00:00:25,195 --> 00:00:29,137 what they are attempting to guess but here we are asking them for their guests. 9 00:00:29,137 --> 00:00:33,267 Now this half baked app is definitely part of the development process. 10 00:00:33,267 --> 00:00:34,527 Once we get some things working. 11 00:00:34,527 --> 00:00:37,807 We can iterate on the concepts until we get to our working product. 12 00:00:37,807 --> 00:00:42,433 But development isn't always Is linear and we have to start somewhere right 13 00:00:42,433 --> 00:00:45,613 now as long as we complete all the stories on our test board. 14 00:00:45,613 --> 00:00:48,383 We know we'll get there and we do that, because we've looked at them. 15 00:00:48,383 --> 00:00:51,523 Okay, so let's get our prompter able to accept guesses. 16 00:00:53,053 --> 00:00:57,403 Okay, so we currently don't have anything at all in our prompter class, and 17 00:00:57,403 --> 00:01:00,853 I wanna get back to a statement that I made earlier. 18 00:01:01,870 --> 00:01:05,310 About how someone else will be using your code. 19 00:01:05,310 --> 00:01:08,370 So the problem here that we're gonna be writing is going to 20 00:01:08,370 --> 00:01:10,280 use the game code that we just wrote. 21 00:01:10,280 --> 00:01:14,410 Now, it's possible but we're on a team and we've made a team responsible for 22 00:01:14,410 --> 00:01:15,360 prompting. 23 00:01:15,360 --> 00:01:17,510 The front in portion of our app. 24 00:01:17,510 --> 00:01:20,720 The prompter is going to need to know about our game object. 25 00:01:20,720 --> 00:01:22,180 So let's do that. 26 00:01:22,180 --> 00:01:27,020 Lets store a private variable that is an instance of our game. 27 00:01:27,020 --> 00:01:31,600 So we'll say game game, and again we wanted to make that private. 28 00:01:31,600 --> 00:01:36,250 We always want to start private, What 29 00:01:36,250 --> 00:01:40,150 good is one of these prompters without a reference to our game. 30 00:01:40,150 --> 00:01:46,400 So let's require one for instantiation by defining it in our constructor. 31 00:01:46,400 --> 00:01:48,240 So again, let's make a constructor. 32 00:01:48,240 --> 00:01:52,240 So I'll say, public and it's the same name as the class prompter. 33 00:01:54,420 --> 00:01:56,810 And we're going to require a game. 34 00:01:56,810 --> 00:02:00,790 So the game is the type game and we'll call it game. 35 00:02:00,790 --> 00:02:03,700 And of course we have our little naming collision problem again. 36 00:02:03,700 --> 00:02:07,330 So we wanna talk about the private variable game. 37 00:02:07,330 --> 00:02:10,680 And set that equal to the argument that came in. 38 00:02:11,950 --> 00:02:14,960 Wanted to show you another little trick really quick here in jshel. 39 00:02:16,080 --> 00:02:20,790 So, if we have a string, so we have string example equals hello. 40 00:02:22,290 --> 00:02:25,310 If I wanted to get a specific character out of that, 41 00:02:25,310 --> 00:02:29,760 there is a Method on strings called charAt. 42 00:02:29,760 --> 00:02:30,890 So, I can say charAt. 43 00:02:30,890 --> 00:02:33,870 So, let's say that I wanted to get the first character. 44 00:02:33,870 --> 00:02:36,100 So that's one, right? 45 00:02:36,100 --> 00:02:38,970 No, zero is the first, I [LAUGH] forgot. 46 00:02:38,970 --> 00:02:39,920 Baby years right? 47 00:02:39,920 --> 00:02:41,540 So zero will get me a. 48 00:02:41,540 --> 00:02:45,280 So the very first character that, awesome. 49 00:02:45,280 --> 00:02:50,080 Okay, now, in our prompter object, 50 00:02:50,080 --> 00:02:53,310 let's add a method that will prompt for 51 00:02:53,310 --> 00:02:57,420 a guess, and it will return whether or not the guess was correct. 52 00:02:57,420 --> 00:03:00,200 That sounds pretty boolean to me, right? 53 00:03:00,200 --> 00:03:01,490 So, we want this to be public. 54 00:03:01,490 --> 00:03:06,330 We wanna allow this to be called, so we'll say boolean, and that's gonna, again, 55 00:03:06,330 --> 00:03:10,888 be whether or not the guess was correct Say promptForGuess. 56 00:03:10,888 --> 00:03:16,808 [SOUND] Okay, now you've probably used the console object before which happens 57 00:03:16,808 --> 00:03:21,943 to be one of many ways that's used to get input and output from a user. 58 00:03:21,943 --> 00:03:27,270 But for the sake of learning new tricks, let's use a handy object called scanner. 59 00:03:27,270 --> 00:03:30,010 Now a scanner lives in the different package. 60 00:03:30,010 --> 00:03:32,860 Than where all the other things we've been importing are. 61 00:03:32,860 --> 00:03:34,890 It lives in the Java util package, 62 00:03:34,890 --> 00:03:37,550 and what that means is that we need to import it. 63 00:03:37,550 --> 00:03:38,210 So let's do that. 64 00:03:38,210 --> 00:03:44,520 Let's go up here, and we'll say import java.util.scanner. 65 00:03:44,520 --> 00:03:50,270 And what this does is it allows us to have access to this class name, right? 66 00:03:50,270 --> 00:03:54,470 So now, we can create a new one of these types. 67 00:03:54,470 --> 00:03:56,930 So scanner, when we create it. 68 00:03:56,930 --> 00:03:58,100 So let's make a new one. 69 00:03:58,100 --> 00:03:59,930 So we'll say Scanner, which is the type. 70 00:03:59,930 --> 00:04:02,930 And we'll name it scanner, our instance variable. 71 00:04:02,930 --> 00:04:04,360 And we'll make a brand new one of those. 72 00:04:04,360 --> 00:04:06,960 So we'll say new Scanner. 73 00:04:08,110 --> 00:04:12,231 So scanner expects us to define where the input is coming from so 74 00:04:12,231 --> 00:04:16,449 much like there's a system.out, there is also a system.in. 75 00:04:19,988 --> 00:04:23,677 Okay, so now we need to prompt them, we need to write out to the screen so 76 00:04:23,677 --> 00:04:25,370 we know how to do that already. 77 00:04:25,370 --> 00:04:30,870 So we'll say system .out.print and I want to keep things on the same line and 78 00:04:30,870 --> 00:04:32,880 I don't really need to format anything here. 79 00:04:32,880 --> 00:04:35,950 So let's just go ahead and we'll just use the print command off of out. 80 00:04:35,950 --> 00:04:37,800 It's just another one of these commands. 81 00:04:37,800 --> 00:04:39,000 So as they enter a letter. 82 00:04:39,000 --> 00:04:43,910 And we'll do a couple spaces there so that they have a space to enter it in, 83 00:04:45,350 --> 00:04:50,290 Scanner has a bunch of helper methods that help Parse input. 84 00:04:50,290 --> 00:04:52,960 Parse meaning read and figure out what it means there. 85 00:04:52,960 --> 00:04:56,350 So, much like how console had the read line method. 86 00:04:56,350 --> 00:04:59,400 Scanner uses one called next line 87 00:04:59,400 --> 00:05:03,490 which allows us to read a line that again happens when they press enter. 88 00:05:03,490 --> 00:05:04,150 So, let's do that. 89 00:05:04,150 --> 00:05:05,350 So we'll store it. 90 00:05:05,350 --> 00:05:06,860 We'll do string. 91 00:05:06,860 --> 00:05:09,820 Guess input and 92 00:05:09,820 --> 00:05:14,910 we'll do equals scanner.nextline and note case there. 93 00:05:16,040 --> 00:05:20,240 Since our applied guess method only takes a char 94 00:05:20,240 --> 00:05:24,110 we need to go get one of those using that method that we saw, right? 95 00:05:24,110 --> 00:05:25,830 That char at. 96 00:05:25,830 --> 00:05:29,180 So let's pull from this guess him put right we have a string. 97 00:05:29,180 --> 00:05:31,236 So let's get the first character off of it. 98 00:05:31,236 --> 00:05:39,610 I guessInput.charAt... that first character again is zero not one right, okay. 99 00:05:39,610 --> 00:05:42,490 So now, we have the guess. 100 00:05:42,490 --> 00:05:46,280 We need to see if that guess matches or not but 101 00:05:46,280 --> 00:05:48,220 that's not the prompter's job right. 102 00:05:48,220 --> 00:05:49,580 That's the game's job. 103 00:05:49,580 --> 00:05:53,080 Now it's a good thing that we passed in our game object, right. 104 00:05:53,080 --> 00:05:53,720 It's a good thing. 105 00:05:56,010 --> 00:06:00,890 Why don't we just return what comes out of applied guess, right? 106 00:06:00,890 --> 00:06:03,160 So apply guess is going to come and see if it exists and 107 00:06:03,160 --> 00:06:04,710 it's going to return a true or a false. 108 00:06:04,710 --> 00:06:11,370 So here we can do the same thing we could just return game.applyGuess. 109 00:06:11,370 --> 00:06:15,210 And play that Guess where that finally 110 00:06:15,210 --> 00:06:20,360 let's add some code to hang man dot java over here and let's use it. 111 00:06:20,360 --> 00:06:21,790 So, let's make a new prompter. 112 00:06:21,790 --> 00:06:22,530 So we know how to do that. 113 00:06:22,530 --> 00:06:24,350 It's a type prompter. 114 00:06:24,350 --> 00:06:26,810 It's a named prompter it's a new prompter. 115 00:06:26,810 --> 00:06:30,990 And remember the way that we're getting the game in there is we're passing it 116 00:06:30,990 --> 00:06:33,820 through the constructor through prompter constructor because what good is 117 00:06:33,820 --> 00:06:35,020 a prompter without the game. 118 00:06:35,020 --> 00:06:36,565 There we go game. 119 00:06:36,565 --> 00:06:41,300 Okay, so let's just write some simple code just walk through what we just did. 120 00:06:41,300 --> 00:06:46,570 So we'll say boolean is hit 121 00:06:46,570 --> 00:06:52,340 prompter dot prompt for guess, and again that's how we run our code. 122 00:06:52,340 --> 00:06:53,162 It's going to come in here. 123 00:06:53,162 --> 00:06:54,980 It's gonna create a new scanner is going to print this line and 124 00:06:54,980 --> 00:06:56,050 then it's going to wait here. 125 00:06:56,050 --> 00:06:59,100 It's going to wait until we finish and then it will go and go on through. 126 00:06:59,100 --> 00:07:01,300 So it's let's see. 127 00:07:01,300 --> 00:07:05,390 So once we get back that result from whatever that we type then we'll say 128 00:07:05,390 --> 00:07:10,010 if is hits and I'm gonna close that if right away. 129 00:07:11,290 --> 00:07:15,980 And then we'll say system.out.println. 130 00:07:15,980 --> 00:07:17,310 We got a hit. 131 00:07:18,800 --> 00:07:22,100 And obviously this is not something we would ever say to the user because hit, 132 00:07:22,100 --> 00:07:25,070 what does it mean to them but this is just for us to look at, all right. 133 00:07:25,070 --> 00:07:28,130 So this is something that you might not have seen before. 134 00:07:28,130 --> 00:07:31,330 Now we want to say what happens if they don't remember we need to do that 135 00:07:31,330 --> 00:07:33,480 branching that we saw else. 136 00:07:33,480 --> 00:07:36,985 We're gonna say 137 00:07:36,985 --> 00:07:45,140 system.out.printline, okay. 138 00:07:45,140 --> 00:07:48,270 So, look at this, I want to show this is a common problem that happened. 139 00:07:48,270 --> 00:07:53,450 So, I forgot to put my closing brace there, 140 00:07:53,450 --> 00:07:55,110 which I letting you in on the secret o what I'm trying to do here. 141 00:07:55,110 --> 00:08:00,060 So look, this one close but this else I didn't close it but it's grabbing. 142 00:08:00,060 --> 00:08:03,360 The methods it's actually the methods, but see it's not highlighting the method. 143 00:08:03,360 --> 00:08:06,700 It should be and if I come here to the class one, the class calls or 144 00:08:06,700 --> 00:08:09,220 it's highlighting there so it's unbalanced. 145 00:08:09,220 --> 00:08:11,530 So, we wanna balance that there we go. 146 00:08:11,530 --> 00:08:13,320 So, now we look at this. 147 00:08:13,320 --> 00:08:14,470 All right now, 148 00:08:14,470 --> 00:08:20,410 let's pop over to our hangman Java here in a press controlled lead jump at jShell. 149 00:08:20,410 --> 00:08:21,694 And let's do clear. 150 00:08:21,694 --> 00:08:27,636 And javac hangman dot java and 151 00:08:27,636 --> 00:08:30,730 java Hangman. 152 00:08:31,730 --> 00:08:32,950 Here we go. So we've compiled it. 153 00:08:32,950 --> 00:08:39,030 We're gonna run it and we should see our prompt Cool so enter a letter. 154 00:08:39,030 --> 00:08:39,680 Let's go ahead and 155 00:08:39,680 --> 00:08:43,900 guess what that's there will say t cuz right it passed in the game treehouse. 156 00:08:43,900 --> 00:08:46,040 Okay, cool we got a hit and then let's do it again. 157 00:08:46,040 --> 00:08:52,050 And let's put in z here and we missed awesome it's working. 158 00:08:52,050 --> 00:08:55,100 So let's go take a look at our story over here and 159 00:08:55,100 --> 00:08:59,210 see as a guesser I should be able to submit a guess so I can play a game. 160 00:08:59,210 --> 00:09:03,420 I think we just did that I'm going to go ahead and call that done. 161 00:09:05,020 --> 00:09:07,010 We completed our first story. 162 00:09:07,010 --> 00:09:10,780 Nothing quite like that feeling of moving something to the done column. 163 00:09:10,780 --> 00:09:12,110 Now it might not be much. 164 00:09:12,110 --> 00:09:15,610 But now we can start into the other stories that were blocked by this one, 165 00:09:15,610 --> 00:09:17,070 progress. 166 00:09:17,070 --> 00:09:19,390 Let's pick up the next one right after we do this quick exercise.