Current Progress8:22 with Craig Dennis
We will figure out how to inform the user about the current state of the Hangman puzzle. We will tackle the story: As a guesser, I should be presented with my current progress, so that I can make an educated guess.
Okay so the next story in the hopper is as a guesser I should be 0:00 presented with my current progress so that I can make an educated guess. 0:05 So current progress let's see. 0:08 Okay, so that'd be like blank slots to show what should be guessed and 0:11 what needs to be guessed. 0:16 So ,for example, our game instance has the answer treehouse right now. 0:18 So in order to show the progress when there was no guess we do something like 0:23 this and then when the guess was applied to the game using apply guess 0:26 it stores that in our hits or misses variables. 0:31 So let's assume that the guess for the letter T was applied. 0:34 It would go in our hit string. 0:38 So now when we were writing out these blanks. 0:40 We would just check to see if it's been guessed, is it in hits and if so 0:42 we'll write up the letter, otherwise we'll write out the blank like so. 0:46 Let's also guess h so now our hits variable has t and an h.. 0:50 All right so 0:55 we'll loop through each of the values in the answer the first letter is t. 0:57 Does our hits have t? 1:00 Well, it does, so we write it. 1:02 Next in our answer is r. 1:04 Is that it hits? 1:06 Nope, so we write a blank. 1:06 Next up as e. 1:08 Is that in our hits? 1:09 And one more time, it's not in our hits, and then finally, h. 1:10 So we'll just loop through each of the letters in the answer and 1:15 see if it has been guessed. 1:18 If it has been, we'll show it, otherwise, we'll show the blank. 1:19 First though, before we dive in, I just wanted to reiterate that because we 1:23 are hiding the implementation from the consumer, we can try several approaches. 1:26 As long as the results are the same, they won't even know that we made a change 1:31 That means that we can use any means necessary to accomplish our goals and 1:35 that's refreshing. 1:40 I mean perhaps there is a better way to do it what we're about to do but 1:41 we should feel empowered to innovate and not worry about getting things perfect 1:45 because we can always make it better, right? 1:48 Let's just focus on getting things working. 1:50 Okay, so let's move our current progress story into in progress. 1:54 And in order to do the story we're going to need to pick up a few more tricks. 2:00 So let's go ahead and start up your jshell. 2:04 Let's start again with an example here a String example = "hello";. 2:07 So there is a method on strings called to tochar array. 2:14 So let's do this example.toCharArray. 2:19 So as you can see here it's created an array, it broke apart the string into 2:25 a series or an array of characters, see each one of those is a character. 2:30 There's a pretty straightforward way to loop through each value in an array and 2:35 that is to use what's known as the enhanced for-loop. 2:41 So here let's watch. 2:45 So I'm going to clear the screen up here. 2:46 So if we say for, and here we say (char letter : and so 2:48 we've seen while loops before. 2:53 And this loop's pretty similar. 2:56 So this is the enhanced for. 2:58 So this is basically you can read this as in for 2:59 each letter in that array that we just looked at, example.toChaArray. 3:03 Open this up and jshell does a nice little, 3:11 see those dots means that we're in here. 3:13 So we'll do a system.out.println(" 3:15 We've got the letter, Letter and 3:20 so this is gonna loop through each of those values and I'm gonna close this. 3:28 It should run. 3:33 So we get the letter h e l l o. 3:35 So each one of these runs through the loop has printed that out. 3:36 Pretty cool, right? 3:41 And when it's done iterating through here, it just drops out. 3:42 So there's more of this in the teacher's notes. 3:45 Okay, so let's go make a method on our game class that 3:47 displays the current progress or at least gets it. 3:52 Let's get some space here. 3:59 So, what this method is going to do is it will return a string, so 4:01 it's definitely public. 4:05 It's going to return a string, and it's going to show dashes for 4:06 unguessed letters, or the letter if it's been guessed, right? 4:10 Like we just talked about. 4:14 So, get current progress. 4:15 Sounds good and doesn't need to take anything here because it's using 4:19 the internal state and let's see. 4:22 So let's start, we'll start with a temporary variable here we'll 4:24 call progress k will just make an empty string and was kind of add on to that. 4:31 All right, then let's loop through each letter, and the answer. 4:36 The way we use use that same for live with it. 4:40 So for (char letter : answer, and 4:42 answer.toCharArray.) Again. 4:46 So this is going to go through all of the letters, in the answer, and for 4:50 each one of those. 4:54 What we'll do is we'll figure out what we're going to display. 4:56 So let's go ahead and we'll set the default to display a dash. 5:00 Now if we actually have a hit, let's check to see if it's been guessed. 5:06 So remember the way that we can do that is we can look in hits. 5:10 So remember if we say hits.index of and 5:13 remember index will return negative one of it is not found. 5:20 So as long as it's found right again, that's a little inverted logic there but 5:25 as long as it's not a -1 5:30 negative one which means not found as long as not not found as long as found. 5:32 We're going to go ahead And we will instead set the display to the letter, 5:35 and then we will use or trick here to plus equal the display. 5:43 So, we'll basically appending each one of the displays there and 5:50 then finally after our four loops all done will return the progress, that make sense. 5:54 Okay and now because we want to actually display that current progress. 6:00 Let's go ahead and allow our prompter to display the progress by adding a method. 6:04 In prompter so we go to prompter. 6:09 And let's say Display progress. 6:11 Sytem.out.printf. 6:23 Try to solve and we'll just give a percent in and 6:27 we'll say game.getCurrentProgress. 6:34 Okay, so we'll save that. 6:43 So now we're accessing that method that we just wrote in over in Hangman, 6:46 let's use it. 6:50 So let's say here, we'll do a display. 6:52 Say prompter.displayProgress. 6:55 And then we'll prompt for the guess. 7:03 And then finally we'll say, prompter.displayProgress. 7:05 Cool, and now let's drop out of jshell and 7:14 we'll say clear in javac, 7:22 hangman.java and java Hangman. 7:27 Cool, so there's all of our dashes right so one, two, three, four, five, 7:34 six tree house right. 7:37 So, we gonna guess t. 7:38 Cool, so we got a hit which we know and that's not gonna be shown to them and 7:40 now look at there is our puzzle there. 7:44 So let's go ahead and do it one more time and I'll do an e this time so 7:46 we're not saving it. 7:51 You know each time it goes through. 7:52 So each one of the hits so we need to work on some sort of loop there. 7:53 Let's put an e and just to make sure. 7:56 There it is, all right so I think we should be able as a guesser I should 7:57 present with my current progress I can make an educated guess.Done, awesome nice. 8:02 We knocked out another one by looping through the letters and 8:08 the answer, we've gained the ability to show off where the character lives 8:10 within a series of characters. 8:13 We're on a roll but before we get too ahead of ourselves. 8:15 Let's take a quick swing at an exercise and 8:18 make sure that all of this is sinking in. 8:20
You need to sign up for Treehouse in order to download course files.Sign up