Determining If the Game is Won8:09 with Craig Dennis
We will use loops and chaining to solve the final story: As a guesser, I should be able to know when the game is solved or failed, so that I can acknowledge completion with celebration or tears.
Here it comes, the last story to complete our minimum viable product. 0:00 If you thought it felt good to move the stories to the Done column, 0:04 wait until you see what it feels like to move the last one. 0:07 So here it is. 0:10 As a guesser, I should be able to know when the game is won or lost so 0:12 that I can acknowledge completion with celebration or tears. 0:16 All right, we can definitely do this. 0:19 So we should provide a method that returns whether or 0:21 not the game [SOUND] was completed successfully. 0:23 So how do we do that? 0:25 Well, one way to do it is just to check and see if there's any blanks left. 0:27 We should just start with this approach. 0:32 [SOUND] If there is a blank, the game has not been won. 0:33 And then we should have the prompter display the final outcome, 0:35 spreading the good or bad news to our users. 0:39 Here we go. 0:41 All right, let's move our final ticket over to the In Progress column. 0:44 We need to let people know if they won or not. 0:48 So, let's go to Game really quick, 0:51 let's stop our program from running, and let's get in here. 0:54 Let's say, we need to let the game know if it's won or not. 0:59 So let's just go ahead and let's put it here. 1:05 Let's say, public and its true and false, right? 1:08 So boolean and isWon, and remember is, is the, 1:12 like the getter, it's a computed getter, right? 1:16 So, we're gonna say isWon, where normally if it was not boolean, 1:21 it would have been get for the stringer and integer or something. 1:24 But it's is because that's the format of the reason. 1:27 And we're going to calculate our value, so let's just use that 1:30 existing method like we talked about, so as I getCurrentProgress. 1:34 Now, I could store this in a variable, 1:39 but I know that this is going to return a string. 1:42 And so that string has a method on it that allows us to see if a character exists. 1:46 What was that called again, indexOf. 1:52 So, what I can do is I can do a .indexOf, right? 1:54 So it's method chaining. 1:59 And we wanna see if there is in fact, any of those dashes in there. 2:00 It's probably a good place to make a constant, 2:05 if we wanted to make this a little bit better. 2:08 And then we want to check and see if it was, in fact, not found. 2:11 So we're gonna say, is it not found? 2:18 If that's not found, they won. 2:20 That's the logic theory. 2:22 Cool. 2:23 So this returns a string. 2:24 And then that string is checked to see if it's dashes. 2:26 And then we see, if there are any dashes in there. 2:29 Then they have, in fact, not won, cuz their stuff left to guess. 2:33 Okay, so let's go use it. 2:37 In our main program here, in Hangman.java. 2:38 We want the game to stop looping once it's been won. 2:41 Now this logic might be a little challenging here, but 2:47 let's think it through. 2:50 So currently the loop goes as long as there are remaining tries. 2:51 So we wanna add another condition, and it really is this. 2:55 As long as the game hasn't been won. 2:57 Okay, so while there are remaining tries, 3:00 and we just wrote that game is won, but 3:05 what we want is not won. 3:10 It's so hard to not do a bar impersonation here, not. 3:13 Sorry, so that's what we want. 3:18 Where both of these are true, keep going, right? 3:21 So we wanna keep going where both of these are true. 3:24 When this is checked, it will keep going and each one of these will go. 3:28 So when it's all done, we wanna display the outcome, right? 3:31 So, we would do something like this, right? 3:35 So it's a prompter.displayOutcome. 3:40 Now, we haven't written that method yet, but I bet you could. 3:43 Why don't you go ahead and pause me? 3:48 What we're gonna do before you pause, let's get the instructions. 3:51 What we're gonna do here is we're gonna try to show if they've won, 3:55 we're gonna say, congratulations. 3:58 And if they failed, we're gonna say, sorry, here's what the answer was. 3:59 So why don't you go write that method? 4:03 And notice, it doesn't return anything. 4:05 Okay, go ahead, pause me and come back when you're ready. 4:08 Cool, this is how I did it. 4:12 So, under displayProgress here, I added a new method. 4:16 And of course, it doesn't return anything. 4:19 So that's a void. 4:21 Display outcome. 4:22 All right, so if the game was won, we wanna say, congratulations. 4:28 So let's say, if game is won. 4:31 I'm gonna close my if and I'm gonna say, else and 4:33 then that's where we're gonna put this, add here. 4:36 So here we go. 4:39 So, we want to say system.out.printf. 4:41 Congratulations, you won with, let's go ahead and 4:51 say how many tries they had remaining. 4:55 With %d tries remaining. 4:58 Again, we'll extra feature there. 5:01 Wasn't asked for. 5:03 Maybe we shouldn't do that. 5:04 We'll see what they think of the demo. 5:06 All right, so you won with that many tries remaining, 5:09 and we're going to say, game.getRemainingTries. 5:15 Otherwise, we're 5:20 going to .out.printf, 5:25 Bummer, the word was %s. 5:31 And I'm gonna make a sad face there, we'll say, %n. 5:37 Okay, and then we need to get access to the answer, don't we? 5:42 Prompter doesn't have access to the answer. 5:47 So I'm gonna write a getAnswer method over here, sounds a little tricky wasn't it? 5:49 So over here in Game, I'm gonna make a getter for getAnswer, right? 5:54 So our answer is still private but we can add a getter, 5:59 want to add a getter up here at the top. 6:04 So we'll say our public, 6:06 we want this to be public. 6:11 And it's a string and we wanna say, 6:15 getAnswer, and it doesn't expect anything, and we are going to return the answer. 6:17 Cool, let's see how I did. 6:21 I'm sure you did great, clear && javac 6:23 Hangman.java && java Hangman. 6:29 Let's test it, T-R-E-H-O-U-S. 6:36 We did it with seven tries remaining, awesome. 6:42 Okay, let's try to fail really quick. 6:45 A-B-C-D-E-F-G-I. 6:46 Bummer, the word history was treehouse, sad face. 6:54 My gosh, we did it, the sprint is over. 6:57 [APPLAUSE] >> Wohoo, we just did it. 7:03 We got a working Hangman MVP. 7:07 And what a great job of separating the prompting from the game logic. 7:09 Now, we could do a web-based version of this, or use it in an Android app, or 7:13 play it on a WiFi enabled refrigerator, you get the point. 7:16 The other great thing about this game class that we created is that 7:20 the implementation details are hidden. 7:23 We absolutely could make this game more efficient as long as we kept up our end 7:25 of the deal, and we kept the interface, or the exposed methods and functionality. 7:29 If we kept those the same, every single one of the users of this class, 7:33 the console, the website, the Android app, the refrigerator, they'd all benefit. 7:36 Also, because you did a great job of naming your methods, almost any English 7:40 speaker could pick up that class and know exactly what it's expected to do without 7:45 ever seeing the methods code, just like that radio example we talked about. 7:49 Great job harnessing the power of objects. 7:54 One more thing I told you that I'd show you was how to pass an arguments to 7:57 the command line. 8:00 I mean after all, if you're gonna play this game, 8:01 you probably want to eventually use a different word than treehouse, right? 8:03 First though, an exercise. 8:07
You need to sign up for Treehouse in order to download course files.Sign up