 And welcome everyone to simple text adventure tutorial written in Java. This is for very, very beginning Java programmers. And we're going to be working on a game called Voldemort's Revenge. So let's get started here. Before we do real quick, shout out to my channel members, many of whom have been with me for six months or more, and some who are newer. So today's concepts that we're going to be looking at are integers, booleans, strings, conditionals, a while true loop, and the scanner class. So if you are familiar with these things, you'll be able to create this text adventure, at least this part of the text adventure that I want to do with you today. Okay, so let me show you what we're going to be doing. Now, what I'm going to show you here is not Java, okay? What I'm showing you here is something I made ages and ages and ages ago using JavaScript. Now we're going to put this into Java, of course. Now we're not going to be able to do graphics or anything like that. We're just going to have a text interface. So a text adventure is an interactive story where you are the protagonist. So here is the story I'm working on. It's called Voldemort's Revenge. And I hope JK Rowling has a sense of humor and does not sue me. So here's the situation. You're Harry Potter, Hermione and Ron have been captured by Lord Voldemort. And you need to rescue them, says press enter to begin. So I'm going to go ahead and do that. So if you've never done interactive fiction, which is also called our text adventure before. Essentially, you are in various locations and there are objects and things that you can interact with. So and you can go in different directions such as north, south, east, west, up or down are typical, typical directions in this sort of thing. So let's take a look here. So it says you are in Dumbledore's office. There are a lot of old books on bookshelves. Dumbledore's desk has various items on it. There's a magic mirror in one corner and Dumbledore is sitting behind his desk. So what you have to think about at this point is if I were in this situation, what would I do? I just did this with my students literally this week. And some of them were like, well, just grab some books. And I'm like, well, you don't walk into someone's office and start grabbing books. So the way these old school text adventures worked is you would issue commands. So in this case, I'm going to say, ask Dumbledore. Now to keep it simple, I'm going to use everything in lower case, couple of little simplifications, just to make this program work that we're going to be doing today. So I'm going to go ahead and type ask Dumbledore and I'm going to hit Enter. Okay, so I do that. It says, Ron and Hermione were kidnapped by Lord Voldemort. Take this magic amulet. It will glow when you get close to your friends and when you find them, say, return us. This is really, really important. When my students play this game, they often forget to write that down. They get to the end and they can't rescue Ron and Hermione because they have not written down all the instructions. So you can see there's an amulet now in this room. So if I type get amulet, okay, I now have that in my inventory. I can also type INV to see what I have. Now we're going to be doing a much simplified version of this, which you'll see soon. And you'll also see that I have a score of 10. Okay, so I've kind of, I've done one important thing in the game. So I'm going to go ahead and examine. This is another thing that you do in Text Adventures. And I'm going to type mirror. So keep in mind, these are the commands that we're going to be doing in Java here in just about a minute. So it says the mirror is bigger than you and has a gold frame. There is some ancient writing on the frame. It says to use this mirror saith thou trelfaths. So clearly I'm going to go ahead and hit say trelfaths. And now watch what happens. I get a little message. It says the magic mirror begins to glow. You feel yourself floating towards the mirror. You go through the mirror and find yourself on a path. You're on a path. The path goes in many different directions. To the north, you can see trees. It must be the legendary forbidden forest. To the south, you can see a river. To the east is the castle of evil Lord Voldemort. So you can see now I can go north, south, or east. Typically in these games, you would just type N, S, E, you know, north, south, or east, W for west, et cetera, et cetera. So what we're going to do is we're just going to stop there. We're going to go ahead and program what we just did in a very simplified manner. This is, again, something that I made for my students. I just did this with my students this week. And I wanted them to be able to apply some of the things that they're learning. And they just started learning Java about six weeks ago. So they know the basics, which I mentioned on the previous slide. So let's go ahead and get started. So I'm going to pull up G, which is my preferred programming editor. You can use whatever you like. I'm going to make a new file. And I'm going to call this Voldemort's Revenge. And I'm going to go ahead and do public static void main string arcs. Now, if you don't know what any of this means, this is not the right tutorial for you. You should check out my Intro to Java tutorial and go from there. So I'm going to go ahead and just say this to my desktop real quick. And Voldemort'sRevenge.java. Now, notice I saved it as the exact same name as the class. So this is going to be where I type all of my game code. Now, I'm keeping this simple. I'm not going to be adding any other classes. Let me go ahead and actually make the text a little bigger so you can make sure everybody can see that. And what I want to do is I want to think about this game. What do I need to keep track of? So when I initialize the game, these are my starting values. So thinking back to what we did, so for example, there was a score. So the score when we start the game is going to be 0. That's kind of logical. Let's see what else do we have. The user entered a command. So that's going to be a string. So we're just setting up our basic values, our basic variables. And what else do we have there? We had a room. Now, the room, what we could do is we could say string room equals and then give it a name. What I did in this particular case was I actually did room as an integer. And it works. And so we'll go ahead and stick with that just to demonstrate a couple of things about Java programming. Now note Dumbledore's room is going to be room 1. And the path I'm going to call that room 2. So each room has to have a unique number. And then finally what I'm going to do is I'm going to make a boolean called has amulet. And when we start the game, of course, we do not have the amulet. So that's basically the start of this game. This is what we have. This is what we need to keep track of so far. Now, there's more, of course, once you get through the whole game. But for this particular part that we want to program, this is all we need. Now, the next thing we need to think about is we need to get input from the user. So when you're talking about keyboard input, this is the scanner class. And again, if you haven't watched my introductory videos and this does not make sense to you, stop now. Go watch those. Come back. So I'm going to go ahead and import java.util.scanner. And then I'm going to go ahead and create a scanner object. Call that scanner sc equals new scanner system.in. And now with this sc, I can actually go ahead and get input from the user. Now, as you noticed, the game continues and continues and continues. So what we're going to have to do here is we're going to have to create a while true loop. And a while true loop is basically just runs forever. You might hear it be called a forever loop. Some people might call it their main loop in their game. So first, what we want to do is we're going to print the room information. And this is where conditionals come in. So as I mentioned, I made this for my students. And we've studied so far basic types. We studied strings. And this unit was about conditionals. So our room is determined by this particular variable. So room equals 1. That is Dumbledore's office. So what I'm going to do is I'm going to go ahead and put if room equals equals 1. Now I'm using equals equals because it's an integer. I'm going to say system.out.println. I'll just say Dumbledore's office. We'll write the whole thing. You are in Dumbledore's office. And then I'm going to go ahead and just copy this because I'm kind of lazy. And I'm typing it out over and over again. I'm going to keep it simple here. Dumbledore looks worried. There is a magic mirror in the corner. In the corner. And I think that was it. That was the key information, I think. So then I need to do that. So why don't I go ahead and run this? Let's compile it. Make sure it's working. So I'm going to hit the compile button. And compile successfully, which is always a good thing. I'm going to run this and watch what happens. OK, so you can see it says you are in Dumbledore's office. There's a magic mirror in the corner. Dumbledore looks worried. It's just repeating itself. So to get this to stop, I've got to do control and z. And if you're on a Mac, it's the same thing. Control z, not command z. So I'm on Linux. I think Windows would probably work the same. I'm going to go ahead and hit Enter. So if you're using Genie and you see this red thing, just go ahead and hit Enter. And that clears it. Now we can run it again. So it's printing the room information over and over because we're stuck in this while true loop. So next, what I'm going to do is I'm going to go and get the user, I'll say player, get the player command. So I'm going to go ahead and type system.out.print, not print line in this case. And I'm going to keep it old school. And I'm going to do that. And then what I do is I do command equals sc.nextline. So if you recall, sc is the name of our scanner object. And to get text input, this is text, not integer, not a double, not a boolean, et cetera. Text input, we have to do sc.nextline. And then here is where we will deal with the user or player, I'll say player, input or commands, depends how you want to put it. So this is where it's really important. We have to think about this. So our first room was Dumbledore's office. Actually, let me go back. Let me see if this is still working. So I'm in room two now. I'm on the path. So if I type ask Dumbledore, nothing should happen. So it says, I'm not sure what ask Dumbledore means. This is because we're in room two. There's no Dumbledore here. So ex, a, mirror, nothing happens. Well, is there anything else? I think that was it. I'm going to try to say trialfaz again. So those are the three commands we're going to work. Now I had this little message up because it says you can't say the trialfaz. I know, clever. So they're not working because we're not in that room. So this tells us that we're going to need another if statement. So if room equals one, and then in here is where I'm going to deal with all the possible inputs for that room. So if command equals, let's say ask Dumbledore. Now note, here I'm using dot equals. I'm not using equals equals. Room is an integer, so we use equals equals. Command is a string, so we use dot equals. So don't make that mistake. It's a very common beginner mistake. So if the command equals ask Dumbledore. So this is where he tells us what's going on. So we're going to go ahead and system.out.println, Hermione, and Ron. Ron had been kidnapped by Lord Voldemort. I forgot my semicolon. Let's go ahead and copy that. You need to rescue them. No pressure. Thanks a lot, Dumbledore. You need to rescue them. Here is a magic amulet. It will glow when you get close. And what was the other thing we needed to do? Yes, when you find them, say return. That's our magic word. I know very clever. So let's try this. Let's see if that works. So I'm going to go ahead and compile it. I'm going to go ahead and run it. It says you are in Dumbledore's office. Dumbledore looks worried. There is a magic mirror in the corner. So I'm going to go ahead and type ask Dumbledore. And you can see there. Let's scroll up a little bit. It says Hermione and Ron have been kidnapped by Lord Voldemort. You need to rescue them. Here is the magic amulet. It will glow when you get close when you find them. Say return us. So far, so good. So our basic commands are working. Now if you recall from when we practiced the game earlier, a couple of things happened. The amulet appeared, and we were able to get it. Now we're going to simplify that. We're just going to get it directly. And our score went up by 10. So what we'll do is in here, we'll go ahead and do score plus equals 10. And notice, if you recall, we did this. So we either have the amulet or we do not. So I'm going to go ahead and do has amulet equals true. So now, OK, I'll just real quick. Don't forget this. Now notice this program is still running. So I need to do Control Z to stop it. This is red. So I need to hit Enter. That's a little annoying thing with Genie. But just so you're aware. If you're using something else, you don't have to do that probably, but you might. So now the score is plus equals 10, has amulet equals true. So we need to print this information out now. Now what I want to do is I want it to be after the room information and before the command. So I'm going to go ahead and say prints inventory. This is the stuff you have. And score. Now score is pretty easy. I'm going to state system.out.println.score and plus score. And again, if this stuff is not familiar to you, this is not the tutorial for you. You should go back and learn some of the basics. So let's go ahead and just test that part. Compile it and run it. And nothing happened. That's weird. Let's go ahead and try it again. Run or review the current file. And let's see why is it doing that too. Ah, OK, it's locked up. Yeah, Genie does this from time to time. Let's see if that fixes it. Nope, I'll have to close Genie, I think, and run that again. So let me go ahead and open that up and compile. And I know you can see that I think the font got a little smaller. Let's make it big again. And go ahead and run it. OK, so you see Dumbledore looks worried. There's a magic mirror in the corner. Score is zero. So I'm going to ask Dumbledore. Now you can see my score is 10, because I did that. Now, of course, I can do this, Dumbledore. I keep getting scores. But if we were going to program this properly, we would avoid that. But for now, we're just going to let that go. So I'm going to do Control-Z to stop it. Enter to get rid of that red thing. And now I want to print my inventory. So I'm going to do System.out.print. You have, and I'm going to do this, if has amulet, System.out.print, amulet, and a space. And the reason I put a space there is because once I start adding other objects, I'm going to need to print them out side by side. Then I'm going to go ahead and do System.out.println to go to the next line. So again, I'm going to go ahead and test it. Compiled. So I'm going to ask Dumbledore. Now it says you have the amulet and your score is 10. So we're making really good progress. I'm hoping that you're seeing it's pretty simple. With what I'm doing here, you could program, essentially, an entire text adventure. Now your code wouldn't be efficient. It would get pretty long, longer than it needs to be. But you could do it. And this is something I tell my students, if you remember nothing else from what I teach you, remember this. All of programming is if this, then that. So if you have the amulet, print amulets. If you don't have the amulet, obviously you don't print it. So let's go ahead and finish out the rest of the commands. We also have elseif command.equals. What was it? Is a mirror? Now we might also want to do or command.equals in case somebody wants to type examine or examine magic mirror. So that way we could strain together multiple possibilities. So this one we just say system. Copy that. I hate typing. I get so sick of typing that out. The mirror is large and has, what does it say, ancient writing on it. It says to useif this, useif this, mirror, sayif now, try with as. And that's the magic word to get the mirror to work. That's all that really happens. So let's go ahead and test that. You notice how every time I add something, I stop and test it. Ooh, did I close that? No, I didn't. So I go to command Z, enter. Go ahead and run it again. So I'll ask Dumbledore. And I'm testing this to make sure I didn't screen anything up with that. That still works. I'm going to do exa mirror. And let's see here. If I go up, you can see here with a little cut off the screen. The mirror is large. It has ancient writing. It says blah, blah, blah, blah, blah. OK, so my next thing is to go ahead and program the command. So elseif, let's go ahead and close that so I don't forget. Elseif, command.equals, say tralfaz. And don't forget that second closing parenthesis. I do it all the time. It's really a rookie mistake, but I do it all the time. So system.out.println, say you float up and through the mirror. Now at this point, we have to think about what happens. So we float through the mirror and what changes. Our score is going to go up by 10 again. And we're going to change to a different room. So I'm going to go ahead and say room equals 2. Score plus equals 10. Now if you notice, we haven't coded information for room 1. So I'm going to go ahead and type elseif here. Elseif, room equals 2. And I'm just going to copy this, change that. You are on a path. And I can say to the north is the Forbidden Forest, the east, Voldemort's Castle, and to the south. So let's go ahead and test it. Compile, run. OK, so I'm going to go ahead and ask Dumbledore. OK, examine mirror. Now again, it's getting cut off. Sorry about that. OK, some mirrors large. And then I'm going to say trough us. OK, you float up and through the mirror, you are on a path to the north is the Forbidden Forest, to the east, et cetera, et cetera. Now again, if I type get amulets, or sorry, if I say ask Dumbledore, nothing happens because I'm on the path. OK? So you can see how, hopefully, you can see with just what I've shown you here, you can create a fairly complex and complicated text adventure with just what I've shown you here. You don't need any fancy, you don't need any other types of loops. You don't need any arrays. You don't need any array lists. You don't need any hash maps. Everything you need can be done with what you see here. Now again, all those other things will make your code more efficient, will make it faster, make it easier to update. But essentially, this entire thing comes down to if this, then that. If you're in room one, print this information. If you're in room two, print this information. If you have the amulet, print amulets. Print the score. Get the command. Now if you're in room one, here are the commands that it recognizes. Now actually, I can actually guess I could add here at the end of this, system.out.println. Sorry, I don't know how to do that, how to do that. Maybe that would give maybe a little bit easier on you. What did I screw up there? I forgot the closing. Yeah, I forgot that. So I'm going to go ahead and compile it again. Run it. And just like blah, blah, blah. And again, I do that. It says, sorry, I don't know how to do that. So you'd have to put that inside of everyone. So then now that we have room two, we can do else if room equals two. And then you deal with all the room two stuff. Now with room two, all you really have is like if command dot equals, let's say east. Don't forget the second one. All you have to do is change your room. Let's say room three is the entrance to Voldemort's castle. So let's go ahead and do that real quick. So we didn't get to it in our practice, but let's just try it here. If room equals three, I don't like to repeat myself a million times, so I'm going to copy that. And you can put more information there. I might also put system dot out dot print ln. There is, because that's the way we came. We came from the west. Let's go ahead and try it. I've got to compile it. I've got to close this. Command Z, Enter, Compile, Play. So now I can just go ahead and jump ahead. I know how to do this. So say trial fads. And I'm going to go ahead and see if I can go east. And it says you are at the entrance to Voldemort's castle. There's a path to the west. So hopefully you can see now with just a bunch of if statements and else if statements, et cetera, et cetera, you can program an entire text adventure. The trick to this is just thinking about what information you need to keep track of. Thinking about its type. So the score, for example, is an int. Your command, of course, is a string. And again, I used integers for the room. You could do something different. I just did it that way. I liked it. And booleans for whether or not you have something or you have done something. So one of my students was very clever. She came up with the idea, well, let's do something like has asked Dumbledore. And then so it was false. So basically what would happen is with the first time you ask Dumbledore, that becomes true. And the second time you ask Dumbledore, he doesn't say the same thing. He says, hey, I already told you what to do. So I thought that was really clever. And I was happy she came up with that idea. So you can see here, this is what coding breaks down to. If this, then that. Essentially, with this kind of structure, you could do almost anything. Arrays and things like that are conveniences. And they're very helpful. But fundamentally, everything that you need to do can be done with basically a while true loop and a ton of if statements. It's really fascinating stuff. So that is that. That's the lesson. I hope you learned something from it. I hope it wasn't too fast. Again, if some of this stuff didn't make sense or you haven't seen it before, you might want to go back and check my beginner Java intro. I think that'll clear up a lot of things. And I hope you can maybe come up with some cool games or come up with some cool ideas of your own. So thanks a lot. Click like, subscribe, et cetera, et cetera down below. And yeah, keep on coding. Take care.