 and welcome everybody to Rock, Paper, Scissors tutorial in drum roll please, da-da-da-da, Java. I know I usually do Python, but today I'm gonna be doing a little bit of Java for you. So before I get started, quick shout out to my members. Thank you so much for supporting the channel. If you'd like to become a member, click join down below, click subscribe, click the bell, comment, upvote, very much appreciated. So today, as I mentioned, we're gonna be working on Rock, Paper, Scissors. So the topics we'll be covering basically are the scanner class, strings, we're gonna be using some random numbers later you'll see, and also there'll be conditionals I guess you'd say in this. So let's get started. Now, notice I've already saved this file. I've called it Rock, Paper, Scissors.java and notice the capitalization. So I'm assuming that you know some Java already. So this should not be your first Java experience. I have other tutorials that will walk you through some of these things. So in Java, everything is a class and I'm gonna call this Rock, Paper, Scissors. Notice Rock, Paper, Scissors.java, Rock, Paper, Scissors. These must match, otherwise it is not going to work correctly. I'm gonna go ahead and create my main method. So I'm gonna say public, public static void main string bracket so it's an array and args. Again, I'm assuming you either know what these things are or I can just tell you you don't need to know them right now. So I'm gonna go ahead and type system.out.println and say welcome to Rock, Paper, Scissors. Don't forget the semicolon. I teach a lot of, I mostly teach Python and most of the people on my channel are Python people and most of the people I teach in real life are Python people. So I'll be probably referring a lot to Python during this and if I do it's just to help those people. So you can see this is a very minimal Java program. Now note, I've got some comments here with the double slash. I got the class, I got my main method and I've got at least one line of code here. Now Java, I'm gonna need to compile and I'm using something called Genie here and if you're using something else that's fine, it shouldn't matter what you're using, but I like Genie and it's free and open source so check it out. And then I've compiled it and now I'm gonna run it and you'll see down here, welcome to Rock, Paper, and Scissors. Now, if your program is not working at this point, don't continue with the tutorial. You gotta get this part working. Otherwise the rest of it's just a waste of your time. So fortunately I've got it working. I've got everything balanced. I've got opening and closing braces here, opening and closing braces here. So thinking about Rock, Paper, Scissors. In this game, the player has to enter RPRS or something like that and then plays against the computer player. So let's go ahead and get that part started. What I'm gonna do is I'm just gonna copy this because I'm gonna be doing quite a lot of printing and so what I usually do is I just kinda copy that so I can just keep pasting it. Now I know some editors you can just type SOPL which will automatically expand. I don't know if Genie does that. Just by the, if you're curious how to spell Genie, it's right there and if you're curious, I'm also using something called Ubuntu Linux although this will work on Mac and Windows as far as I know. So yeah, so let's go ahead and see about getting some input from the user. So I'm gonna say please choose R, Rock, P for Paper or S for Scissors. Note the way I've written this and when I do it like this, it's kind of an old school thing so the capital R with the, you know, the right parenthesis means I expect the user to type R, capital R not the whole word Rock. In this case, not lowercase R. We're not gonna deal with that for this tutorial. So what I need to do here is I need to get input from the user and Java being Java, this is a bit more complicated than it is in something say like Python. So what I wanna do is I'm gonna use something called the scanner class. So I'm gonna go ahead and type import Java.util.scanner. Notice the capitalization. Scanner is a class so it's capitalized. And what I'm gonna be doing is I'm gonna be creating a scanner object which is gonna look like this. Let's see, scroll that up a little bit. So scanner again, capital. I'm gonna give my scanner a name. I'm using SC just because that's what I use and I probably got it from tutorials somewhere. I think it seems to be a common Java type thing so I'm gonna use that. Type new scanner and here's a weird one. We have to tell the computer where the input's coming from. So I wanna type system.in. So I assume that is the system standard input. I've created the object. Now I actually have to ask the user to use it. So actually what I wanna do is I'm going to move this up here for a second because it'll save us some trouble later. And now I have to copy that again. Copy that whole thing again and do the paste thing. So you don't need that line. I'm just doing this for my own use. So I've got the scanner. Now I'm gonna be asking the user for RPRS. So I'm gonna need a couple of variables to hold this information. So I'm gonna say string user choice. And I'm just gonna put it as an empty string and then I need another string for the computer choice. Like that. So to get the user choice, I'm gonna do the following. I'm gonna say user choice equals sc.nextline. So in this case, what I'm doing is I'm asking the user for a string. I'm gonna input a line. So let's go ahead and see how that works. So I've saved the file. I'm gonna compile it. And I'm gonna run it. So it says please choose rock, paper, scissors. Once I have R, seems like it's working. The only thing is I didn't like how the R is on the next line. So I'm gonna go ahead and change this to print. I'm gonna save it, compile it and test it. I cannot emphasize this enough for beginners to save compile test, save compile test. Because what happens is, and I'll invariably get questions later, you know, I got to line 47 and there's a problem in line 12 because I didn't check my code, okay? It's a lot easier to fix an error at the beginning. You can probably tell this is a pet peeve of mine, okay? So we've got a user choice. So what I wanna do next is, I wanna do the computer choice. I'm gonna say computer choice. Now right now, I'm just gonna hard code it because that part comes in later. So I'm gonna say computer choice equals S, which of course means scissors. So what I have to do is print the computer choice. So watch what I do here. So if computer choice dot equals, this is how you compare strings in Java. And I got that. The computer chose scissors. Of course, I know it's hard coded, but we'll fix that in a little bit, okay? So I'm gonna go ahead and save that and I'm going to compile it. Okay, I think my computer locked up. It'll unlock in a second. It does this every once in a while, I don't know why. But basically what I'm gonna do is I'm going to test the code because testing is very, very important. So there we go. So the compilation failed and because I forgot the parenthesis. It's very, very common. This parenthesis has to match this one because this one matches that one. Let's go ahead and compile again and run it. And it doesn't really matter what I choose at this point. But you can see how it says the computer chose scissors. I chose rock, the computer chose scissors. Now at this point, what I'd probably do is I'd go ahead and just do the other two choices real quick and paste that in there. I could have done duplicate, I believe as well. Just find what I should have done. So we got scissors, rock, and paper. So here I'm gonna put rock and here I'm gonna put paper. And again, at this point, I would probably test it. So let's do scissors, let's try rock, compile. Okay, rock, and the computer chose rock. And we did scissors, so let's go ahead and test paper. Notice it's a capital P, not a small P, run it. And I can put W, it doesn't really matter. The computer chose paper. So I'm gonna go ahead and go back to scissors. Doesn't really matter, but you can do what you want. So no matter what the computer choice is, now it's gonna print that out. And again, I know we would normally not use three if statements. We'll come back to that possibly later if we have time. Now we have to determine a winner. So this should be probably familiar to everyone, but let's go ahead and take a look at the possible outcomes of Junkin. Sorry, Junkin is what we call it in Japan. So you can see that the player has three possibilities, rock, paper, scissors. The computer has three possibilities, rock, paper, scissors. So obviously rock and rock is a tie. Paper and paper is a tie. Scissors and scissors is a tie. If the player chooses rock and the computer chooses scissors, the player wins. If the player chooses rock and the computer chooses paper, the computer wins. And so on and so forth for all nine possibilities. So there's nine potential combinations or outcomes of this game. There's three outcomes, win, lose, or tie, but there's nine potential combinations that lead to those outcomes. So let's go ahead and take a look back at our code. And what I wanna do is I'm just gonna go ahead and code one possible outcome. So I've already said that the computer choice is S. So I'm just gonna code S. I'm gonna say if, say user choice dot equals rock. And it's two ampersands. Computer choice dot equals scissors, because that's what we've coded. I'm gonna put an extra parenthesis to make sure this one matches that one. And then I'm gonna go ahead and copy this. I don't wanna type that whole thing in again. I'm gonna close it off. And I'm gonna say who won here. So in this case, user has rock, computer has scissors. We'll say the user won. Or you can say something like you won since you are the user, but you can say the user won. So at this point, I'm just gonna go ahead and test it. Because if this doesn't work, there's no point in moving forward. So I'm gonna go ahead and run it. It says please choose rock, paper, scissors. I'm gonna type R. And you can see the computer chose scissors, the user won. So so far, I have exactly what I think I need to happen. So rock, beat, scissors. So what I could do here at this point is go ahead and just kinda copy this. I'm gonna try and command D, which is duplicate. Now, here's what you need to think about. So I'm gonna go back to this matrix real quick. Okay, so there are one, two, three situations where the player wins. So player chooses paper and computer chooses, sorry, player chooses scissors, computer chooses paper, player chooses paper, computer chooses rock, and player chooses rock, computer chooses scissors. Now we've already done that one. So what I'm gonna do is I'm just gonna go ahead and code all of those possibilities. So rock beats scissors, paper beats rock, and scissors beats paper. So you can see here I've coded all three possible wins for the user. Now what I could do at this point, I've already tested scissors, I can go ahead and do paper. Okay, so scissors beats paper. So the computer chose paper, the user won. And then I could do rock. Okay, and then I, as the user, will choose paper. Okay, so clearly it's working exactly as I expected, and I forgot to switch back, I know. I did that, I did that in the last time, I had to stop the whole thing, and yeah, I got really annoyed. So all I did was I tested, you know, I did R, I tested it, I did paper, I tested it, I did scissors, I tested it. I'm not redoing the video. I can only have the patience to do it twice in one night. So what I've got working here is I've got all three where the user wins. So I'm just gonna go ahead and copy this. Actually I'm gonna go ahead and hit Command, Control-D, which basically duplicates it. Space here. So I'm gonna change this to the computer one. So again, I have to think about this. So let's do computer chose rock. That means the user chose scissors. The computer chose paper. So the user chose rock. And the computer chose scissors. So the user chose paper. So those are the three ways that the computer can win. So rock beats scissors, paper beats rock, scissors beats paper. Now again, I should test this, I'll test one code, but you should test them all. So the computer's going to choose rock. So rock beats scissors, and you'll see the computer won. So trust me when I say that all of these are gonna work, at least I think they will, unless I made a mistake somewhere. And then we have one more case, which is a tie. And so I'm gonna go ahead and duplicate this. So Control-D on my computer. Might be Command-D if you're using the Mac. So if the user choice equals S, and the computer choice is S, say tie. And so SS and rock, rock. And copy that. Definitely recommend lots of copy and pasting to make your life easier. Rock, rock, paper, and paper. So let's go ahead and test it. Okay, it's compiling, that's a good sign. And I think it was scissors, right? Oh, computer chose rock, let's try it again. Rock. And you can see how I chose a rock and the computer chose rock, so we have a tie game. So I'm pretty confident that this is all the code that we need for this game to work. Now, of course, the astute amongst you will be like, well, I don't wanna have to hard code this because every single time now it's just gonna be rock. So what we need to do is we need to ask the computer to choose randomly. And here's how we're gonna do that. We're gonna use something called the random class. So I'm gonna say import java.util.random. And I know there are other ways to do this, so don't flood my inbox or the comments, yes. And what I'm gonna do is I'm gonna go ahead and create a new random object. So just like I did with scanners, I'll put it here to make it, because it looks similar. So random called rand equals new random parentheses. Now, I don't need system in for this one, I'm just gonna put parentheses, okay? And what I'm gonna do is I'll put a little comment here. I'll say computer choice. Yeah, what I'll do here is I'm going to go ahead and do make a new int. I'm gonna call it num. And what I'll do here is I'm gonna say num equals rand.nextint parentheses three. And what this does is this gives me an integer from zero to three, but doesn't include three. So like zero, one, or two. Now, if this was a four, it would give me a random number, either zero, one, two, or three. But because I only have rock, paper, and scissors, I'm just gonna put a three there, okay? Now, what I'm gonna do is the following. I'm gonna say if num equals zero. I'll put parentheses, er, braces there. Computer choice equals r. And I'm just gonna go ahead and hit command D, or control D. Say one is gonna be paper. And two is going to be scissors. Now, notice, because this is an integer, defined as an integer up here, I'm gonna use double equal signs here. Okay, now for strings, I have to use the dot equals method. So if you're not familiar with that, there you go. So what now will happen is the program will come down, we'll get a random number, zero, one, or two. Zero, it's an r, it's a one, it's a p, it's a two, computer choice is s, and the rest of the program will function exactly as it did before. So let's go ahead and compile it, yosh. And run it, so I'm gonna go ahead and choose rock, and it says the computer chose paper, the computer won. So I'm gonna run it again, go ahead and hit rock. The computer chose scissors, the user won. And again, I would keep testing it just to make sure everything works, but I think we tested it enough earlier, so I think we should be okay with that. And there is our simple rock, paper, scissors program. Okay, at this point everything is working. I'm a happy person, life is good, okay? Now, those of you who've done some coding before probably realize, okay, well, this isn't particularly efficient code. You know, it's not good to go if, if, if because num can't be zero and it can't be one at the same time. So I would just go ahead and probably do something like else if and else if. And I could put an else if I was worried that the random number was gonna be a different number, but we'll just skip that part for now. Same thing here, the computer choice has to be SR or P. So I'm gonna go ahead and put else here, else if there, and same thing here. Cause we can, all of these options are what we'd say mutually exclusive. So we can go ahead and do that. Else if, else if, else if, and else if, and else if. And again, I probably could have done that from the beginning, see I misspelled else. So at this point, you know, again, I'm gonna compile it, see if I made any mistakes. Fantastic. I'm gonna run it and just kinda test it a couple times. I'll do R. Okay, so I chose rock, computer chose scissors, user one. Let's go ahead and I'll do paper this time. Oops, type the wrong spot. Okay, so I chose paper, computer chose scissors, computer one, and I'll go ahead and do scissors this time. See what happens. So we got scissors and scissors is a tie. And there's one more like optimization I could do here. There's actually a couple more, but I think I'll leave it fairly simple. I'm gonna go ahead and put some space there, like make it look a little bit nicer. Then you might prefer to have it squished together. I like having a little bit of space. That's better for my eyes I think. And so some of you've probably realized, especially those of you who have a little bit of a programming background, it's kind of inefficient to say this equals S and this equals S. This equals R and this equals R. This equals P and this equals P. So what I can do is I can take all three of those statements and I can condense them down to one. So I'm gonna delete the last two. And what I'm gonna say is if the user choice equals the computer choice, that's it. So if the user choice is S and the computer choice is S, we've got a tie. If it's R and R, we've got a tie. And if it's P and P, we have a tie as well. So I'm gonna go ahead and test it just to make sure it compiles. Make sure it runs. Say rock. Okay, so we've got paper. So let's see if we can get a tie here. Just test one of them. Okay, there we go. So we got paper and paper. It is a tie. So that is our simple rock, paper, scissors game using some basic Java stuff. And maybe some stuff you haven't seen before, like random. Again, there's another way to do this. But I think this way is a lot simpler for beginners. So let me just review the code real quick. So we've imported the scanner class, so we can do keyboard input. We've imported the random class so that we can do a random number. And we've got our standard Java class structure here. We've created a new scanner object called SC. We've created a random object called R-A-N-D. We've created an integer called num. Didn't have to be num, I just called it that. And we've got two strings, user choice and computer choice to store the information for those, yeah, the user and the computer respectively. To print out the information, then we use the next line method of the scanner object or scanner class to assign that value to user choice. Then we use the next int method of the random class to assign a value to num, either zero, one or two. If the value is zero, we set computer choice to R. If it's one, we set the computer choice to P. And if it's two, we set it to S. Then we print the computer's choice. So if the computer chose S, scissors, computer chose R, rock, here chose P, it's paper. And then down here we had our statements basically determining who the winner is. So rock beats scissors, paper beats rock, and scissors beats paper. And then the opposite for the computer winning. And then also then down here with the tie, we did a little bit of making our code a little bit more efficient. We said if the user choice equals the computer choice, there's a tie. Now just look what happens. If I enter like Q, basically we get the computer choice, but nothing happens. So this is something that we could deal with. But I think for beginners, this is pretty good. Just on a side note, I didn't have to do this part here. I could have done down here, I could have done something like num equals zero instead of the computer choice equals R or P or whatever like that. I could have just kept it zero. However, it just makes the code a lot harder to read, a lot harder to understand. Because then you have to think in your head, okay, what's a zero? What's a one? What does a two represent? So I think I found it easier if we're comparing similar values. And these values I chose because R stands for rock, S stands for scissors, and P stands for paper. So those are some of the choices you wanna make as a programmer to choose good values that make sense, to choose good variable names that make sense. Because you wanna be able to remember what you did when you come back to it and you want other people to understand what you did as well. So anyway, that is it. That is Rock, Paper, Scissors. I'll put a link down below to this code on my GitHub. I'll put a link down below to my other Java tutorials in case you haven't done a lot of Java and it'll show you most of these things. The only thing you might not see in the tutorials is random, because that's something new that I'm using now. And I just learned about it a couple days ago myself. So yeah, hope you liked it. So thanks for checking it out. Again, if you can join as a member, you can subscribe as a subscriber, and definitely click like and any questions in the comments below. Thanks again, and as I like to say, keep on coding. Take care.