1 00:00:00,300 --> 00:00:03,900 Okay, so let's take a stab at the first story which is this. 2 00:00:03,900 --> 00:00:07,940 As a guesser, I should be able to submit a guess, so that I can play the game. 3 00:00:09,340 --> 00:00:11,840 Sometimes these beginning stories are a little silly. 4 00:00:11,840 --> 00:00:15,140 Now obviously a guesser is gonna guess, but it needs to be stated so 5 00:00:15,140 --> 00:00:16,900 that development will happen. 6 00:00:16,900 --> 00:00:18,520 Okay, so let's think about this a bit. 7 00:00:19,840 --> 00:00:23,300 Our game object currently holds what the answer is. 8 00:00:23,300 --> 00:00:26,340 And now we want to start having it store what 9 00:00:26,340 --> 00:00:28,840 guesses have been made towards that answer. 10 00:00:28,840 --> 00:00:34,190 So this story is requiring that the game allows the submission of a guess. 11 00:00:34,190 --> 00:00:37,580 Sounds like we might need to create a method that accepts a single letter. 12 00:00:37,580 --> 00:00:39,690 How about applyGuess. 13 00:00:39,690 --> 00:00:44,120 So with that guess, we should be able to tell if that guess is a hit, 14 00:00:44,120 --> 00:00:48,080 like it's in the answer, or if it's a miss if it isn't. 15 00:00:48,080 --> 00:00:50,910 By storing this information in the game instance, 16 00:00:50,910 --> 00:00:53,850 others could then apply guesses from whatever means. 17 00:00:55,330 --> 00:00:57,790 Let's get it working real quick, and then it should start clicking. 18 00:00:57,790 --> 00:00:59,551 Here we go. Okay, 19 00:00:59,551 --> 00:01:04,510 so let's move our story into the In Progress column. 20 00:01:04,510 --> 00:01:07,661 So first things first, I want to pop into JShell and 21 00:01:07,661 --> 00:01:12,667 introduce you to a new primitive data type that you might have met before, char. 22 00:01:12,667 --> 00:01:17,557 So the char data type is used to represent a single character, in fact, 23 00:01:17,557 --> 00:01:19,683 char is short for a character. 24 00:01:19,683 --> 00:01:23,003 Use a char as the data type, letter =, and 25 00:01:23,003 --> 00:01:27,600 here I want you to note that we're using single quotes. 26 00:01:27,600 --> 00:01:31,940 Now other languages can use the double quotes and single quote interchangeably. 27 00:01:31,940 --> 00:01:38,380 But in Java double is for string literals and single is for char literals. 28 00:01:39,840 --> 00:01:43,000 So I've used this analogy about strings before. 29 00:01:43,000 --> 00:01:46,550 But I want you to imagine a string as a group of characters 30 00:01:46,550 --> 00:01:49,490 strung together like a banner at a party. 31 00:01:49,490 --> 00:01:51,910 So there are a few ways to check and 32 00:01:51,910 --> 00:01:56,730 see if a string has a specific character in its string of characters. 33 00:01:56,730 --> 00:01:59,628 Here's one that we'll use, so let's make an example, so let's say, 34 00:01:59,628 --> 00:02:04,180 string example = "Hello", and 35 00:02:04,180 --> 00:02:08,270 of course, now pay more attention though there's double quotes there. 36 00:02:08,270 --> 00:02:11,660 Okay, and there are a lots of ways to do this, 37 00:02:11,660 --> 00:02:14,080 the one that we're going to use is called indexOf. 38 00:02:14,080 --> 00:02:21,470 So, we're gonna say, example.indexOf, and I'm going to look for the character e. 39 00:02:23,500 --> 00:02:25,540 It tells us that it is at 1. 40 00:02:25,540 --> 00:02:30,320 Note how it returned one but it really seems like it should be two, right? 41 00:02:30,320 --> 00:02:32,917 One, two, I would have assumed that. 42 00:02:32,917 --> 00:02:36,769 Now, this is because when indexing in Java and many other languages, 43 00:02:36,769 --> 00:02:38,710 counting always starts at zero. 44 00:02:38,710 --> 00:02:40,800 Now, this seems a little bit weird. 45 00:02:40,800 --> 00:02:43,780 And I will try to cover this quite a bit cuz I know it's something that people 46 00:02:43,780 --> 00:02:44,840 struggle with. 47 00:02:44,840 --> 00:02:48,180 A way that I remember this is, by thinking of babies ages. 48 00:02:48,180 --> 00:02:51,848 When babies are born we often talk about their first year. 49 00:02:51,848 --> 00:02:55,480 Yet, when we're asked how old they are, we always answer in months. 50 00:02:55,480 --> 00:02:57,850 And that's because zero years old sounds weird. 51 00:02:57,850 --> 00:02:58,890 How old's your baby? 52 00:02:58,890 --> 00:02:59,890 Zero years old. 53 00:02:59,890 --> 00:03:00,920 We don't say that. 54 00:03:00,920 --> 00:03:03,290 So after their first year we start saying one year old and 55 00:03:03,290 --> 00:03:06,210 then we say two year old but zero just sounds weird, right? 56 00:03:06,210 --> 00:03:10,430 Well this is true here too, we often say that the first element, but 57 00:03:10,430 --> 00:03:13,670 in reality it's really the zeroth index. 58 00:03:13,670 --> 00:03:19,830 So if we look at example.indexOf and we push in an H. 59 00:03:21,270 --> 00:03:25,840 Character is, I'm gonna do Ctrl + L, so that we're clean. 60 00:03:25,840 --> 00:03:33,150 So example.indexOf('H') in Hello, will be the zeroith. 61 00:03:33,150 --> 00:03:34,650 So what happens when it's not found? 62 00:03:34,650 --> 00:03:37,970 So if you say example.indexOf, and 63 00:03:37,970 --> 00:03:43,872 z is definitely not in there, so what happens when z is not there? 64 00:03:45,685 --> 00:03:50,580 So as long as the index of the char is greater or equal to 0, it's in there. 65 00:03:50,580 --> 00:03:52,640 So you can kind of do that expression like this. 66 00:03:52,640 --> 00:03:59,468 So we can say, example.indexOf('o'), which is in there, right? 67 00:03:59,468 --> 00:04:04,570 If that's greater or equal to 0, we know it'll return a true, right? 68 00:04:04,570 --> 00:04:07,170 And the same is true if it's missing. 69 00:04:07,170 --> 00:04:10,967 So example indexOf, and we do y, 70 00:04:10,967 --> 00:04:15,160 we say greater than or equal to 0. 71 00:04:15,160 --> 00:04:17,120 It's going to tell us that that's false. 72 00:04:17,120 --> 00:04:21,390 So the other way to check, and this is kind of more succinct actually, 73 00:04:21,390 --> 00:04:30,062 if you say, example.indexOf('e'), is not equal to -1, right? 74 00:04:30,062 --> 00:04:32,170 Cuz -1 is saying that it's not in there. 75 00:04:32,170 --> 00:04:33,935 So this is like basically saying if it's not found. 76 00:04:33,935 --> 00:04:37,760 It's actually saying if it's not not found. 77 00:04:37,760 --> 00:04:41,840 One thing you might not of come across yet is string concatenation. 78 00:04:41,840 --> 00:04:45,167 So you can combine strings using the plus sign, 79 00:04:45,167 --> 00:04:47,923 kind of like add this word to this word. 80 00:04:47,923 --> 00:04:51,883 Like so, so if I say, 81 00:04:51,883 --> 00:04:57,828 example = example + "Wor". 82 00:04:57,828 --> 00:05:01,680 What's gonna happen it's gonna append example, just like we saw before. 83 00:05:01,680 --> 00:05:05,515 You take the variable and another variable and apply the plus to it. 84 00:05:05,515 --> 00:05:08,480 And you can also concatenate chars. 85 00:05:08,480 --> 00:05:16,290 So we'll say example = example + 'l', the single character l. 86 00:05:17,660 --> 00:05:18,380 Cool, so there we go. 87 00:05:18,380 --> 00:05:21,730 And that shortcut that we learned about adding integers also works. 88 00:05:21,730 --> 00:05:25,650 So if I say, example += 'd'. 89 00:05:25,650 --> 00:05:27,400 It's going to add d to example. 90 00:05:28,550 --> 00:05:29,840 So now example has been changed. 91 00:05:31,430 --> 00:05:32,480 Cool, so 92 00:05:32,480 --> 00:05:38,070 armed with that info, let's use it to solve our user story of applying guesses. 93 00:05:38,070 --> 00:05:43,970 So we want to be able to accept a guess and store if it was a hit or a miss. 94 00:05:43,970 --> 00:05:46,000 So why don't we make that state. 95 00:05:46,000 --> 00:05:48,160 Since strings can be concatenated and 96 00:05:48,160 --> 00:05:52,030 checked, why don't we store that info in strings themselves. 97 00:05:52,030 --> 00:05:55,140 So let's go to the game logic and 98 00:05:55,140 --> 00:05:58,842 in here, let's say private, cuz we always start private, right? 99 00:05:58,842 --> 00:06:03,220 We're gonna make a new string, and will store hits. 100 00:06:03,220 --> 00:06:05,591 And then misses, that's two separate strings. 101 00:06:07,694 --> 00:06:12,640 And inside of our game constructor, we will initialize this. 102 00:06:12,640 --> 00:06:16,286 So we'll say hits and misses. 103 00:06:19,100 --> 00:06:21,250 Cool, so now those are initialized and they're private. 104 00:06:23,636 --> 00:06:25,960 I'm gonna scroll this down a little bit. 105 00:06:25,960 --> 00:06:31,000 So now let's add a method that will allow us to apply a guess towards the answer. 106 00:06:31,000 --> 00:06:34,010 So let's do this, we know it's public. 107 00:06:34,010 --> 00:06:37,770 We want people to be able to do a public, and let's return whether or 108 00:06:37,770 --> 00:06:38,870 not it was a hit. 109 00:06:38,870 --> 00:06:41,059 So we'll use a Boolean return value, right? 110 00:06:41,059 --> 00:06:44,700 We'll say, you apply the guess you know if it matched or not. 111 00:06:44,700 --> 00:06:48,740 And this way consumers of the objects will know if they got the guess right. 112 00:06:48,740 --> 00:06:50,040 So we'll call applyGuess. 113 00:06:51,500 --> 00:06:55,010 And let's see, we wanna take in a letter. 114 00:06:55,010 --> 00:06:57,530 So let's make that a char letter, right? 115 00:06:57,530 --> 00:06:58,600 A single letter is what we want. 116 00:06:59,758 --> 00:07:03,330 So we're gonna start out and we'll say boolean is it a hit, 117 00:07:03,330 --> 00:07:06,114 and what we will do is we'll see if it's in the answer. 118 00:07:06,114 --> 00:07:15,320 Is answer indexOf(letter) not equal to -1. 119 00:07:15,320 --> 00:07:18,170 So is the letter in the answer, okay? 120 00:07:19,560 --> 00:07:24,710 We'll store that in this hit, so we will say, if it is a hit, this is why camel 121 00:07:24,710 --> 00:07:30,160 casing is important, hits += letter, right? 122 00:07:30,160 --> 00:07:32,760 So, we're gonna concatenate the letter onto the hits. 123 00:07:32,760 --> 00:07:34,720 So hits is an empty string originally. 124 00:07:34,720 --> 00:07:35,580 Here we go. 125 00:07:35,580 --> 00:07:39,260 This might be the first time that you've encountered this type of branching, 126 00:07:39,260 --> 00:07:43,610 it's possible to perform another block of code when the expression isn't met. 127 00:07:43,610 --> 00:07:47,750 So in this case, we want to say else here. 128 00:07:47,750 --> 00:07:51,390 So else is used to describe what to do if our expression here, 129 00:07:51,390 --> 00:07:55,570 this is hit, is false, it's not met. 130 00:07:55,570 --> 00:08:00,940 So, in our case, we are interested in knowing if it wasn't a hit, right? 131 00:08:00,940 --> 00:08:04,703 We're gonna say that goes in the misses string, so. 132 00:08:06,452 --> 00:08:11,740 Okay, and look, this is closing that block and this is closing that block, cool. 133 00:08:11,740 --> 00:08:16,469 And then finally, what we need to do is we need to return isHit. 134 00:08:17,570 --> 00:08:20,550 Now do you notice I did something a little bad here. 135 00:08:20,550 --> 00:08:24,840 Look, so this is not the closing brace here. 136 00:08:24,840 --> 00:08:31,104 We need to close the actual, you need that method, we need this to close a class. 137 00:08:31,104 --> 00:08:33,260 So I was missing a brace. 138 00:08:33,260 --> 00:08:36,680 Very common as we start with more and more of these braces. 139 00:08:36,680 --> 00:08:40,163 Okay, so let's go ahead and let's save this, and 140 00:08:40,163 --> 00:08:42,597 let's jump in back to jshell here. 141 00:08:42,597 --> 00:08:46,340 And we're gonna say /open Game.java. 142 00:08:46,340 --> 00:08:50,021 And we will make a new game, so we'll say Game game = new Game. 143 00:08:51,390 --> 00:08:52,666 And we're gonna pass in treehouse. 144 00:08:54,759 --> 00:08:58,500 Cool, and now, we can use our applyGuess method. 145 00:08:58,500 --> 00:09:01,170 So it should let us know if it's in there or not. 146 00:09:01,170 --> 00:09:10,370 So let's say game.applyGuess, so t is in there, right? 147 00:09:10,370 --> 00:09:11,960 So it's in the puzzle. 148 00:09:11,960 --> 00:09:13,410 There it is right there game is in treehouse. 149 00:09:13,410 --> 00:09:15,080 So let's do it, this should return true. 150 00:09:16,210 --> 00:09:19,860 Perfect, and let's try to see what happens if we miss. 151 00:09:19,860 --> 00:09:21,520 Should return false, right? 152 00:09:21,520 --> 00:09:24,950 Perfect, the logic portion of the story is looking great. 153 00:09:24,950 --> 00:09:26,240 Let's get something using it. 154 00:09:27,300 --> 00:09:28,240 Great job. 155 00:09:28,240 --> 00:09:31,150 We have exposed our applyGuess method in the Game class. 156 00:09:31,150 --> 00:09:34,380 Now anyone using our game object can add a guess and 157 00:09:34,380 --> 00:09:37,930 the object can tell if it exists in answer or not. 158 00:09:37,930 --> 00:09:40,691 So let's go take some input from the console using our prompt object. 159 00:09:41,700 --> 00:09:44,200 But first let's work out those string and char muscles.