 Okay, everyone. Welcome to Wordle in the terminal in Java with Tokyo EdTech. That is me The idea for this came from actually, uh, I was did it this in Python a couple days ago So let's go take a look at that. I'm gonna run it and show you what the program looks like. It's supposed to look like So in Wordle, you are given a five letter word and you have six guesses to figure out what that word is So I'm gonna try Please And so what you see here is ea and s are in the word, but they're in the wrong spot So that's why this is yellow. So let's see Shake Okay, so s and a are in the word e is in the word and in the last spot Now you can kind of see here some of these words. It's probably amused and you can see I got all four correct I win yay me. So what we're gonna do is we're gonna try to do that in Java so just a quick kind of overview You may have seen the game like this It's much nicer obviously with graphics, but we're gonna do it in the terminal kick at old school And I want to thanks Thank Yamashita Tatsuo's on for this picture here on Flickr. So you can see that the correct word is shake and based on the What the user enters each? Round you can see how the colors change again P and T are not in the word at all MUD are not in the word A and s are in the word, but they're not in the correct location Now e is in the word and it is in the correct location. So that's kind of how this works so what we're gonna be looking at today is Get back to the other screen is Using this in Java or programming this in Java and we're gonna be using the scanner class to get input We're gonna be using substring to compare one character at a time We're definitely use some loops. We're gonna use ANSI color codes Okay, so that's what you saw with like the background was yellow the background was green Those come from they're called you know ANSI color codes. I got it from this website this blog is amazing check it out. I'll put a link down below if you're watching this on YouTube and Basically, they're these special codes that you can enter into the terminal and they will change the color of the text They'll change the color of the background of the text And we're only gonna be using a few of those and there's a bunch of them here check it out It's really really useful. I'll show you just the bare minimum. You need to Program world. Let's go ahead and get coding on that. So this is a Java program. So we're gonna need a class and It's gonna be called wordl and notice. I've saved this already as wordl.java notice it's capitalized and Then of course we need our public static void main string args and I'm not gonna use any functions in this program. I'm just gonna put everything inside the main Method, I'm just gonna go ahead and try and compile this and just make sure it compiles now if I run it Nothing's gonna happen because no code does anything. So let's go ahead and Get started. So system.out.println wordl So what we need first is we need the correct word And we need a guess from the user. So these are gonna be strings So correct and we're gonna just go ahead and use what we saw earlier So if I go back to Musha-san's thing here, we're gonna use shake as our correct word. Notice how I've capitalized all the letters And I'm gonna make a string called guess And I'm trying to go ahead and use What we see here paste Now eventually we'll get input from the user, but just for now, we'll just start like this So the user is going to make a guess and in this case it is paste so What would you do here? This this is where especially beginners are like, well, I don't even know how to start this What would I do? So we need to think about the problem a little bit. So in my mind We're not looking at a word so much as we're looking at each letter So basically we want to know our S and P the same letter if they are then we're going green If they're not Then we got to check and see well is P in the word somewhere, but not the first letter If it is then we got to go yellow and then if it's not green or yellow It's not the word at all. We're just gonna do a regular black background So this is gonna be pretty simple So in Java like the way that we get the first letter out of our string is gonna be like this. I'm gonna do if Correct dot substring 0 comma 1 Dot equals notice. I'm not using equals equals This does not work correctly or doesn't work as you expect in Java. We're gonna use the equals method and It's gonna be guess that substring I did this same way in Python so I'm gonna say guess that's up and it doesn't really matter, but it just Seems a bit easier to follow me. So guess that substring zero one. So P is the zero with letter A is the first letter. So zero and one these are the indices or the indexes how we want to say it So this is zero one two three four So notice there are five letters, but the index is zero one two three and four So hopefully you should probably already know that before you try this So then we're gonna go dot equals and We're gonna say correct dot substring Zero comma one and let's make sure we get the right number of parentheses So notice this parenthesis matches this parenthesis and this matches this if you're curious I'm using an editor called genie. It is free and open source. I'll put a link below check it out Okay, so this is our first case. So the letter matches So what we would do here is we would print you know in green so System dot out dot print Okay, so we need to print green. So let's go ahead and show you how to do that now I'm just gonna copy this from my Python program, but you could also get it from that web page I showed you so basically what I've got is I've got three different codes I got background green background yellow and reset So I'm gonna go over back to my Java and I'm gonna put these in the program Here just at the start. These are these are gonna be final because they don't change They are strings. I'm say final string bg green Because I'm never these don't change. These are these are constant string Which is nice also because I can use the same You know variable naming convention final string reset So what I need to do is I need to put this is correct. So the bg green plus Guess that sub string Zero comma one plus reset So what that does is it prints across? No, there's no print ln because we do want to print across so it will print with a green background The first letter of guess that could have done correct because they're gonna be the same letter in this case But it makes a little bit easier in the next part to do it And then I want to reset back to black and white because we don't know what the next one's gonna be So our second case is else if oops And this is a case where the letter is In the word but in a different location and so to figure out if a letter is in a Word we use index of So watch what I do here. So if correct Index Index of guess that sub string It's zero comma one is Greater than negative one So index of tells us whether a not a letter is in or actually tells us where a letter is in a different string If it's not there It returns negative one. So for example p is not in Shake, so it's gonna return negative one, but if I want to do the a a is in shake at zero one and two so It's greater than negative one. So we're gonna print that out as Yellow so I'm gonna copy this letter is In word But different location So I'm just gonna change that to background yellow and The final else Okay, so nothing we've got it wrong. It's not in the words. Nothing's happened We're just gonna go ahead and say, you know, not Word we're gonna go ahead and print out some system system Dot out that print Guess that substring So I think that's about right, so let's go ahead and test it. Let's see what happens here Pretty confident, but maybe a mistake. You never know and this is always when I tell students Okay, so we got an error because I copy and paste it from Python about my semi-colons. I wonder how many of you caught that if you caught that go ahead and comment below Let's go ahead and compile that again Okay compile successfully Okay, so P is black and white so black background white letters Which is what we expect because P is not in the words. Let's go ahead and try S Let's do a different word so shame and Compile that so now we're compiling S or comparing S and S That gives us green, which is what we want. So let's do a muse and this should give us yellow Because a is in the word, but it's in a different location Okay, so I've tested all three possibilities that I know of at this point. I'm pretty confident that What I've done is correct. Okay, you know, there's always unintended consequences, but I'm pretty confident that is what should happen so This is our basic comparison And this this this will work for all of our different comparisons. The problem is we need to compare First letter to the first letter or I should say zero zero one one two two three three four four There's no five five because it's zero to four So those of you at home who've done this before hopefully are thinking hey This would be a great place for a loop and you are correct. So what I'm gonna do I'm gonna say I'm gonna create a loop to iterate Through each letter. So I'm gonna say for int I Equal zero we're starting at zero I is less than five because that's the length of the word now In word old words are always five letters. So we can use I is less than five We want to increment by one each time So put a curly brace. I'll be careful here as you got to indent that part Okay, so this should line up this should line up. Let's go ahead and run that and see what happens Hopefully you figure out there's a problem so So uh, uh, uh, uh, uh, uh, okay because we got a and s so it's not Changing it's it's hard coded to zero and one So here's what I always tell my students to think about What is the relationship between zero and one mathematically? So Yeah, you think about it. Okay, obviously Zero plus one is one. So the relationship is plus one. So what I'm gonna do is I'm gonna put I And I plus one So everywhere I've got zero one. I'm gonna change that to I and I plus one All right, so let's go ahead and compile that And see what happens. You can see we've got am you as e so the word is shake So a is in the word, but it's in the wrong spot s is in the word, but it's in the wrong spot and then e is in the word and it is in the Correct spot and em you are not so I'm pretty confident that this is what we want to happen Let's go ahead and do shake just to test it. Okay, and then obviously it's working. Okay, so At this point, we've got the basic program functioning as we want. We've done the hard part really Was just like kind of comparing everything putting out the colors, etc. etc. So the next thing we need to think about is Well, let's go ahead and do input from the user. Well, yeah, what do you input from the user at this point? Okay, so to do that we're gonna use something called the Scanner class. So I'm gonna say import Java dot util dot scanner and then down here what we're gonna do is we're going to actually create a scanner object Say scanner SC equals new scanner. We have to type system dot in yeah, I presume most of you are And then for the user the user is going to ask for SC dot next line And that will get Input from the user and I'm gonna go ahead and try that. So it's just a rock brilliant It's really hard to talk and type at the same time Wordle, okay. Well, I should have typed something there. So let's try Shake Okay, so shake shake everybody's happy. Looks like it's working Let me go ahead and put here a little print system dot out print LN print LN print Did I put over in the Python program? It looks similar Please guess, okay. That's very original. Okay, please guess and try this again System yeah, let's do shake again Okay, we got all four. There's it doesn't loop. We'll fix that in a minute I'm gonna try this again. I'm gonna type shake lowercase. Let's see what happens. First of all, what do you think is gonna happen? and if you said well Nothing, you're correct sort of so you can see how it doesn't even Recognize like the letters at all. Okay, which is kind of sad. That's not what we wanted to happen Now it's weird that didn't print anything out Oh That's pretty pretty out what I print. Okay. So what we're gonna do is we're just gonna go ahead and use dot upper I think we can add it here and test this one and this will change whatever I type into upper case So let's compile it Okay, upper is Python. It's to upper maybe in Java. Nope still not happy with that Down here. So I say guess equals guess not to upper It does not like this at all cannot find symbol guess Let's try this and then we'll say guess Equals cannot find symbol. Why can't I find a symbol? Let me think about this for a minute. Okay, I messed around with that a little bit and I figured out what it is All right, let's let's put this back to what I had before String guess I'll say guess and then Guess equals SC that X line dot to upper case Will come as no surprise. I don't do a lot of Java program believe it or not Compared to Python at least so then it compiles and here we go. Let's try the shake and Notice how shake was converted to all capital letters and now it is working So let me have to move this up here Because we're gonna now we need to add a loop where we get six guesses Okay, so the loop is gonna start here. So a loop for six guesses so what I'm gonna do is I'm gonna make another for loop and I could there's different ways I could do this But I'm gonna do with another for loop since we know it's always six guesses And we'll say int and I can't use guess because we already use guess here I'll say round so round one round two so round equals Zero Now we could make it one if you want we can make it round one But usually we count from zero just like we did with the index for the strings So for it round zero equals around zero round is less than six and round plus plus Now what happens is this whole thing now has to go inside That Again, there's we could done it slightly different way we could make a function make a little bit cleaner But again, I make these for my students and my students haven't really learned a lot about functions yet. So We'll get to that eventually So this gives us six guesses. Let's go ahead and try it. We're gonna see some weird weird things here I think so I'm gonna say guess I'm gonna try a muse since we know that's one word Now it gave me the correct output, but then it put this next to That which I don't want so I'm gonna type control Z to in that So after the loop that Interates through each letter we need to print To the next line so system dot out dot print L N And that'll go to the next line. So let's go ahead and test that So amuse See what else Shale can get almost all of them. Maybe CD Let's go ahead and do the correct word see what happens Okay, so notice we got the word correct But it's still playing. Okay, so this is this is something we have to do. So let's go ahead and fix that so After we check we're gonna say if Guess dot equals So system dot system Let's go ahead and again, this is you notice how every time I make a change of some sort I go ahead and I Recompile it or retest it so I know Okay amuse Shake Correct you win program didn't stop And that is one I actually Forget actually forget how to do that. Um, so what I'm gonna do is I'm gonna break out of the Loop So break will end This for loop So that's a command you might not use Okay shake Okay, so we got all five correct it says correct you in program is over Now we haven't dealt with one other situation And this is where I think this is my sixth guess. So I did six guesses. I didn't get it correct. I Should do something with that. Okay, so as I said when I use this break command I broke out of This six guess loop. So I'm gonna go down What I'm gonna do is after that Print correct answer if player loses So I'm just gonna do if not guess dot equals Correct So if the guess is not equal to the correct number if we've gotten this far and guess is not equal with a system dot Out that print ln is Plus I think that's it. Let's try it. Guess might not have been initialized. Okay See your string guess. So basically up here Because I put string guess. I didn't give it a value. I Just initialized I didn't initialize it. I just it just made straight But I didn't give it a value. It's null right now now this down here. There's no guarantee In the computer's mind that we'll get to this. So what we got to do is just give this a value And I'm just gonna give it null not null. I'm gonna give it an empty string. Sorry. There's a big difference there So now it compiles Okay, so let's do a muse Again third fourth Fifth here's my sixth attempt and wrong. It says the correct word is shake So basically we now have a functioning wordal game. We're going to pile up one more time And muse Simple, that's too many letters slams Okay, and shake correct you Okay, so the only problem this game now is that it always Shake is always the word. So let's go ahead and fix that real quick And so what we're gonna do is we're going to make a List of words or an array of words. Sorry lists are Python So I'm gonna make an array of strings I'm gonna call it words And this way I can actually just set it at the beginning So I'm just gonna be lazing copy this from over here. I already did shake share panic of using shade I'm gonna copy that over to here Yeah, let's compile that make sure I did that right because I don't do as much Java as I do I do make mistakes and stuff like this. So what we need to do is we need to choose a random word So we need to import Java dot util dot Or no, it's math math that's what we wanted. I think there is a random, but I think we'll just do math Let's try this so We'll say so say words and that's me math that random times Here, let's let's separate this out. So we'll say int W index equals math that random Times birds dot And again, I should check this but it might be this I do in to cast this Yeah, I probably should it figures one out ahead of time This is gonna be quite correct, but we'll try it I'm gonna add one here plus one. Let's try it W index Piles even works compiled. Oh my gosh It's right almost there come on Index five out of bounds. Yeah Okay, where's that length so zero one two three four. Okay. Yeah, that was my bad So this will give us a random number from zero to four Okay, so let's try and use It's a shake. Oh, it's not shake shale share Correct all right, so let's try it one more time. Just make sure it's not always given us the same word news And shake was Okay, so there you have it a couple little hiccups to here and there again, sorry, I'm much more familiar with Python than I am Java and We got it done. So just kind of to go through this one more time real quick You'll see we have the scanner class. We use that for user input We've used these and see color codes again, there's quite a few more you can use It's pretty pretty cool actually what you can do with it. We print it out the title We chose a random word using the right math that random method and a little bit of math there and casting again I don't know that part You just just copy it as it is So we chose a chose one of the words at random Now you can add as many as you like here and this will automatically adjust and using the scanner We're gonna be getting a guess from the user. So we have six rounds in each round. There's a guess We compare each of the five letters to its counterpart in the correct word and print out green if it matches yellow if it matches, but in a different location and Regular like black and white if it is not in the word Then we print a new line so we go the next line and we check if the player has won We break game over if the player is not won We repeat until we get to six tries if we get to six tries and the player has not guessed the correct word We tell them a correct word game is over Pretty cool. Okay, so, you know Thanks for watching subscribe like follow Join my discord whatever links are down below. Take care