1 00:00:00,630 --> 00:00:05,160 Okay so the next story in the hopper is as a guesser I should be 2 00:00:05,160 --> 00:00:08,798 presented with my current progress so that I can make an educated guess. 3 00:00:08,798 --> 00:00:11,990 So current progress let's see. 4 00:00:11,990 --> 00:00:16,380 Okay, so that'd be like blank slots to show what should be guessed and 5 00:00:16,380 --> 00:00:17,580 what needs to be guessed. 6 00:00:18,620 --> 00:00:23,140 So ,for example, our game instance has the answer treehouse right now. 7 00:00:23,140 --> 00:00:26,470 So in order to show the progress when there was no guess we do something like 8 00:00:26,470 --> 00:00:31,010 this and then when the guess was applied to the game using apply guess 9 00:00:31,010 --> 00:00:34,240 it stores that in our hits or misses variables. 10 00:00:34,240 --> 00:00:38,140 So let's assume that the guess for the letter T was applied. 11 00:00:38,140 --> 00:00:40,120 It would go in our hit string. 12 00:00:40,120 --> 00:00:42,660 So now when we were writing out these blanks. 13 00:00:42,660 --> 00:00:46,600 We would just check to see if it's been guessed, is it in hits and if so 14 00:00:46,600 --> 00:00:50,670 we'll write up the letter, otherwise we'll write out the blank like so. 15 00:00:50,670 --> 00:00:55,700 Let's also guess h so now our hits variable has t and an h.. 16 00:00:55,700 --> 00:00:57,170 All right so 17 00:00:57,170 --> 00:01:00,970 we'll loop through each of the values in the answer the first letter is t. 18 00:01:00,970 --> 00:01:02,460 Does our hits have t? 19 00:01:02,460 --> 00:01:04,320 Well, it does, so we write it. 20 00:01:04,320 --> 00:01:06,010 Next in our answer is r. 21 00:01:06,010 --> 00:01:06,870 Is that it hits? 22 00:01:06,870 --> 00:01:08,620 Nope, so we write a blank. 23 00:01:08,620 --> 00:01:09,380 Next up as e. 24 00:01:09,380 --> 00:01:10,350 Is that in our hits? 25 00:01:10,350 --> 00:01:15,080 And one more time, it's not in our hits, and then finally, h. 26 00:01:15,080 --> 00:01:18,070 So we'll just loop through each of the letters in the answer and 27 00:01:18,070 --> 00:01:19,650 see if it has been guessed. 28 00:01:19,650 --> 00:01:21,990 If it has been, we'll show it, otherwise, we'll show the blank. 29 00:01:23,110 --> 00:01:26,930 First though, before we dive in, I just wanted to reiterate that because we 30 00:01:26,930 --> 00:01:31,180 are hiding the implementation from the consumer, we can try several approaches. 31 00:01:31,180 --> 00:01:34,390 As long as the results are the same, they won't even know that we made a change 32 00:01:35,620 --> 00:01:40,080 That means that we can use any means necessary to accomplish our goals and 33 00:01:40,080 --> 00:01:41,140 that's refreshing. 34 00:01:41,140 --> 00:01:45,030 I mean perhaps there is a better way to do it what we're about to do but 35 00:01:45,030 --> 00:01:48,730 we should feel empowered to innovate and not worry about getting things perfect 36 00:01:48,730 --> 00:01:50,880 because we can always make it better, right? 37 00:01:50,880 --> 00:01:52,559 Let's just focus on getting things working. 38 00:01:54,220 --> 00:02:00,200 Okay, so let's move our current progress story into in progress. 39 00:02:00,200 --> 00:02:04,170 And in order to do the story we're going to need to pick up a few more tricks. 40 00:02:04,170 --> 00:02:07,080 So let's go ahead and start up your jshell. 41 00:02:07,080 --> 00:02:14,500 Let's start again with an example here a String example = "hello";. 42 00:02:14,500 --> 00:02:19,580 So there is a method on strings called to tochar array. 43 00:02:19,580 --> 00:02:25,640 So let's do this example.toCharArray. 44 00:02:25,640 --> 00:02:30,663 So as you can see here it's created an array, it broke apart the string into 45 00:02:30,663 --> 00:02:35,557 a series or an array of characters, see each one of those is a character. 46 00:02:35,557 --> 00:02:41,207 There's a pretty straightforward way to loop through each value in an array and 47 00:02:41,207 --> 00:02:45,210 that is to use what's known as the enhanced for-loop. 48 00:02:45,210 --> 00:02:46,640 So here let's watch. 49 00:02:46,640 --> 00:02:48,691 So I'm going to clear the screen up here. 50 00:02:48,691 --> 00:02:53,256 So if we say for, and here we say (char letter : and so 51 00:02:53,256 --> 00:02:56,110 we've seen while loops before. 52 00:02:56,110 --> 00:02:58,010 And this loop's pretty similar. 53 00:02:58,010 --> 00:02:59,440 So this is the enhanced for. 54 00:02:59,440 --> 00:03:03,963 So this is basically you can read this as in for 55 00:03:03,963 --> 00:03:11,353 each letter in that array that we just looked at, example.toChaArray. 56 00:03:11,353 --> 00:03:13,107 Open this up and jshell does a nice little, 57 00:03:13,107 --> 00:03:15,270 see those dots means that we're in here. 58 00:03:15,270 --> 00:03:20,894 So we'll do a system.out.println(" 59 00:03:20,894 --> 00:03:28,309 We've got the letter, Letter and 60 00:03:28,309 --> 00:03:33,080 so this is gonna loop through each of those values and I'm gonna close this. 61 00:03:33,080 --> 00:03:33,640 It should run. 62 00:03:35,200 --> 00:03:36,970 So we get the letter h e l l o. 63 00:03:36,970 --> 00:03:41,190 So each one of these runs through the loop has printed that out. 64 00:03:41,190 --> 00:03:42,490 Pretty cool, right? 65 00:03:42,490 --> 00:03:45,940 And when it's done iterating through here, it just drops out. 66 00:03:45,940 --> 00:03:47,750 So there's more of this in the teacher's notes. 67 00:03:47,750 --> 00:03:52,012 Okay, so let's go make a method on our game class that 68 00:03:52,012 --> 00:03:56,104 displays the current progress or at least gets it. 69 00:03:59,953 --> 00:04:01,760 Let's get some space here. 70 00:04:01,760 --> 00:04:05,380 So, what this method is going to do is it will return a string, so 71 00:04:05,380 --> 00:04:06,210 it's definitely public. 72 00:04:06,210 --> 00:04:10,050 It's going to return a string, and it's going to show dashes for 73 00:04:10,050 --> 00:04:14,080 unguessed letters, or the letter if it's been guessed, right? 74 00:04:14,080 --> 00:04:15,270 Like we just talked about. 75 00:04:15,270 --> 00:04:18,450 So, get current progress. 76 00:04:19,935 --> 00:04:22,840 Sounds good and doesn't need to take anything here because it's using 77 00:04:22,840 --> 00:04:24,998 the internal state and let's see. 78 00:04:24,998 --> 00:04:31,980 So let's start, we'll start with a temporary variable here we'll 79 00:04:31,980 --> 00:04:36,350 call progress k will just make an empty string and was kind of add on to that. 80 00:04:36,350 --> 00:04:40,450 All right, then let's loop through each letter, and the answer. 81 00:04:40,450 --> 00:04:42,550 The way we use use that same for live with it. 82 00:04:42,550 --> 00:04:46,480 So for (char letter : answer, and 83 00:04:46,480 --> 00:04:50,417 answer.toCharArray.) Again. 84 00:04:50,417 --> 00:04:54,624 So this is going to go through all of the letters, in the answer, and for 85 00:04:54,624 --> 00:04:55,760 each one of those. 86 00:04:56,990 --> 00:05:00,726 What we'll do is we'll figure out what we're going to display. 87 00:05:00,726 --> 00:05:04,770 So let's go ahead and we'll set the default to display a dash. 88 00:05:06,610 --> 00:05:10,640 Now if we actually have a hit, let's check to see if it's been guessed. 89 00:05:10,640 --> 00:05:13,760 So remember the way that we can do that is we can look in hits. 90 00:05:13,760 --> 00:05:20,350 So remember if we say hits.index of and 91 00:05:20,350 --> 00:05:25,490 remember index will return negative one of it is not found. 92 00:05:25,490 --> 00:05:30,100 So as long as it's found right again, that's a little inverted logic there but 93 00:05:30,100 --> 00:05:32,740 as long as it's not a -1 94 00:05:32,740 --> 00:05:35,660 negative one which means not found as long as not not found as long as found. 95 00:05:35,660 --> 00:05:43,310 We're going to go ahead And we will instead set the display to the letter, 96 00:05:43,310 --> 00:05:50,650 and then we will use or trick here to plus equal the display. 97 00:05:50,650 --> 00:05:54,180 So, we'll basically appending each one of the displays there and 98 00:05:54,180 --> 00:05:59,520 then finally after our four loops all done will return the progress, that make sense. 99 00:06:00,640 --> 00:06:04,710 Okay and now because we want to actually display that current progress. 100 00:06:04,710 --> 00:06:09,190 Let's go ahead and allow our prompter to display the progress by adding a method. 101 00:06:09,190 --> 00:06:11,740 In prompter so we go to prompter. 102 00:06:11,740 --> 00:06:19,121 And let's say Display progress. 103 00:06:23,643 --> 00:06:27,760 Sytem.out.printf. 104 00:06:27,760 --> 00:06:34,666 Try to solve and we'll just give a percent in and 105 00:06:34,666 --> 00:06:40,717 we'll say game.getCurrentProgress. 106 00:06:43,453 --> 00:06:45,410 Okay, so we'll save that. 107 00:06:46,630 --> 00:06:50,730 So now we're accessing that method that we just wrote in over in Hangman, 108 00:06:50,730 --> 00:06:52,130 let's use it. 109 00:06:52,130 --> 00:06:55,918 So let's say here, we'll do a display. 110 00:06:55,918 --> 00:07:03,338 Say prompter.displayProgress. 111 00:07:03,338 --> 00:07:05,740 And then we'll prompt for the guess. 112 00:07:05,740 --> 00:07:11,063 And then finally we'll say, prompter.displayProgress. 113 00:07:14,137 --> 00:07:22,525 Cool, and now let's drop out of jshell and 114 00:07:22,525 --> 00:07:27,884 we'll say clear in javac, 115 00:07:27,884 --> 00:07:34,650 hangman.java and java Hangman. 116 00:07:34,650 --> 00:07:37,050 Cool, so there's all of our dashes right so one, two, three, four, five, 117 00:07:37,050 --> 00:07:38,410 six tree house right. 118 00:07:38,410 --> 00:07:39,060 So, we gonna guess t. 119 00:07:40,830 --> 00:07:44,240 Cool, so we got a hit which we know and that's not gonna be shown to them and 120 00:07:44,240 --> 00:07:46,540 now look at there is our puzzle there. 121 00:07:46,540 --> 00:07:51,290 So let's go ahead and do it one more time and I'll do an e this time so 122 00:07:51,290 --> 00:07:52,050 we're not saving it. 123 00:07:52,050 --> 00:07:53,180 You know each time it goes through. 124 00:07:53,180 --> 00:07:56,100 So each one of the hits so we need to work on some sort of loop there. 125 00:07:56,100 --> 00:07:57,980 Let's put an e and just to make sure. 126 00:07:57,980 --> 00:08:02,614 There it is, all right so I think we should be able as a guesser I should 127 00:08:02,614 --> 00:08:08,120 present with my current progress I can make an educated guess.Done, awesome nice. 128 00:08:08,120 --> 00:08:10,410 We knocked out another one by looping through the letters and 129 00:08:10,410 --> 00:08:13,960 the answer, we've gained the ability to show off where the character lives 130 00:08:13,960 --> 00:08:15,980 within a series of characters. 131 00:08:15,980 --> 00:08:18,610 We're on a roll but before we get too ahead of ourselves. 132 00:08:18,610 --> 00:08:20,360 Let's take a quick swing at an exercise and 133 00:08:20,360 --> 00:08:21,790 make sure that all of this is sinking in.