Remaining Tries7:04 with Craig Dennis
We will keep track of how many wrong guesses have been made when attempting to solve the puzzle, and display the amount of tries remaining. We will wrap up the user story: As a guesser, I should know how many remaining tries I have left, so I am encouraged to be cautious.
All right. 0:00 So our next ticket in order of priority is this, as a guesser I should 0:01 know how many remaining tries I have left so I am encouraged to be cautious. 0:06 So basically this implies that we're going to need to allow multiple guesses through 0:11 some sort of loop until they run out of tries. 0:15 Now in order to do that we'll have to set a limit. 0:18 That limit seems like something we want to make publicly available and 0:22 it's related to all games and it shouldn't change. 0:26 We know how to do that. 0:29 Let's make a constant. 0:30 Let's sketch out the remaining tries logic real quick. 0:32 So a standard hangman game allows you to miss seven times. 0:36 So let's look at a game in progress. 0:40 So a t and an h were a hit and z and w were incorrect guesses. 0:42 So therefore if we count how many misses they've made and subtract that 0:48 from the total amount of tries, we should know how many tries remain. 0:52 Here there are seven misses allowed and there have been two bad guess so 0:56 seven minus two is five. 1:00 So there are five tries remaining and if we guess why it appends the misses and 1:02 now there are three misses and 7- 3 = 4. 1:07 So there is four guesses left. 1:10 All right let's do this. 1:12 Okay so let's move our remaining tries take it into in progress. 1:14 All right, so let's go and set our guess limit. 1:21 This is definitely public and we want it to be true of all games. 1:26 So let's expose it off of the class. 1:31 So we'll make that static. 1:33 And we definitely don't want to change, right. 1:35 I don't want anybody cheating. 1:38 So that's final. 1:39 It's an integer and because it's a constant, 1:40 let's make the name all capitals. 1:42 So we'll do max under misses. 1:45 So like we said seven is the standard number of tries. 1:48 Now let's write some code that exposes how many tries are left. 1:52 I'm gonna start Jshell up down here. 1:56 So again that code was max misses allowed minus how many misses they have made. 1:57 But, how do we count the amount of misses? 2:05 So, because it is a string 2:08 we can use a new method that you might not have seen before. 2:09 So we'll say string.example = 'hello'. 2:11 You want .example. 2:19 So now if we do example.link, we'll see that there were 5. 2:27 So that will work because we'll know how many misses are there because our string 2:33 will be that long. 2:37 Cool. 2:39 So in the game object let's go ahead and make a new computed property right. 2:39 So we want to be public because we want people to use it. 2:45 And it's going to return a number and 2:49 let's call it getRemainingTtries and this lines up with the user story. 2:52 And so what do we wanna do? 2:56 We wanna return here, 2:58 we wanna return that max misses that we just made, that's how many were there, 2:59 minus the misses and because it's a string we'll just use that length property there. 3:04 Cool. 3:09 Okay so now that we have it, let's make our display progress get upgraded. 3:10 Let's put it in there. 3:15 So let's say you have %d. 3:17 And now %d is where you wanna show numbers in a string formatter. 3:22 I put some more in the teacher's notes about this. 3:27 But, you want %D, and D stands for decimal there. 3:29 So, you have %D tries left to solve and 3:32 then here we go and then %S will still stay there. 3:38 So, if there's two string formatters, the first will say game.getRemainingTries and 3:41 we'll call that into a comma. 3:48 There we go. 3:49 So now it will push that remaining tries count that we have in there. 3:50 So now let's build a loop that will just keep asking 3:54 while there are remaining attempts. 3:58 Now the specific story doesn't talk about what to do with a win. 4:01 So let's not handle that case yet. 4:06 Let's let the next person who picks up the next story deal with that. 4:08 Now this is a good development practice. 4:11 Limit your scope to the story that you're working on. 4:13 This way you don't have to run into other developers who might pick up the next 4:15 ticket and implement things differently than you might have. 4:18 Let's see what does that code look like. 4:22 Let's go ahead and let's get rid of this testing code that we're doing here before. 4:23 I gonna get rid of story aim and story in the variable. 4:29 So we have these two things and we wanna loop. 4:32 So what can we say, we said while we've got remaining tries, 4:34 so while game.getRemainingTries is greater than zero. 4:41 Right. 4:49 As long as there is more than zero tries left you can still keep playing. 4:49 So we'll go ahead and 4:53 move these in here. 4:57 Here we go, that should do it. 5:08 Let's bring this up, let's hit CTRL D, and let's see if it's here. 5:09 It's not, okay. 5:17 So, we'll say clear & javaC 5:17 Hangman.java & java hangman. 5:22 Okay here we go. 5:28 Awesome so it says we have seven tries left to solve which is right. 5:31 And let's see, let's guess T. 5:34 Cool so it didn't go down because we didn't miss but let's miss. 5:37 And then let's miss again. 5:40 And let's miss again, and see they're going down and it's right, so 5:42 now if we guess it should stay at four, it's that. 5:46 I just can't get this one. 5:49 What is it? 5:50 It's a Q, it's probably a Q. 5:51 You always guess Q, right? 5:52 So, uh-oh, oop there you go. 5:54 So it ran out. 5:59 So the loop finish when we're done. 5:59 I think we can go ahead and consider this one done-zo. 6:01 Hey, it's starting to look like a game and we're so close, only two stories left. 6:05 A good lesson that we learned here is that by only completing the task as required by 6:12 the story we had a good stopping point. 6:17 In case project scope or requirements for our NVP changed. 6:19 We're still on target, but 6:22 we could in fact bring in more people if we hit a time crunch. 6:24 Breaking down stories in the sane technical tasks takes a certain amount of 6:27 effort and finesse. 6:30 But it almost always pays off in the end. 6:31 Speaking of a stopping point, you've done an awful lot in this stage and 6:34 I think we should wrap this one up. 6:38 We've got an app-ish looking app and you deserve to take a break. 6:39 Give yourself a huge pat on the back. 6:42 You're mastering using Java objects and picking up some more core Java skills. 6:44 You're doing great. 6:48 Way to stick with it. 6:50 I've thrown a lot of information at you and you're processing it. 6:51 I am super impressed. 6:54 Of course though, you don't need me to tell you all that, because you've been 6:55 proving it yourself by rocking these exercises I keep throwing at you. 6:59 Like this final one for this stage. 7:02
You need to sign up for Treehouse in order to download course files.Sign up