Prompting for Guesses9:19 with Craig Dennis
We will prompt for a guess from the user and use our game logic to store the value. We will complete the story: As a guesser, I should be able to submit a guess, so that I can play the game.
All right, so now we have the ability to apply a guess and keep track of hits and 0:00 misses, the time has come to actually take some input from the user. 0:05 Now this is probably going to look a little strange in our current state, 0:09 until we get some of the more detailed stories like the ones that shows 0:12 the current progress for example. 0:16 This is gonna be fairly useless except for a cool trick at the command line. 0:17 What I mean is this we haven't even shown users of our hang man app 0:22 what they are attempting to guess but here we are asking them for their guests. 0:25 Now this half baked app is definitely part of the development process. 0:29 Once we get some things working. 0:33 We can iterate on the concepts until we get to our working product. 0:34 But development isn't always Is linear and we have to start somewhere right 0:37 now as long as we complete all the stories on our test board. 0:42 We know we'll get there and we do that, because we've looked at them. 0:45 Okay, so let's get our prompter able to accept guesses. 0:48 Okay, so we currently don't have anything at all in our prompter class, and 0:53 I wanna get back to a statement that I made earlier. 0:57 About how someone else will be using your code. 1:01 So the problem here that we're gonna be writing is going to 1:05 use the game code that we just wrote. 1:08 Now, it's possible but we're on a team and we've made a team responsible for 1:10 prompting. 1:14 The front in portion of our app. 1:15 The prompter is going to need to know about our game object. 1:17 So let's do that. 1:20 Lets store a private variable that is an instance of our game. 1:22 So we'll say game game, and again we wanted to make that private. 1:27 We always want to start private, What 1:31 good is one of these prompters without a reference to our game. 1:36 So let's require one for instantiation by defining it in our constructor. 1:40 So again, let's make a constructor. 1:46 So I'll say, public and it's the same name as the class prompter. 1:48 And we're going to require a game. 1:54 So the game is the type game and we'll call it game. 1:56 And of course we have our little naming collision problem again. 2:00 So we wanna talk about the private variable game. 2:03 And set that equal to the argument that came in. 2:07 Wanted to show you another little trick really quick here in jshel. 2:11 So, if we have a string, so we have string example equals hello. 2:16 If I wanted to get a specific character out of that, 2:22 there is a Method on strings called charAt. 2:25 So, I can say charAt. 2:29 So, let's say that I wanted to get the first character. 2:30 So that's one, right? 2:33 No, zero is the first, I [LAUGH] forgot. 2:36 Baby years right? 2:38 So zero will get me a. 2:39 So the very first character that, awesome. 2:41 Okay, now, in our prompter object, 2:45 let's add a method that will prompt for 2:50 a guess, and it will return whether or not the guess was correct. 2:53 That sounds pretty boolean to me, right? 2:57 So, we want this to be public. 3:00 We wanna allow this to be called, so we'll say boolean, and that's gonna, again, 3:01 be whether or not the guess was correct Say promptForGuess. 3:06 [SOUND] Okay, now you've probably used the console object before which happens 3:10 to be one of many ways that's used to get input and output from a user. 3:16 But for the sake of learning new tricks, let's use a handy object called scanner. 3:21 Now a scanner lives in the different package. 3:27 Than where all the other things we've been importing are. 3:30 It lives in the Java util package, 3:32 and what that means is that we need to import it. 3:34 So let's do that. 3:37 Let's go up here, and we'll say import java.util.scanner. 3:38 And what this does is it allows us to have access to this class name, right? 3:44 So now, we can create a new one of these types. 3:50 So scanner, when we create it. 3:54 So let's make a new one. 3:56 So we'll say Scanner, which is the type. 3:58 And we'll name it scanner, our instance variable. 3:59 And we'll make a brand new one of those. 4:02 So we'll say new Scanner. 4:04 So scanner expects us to define where the input is coming from so 4:08 much like there's a system.out, there is also a system.in. 4:12 Okay, so now we need to prompt them, we need to write out to the screen so 4:19 we know how to do that already. 4:23 So we'll say system .out.print and I want to keep things on the same line and 4:25 I don't really need to format anything here. 4:30 So let's just go ahead and we'll just use the print command off of out. 4:32 It's just another one of these commands. 4:35 So as they enter a letter. 4:37 And we'll do a couple spaces there so that they have a space to enter it in, 4:39 Scanner has a bunch of helper methods that help Parse input. 4:45 Parse meaning read and figure out what it means there. 4:50 So, much like how console had the read line method. 4:52 Scanner uses one called next line 4:56 which allows us to read a line that again happens when they press enter. 4:59 So, let's do that. 5:03 So we'll store it. 5:04 We'll do string. 5:05 Guess input and 5:06 we'll do equals scanner.nextline and note case there. 5:09 Since our applied guess method only takes a char 5:16 we need to go get one of those using that method that we saw, right? 5:20 That char at. 5:24 So let's pull from this guess him put right we have a string. 5:25 So let's get the first character off of it. 5:29 I guessInput.charAt... that first character again is zero not one right, okay. 5:31 So now, we have the guess. 5:39 We need to see if that guess matches or not but 5:42 that's not the prompter's job right. 5:46 That's the game's job. 5:48 Now it's a good thing that we passed in our game object, right. 5:49 It's a good thing. 5:53 Why don't we just return what comes out of applied guess, right? 5:56 So apply guess is going to come and see if it exists and 6:00 it's going to return a true or a false. 6:03 So here we can do the same thing we could just return game.applyGuess. 6:04 And play that Guess where that finally 6:11 let's add some code to hang man dot java over here and let's use it. 6:15 So, let's make a new prompter. 6:20 So we know how to do that. 6:21 It's a type prompter. 6:22 It's a named prompter it's a new prompter. 6:24 And remember the way that we're getting the game in there is we're passing it 6:26 through the constructor through prompter constructor because what good is 6:30 a prompter without the game. 6:33 There we go game. 6:35 Okay, so let's just write some simple code just walk through what we just did. 6:36 So we'll say boolean is hit 6:41 prompter dot prompt for guess, and again that's how we run our code. 6:46 It's going to come in here. 6:52 It's gonna create a new scanner is going to print this line and 6:53 then it's going to wait here. 6:54 It's going to wait until we finish and then it will go and go on through. 6:56 So it's let's see. 6:59 So once we get back that result from whatever that we type then we'll say 7:01 if is hits and I'm gonna close that if right away. 7:05 And then we'll say system.out.println. 7:11 We got a hit. 7:15 And obviously this is not something we would ever say to the user because hit, 7:18 what does it mean to them but this is just for us to look at, all right. 7:22 So this is something that you might not have seen before. 7:25 Now we want to say what happens if they don't remember we need to do that 7:28 branching that we saw else. 7:31 We're gonna say 7:33 system.out.printline, okay. 7:36 So, look at this, I want to show this is a common problem that happened. 7:45 So, I forgot to put my closing brace there, 7:48 which I letting you in on the secret o what I'm trying to do here. 7:53 So look, this one close but this else I didn't close it but it's grabbing. 7:55 The methods it's actually the methods, but see it's not highlighting the method. 8:00 It should be and if I come here to the class one, the class calls or 8:03 it's highlighting there so it's unbalanced. 8:06 So, we wanna balance that there we go. 8:09 So, now we look at this. 8:11 All right now, 8:13 let's pop over to our hangman Java here in a press controlled lead jump at jShell. 8:14 And let's do clear. 8:20 And javac hangman dot java and 8:21 java Hangman. 8:27 Here we go. So we've compiled it. 8:31 We're gonna run it and we should see our prompt Cool so enter a letter. 8:32 Let's go ahead and 8:39 guess what that's there will say t cuz right it passed in the game treehouse. 8:39 Okay, cool we got a hit and then let's do it again. 8:43 And let's put in z here and we missed awesome it's working. 8:46 So let's go take a look at our story over here and 8:52 see as a guesser I should be able to submit a guess so I can play a game. 8:55 I think we just did that I'm going to go ahead and call that done. 8:59 We completed our first story. 9:05 Nothing quite like that feeling of moving something to the done column. 9:07 Now it might not be much. 9:10 But now we can start into the other stories that were blocked by this one, 9:12 progress. 9:15 Let's pick up the next one right after we do this quick exercise. 9:17
You need to sign up for Treehouse in order to download course files.Sign up