 And welcome to Rock, Paper, Scissors Tutorial Part 2. In this part I'm going to add a loop to the program that we made last time and so we can get our game to repeat more than once. So a quick shout out to my members, Kim Cheong joining our four month member team of Kevin and Paul. And we have quite a lot of movement over here. I think Hassan moved over to the paddles, third month, I think King Monk moved up to the snake level. So thank you all for your support and everybody also check out Old School Coder who has an excellent YouTube channel as well. So yeah, thanks to everybody for supporting the channel. So let's take a look at what we've been doing. This is a Java tutorial. I'm normally a Python guy as you know, but I also teach Java and I'm teaching Java right now and I'm actually going to be doing this lesson tomorrow with my students. And yeah, so I thought this would be a great way for me to prepare and I thought I could share it with everyone. So let's go ahead and just review what we've got so far. So we've got welcome to rock, paper, or scissors and I choose, let's say I choose rock, go figure and you can kind of scroll out the screen, but we see the computer had a choice and in this case it was paper and paper beats rock, which yeah, really does it. But anyway, that's the way the game works and the program ends. So at this point, you know, I have to rerun the program and it just gets tiresome to do that. So what I want to do here is I'm going to do something called a for loop. And what I want to do with this is I'm going to decide how many times I want my program to repeat. So before we do that, let's take a look at a couple of things here. You can see here in the main method, this is where we create our variables, this is where we create our objects or I should say instantiate our objects. It's probably a better way of putting it. So we've got a scanner, we've got a random object and we've got some strings and we've got an int. And then here we're printing the title of the game. So as a programmer, I have to think about which section do I want my program to repeat. Now in Java, you only want to do this part one time. You only need to set those objects up, you only need to set up the variables so that there's a space in memory for them. And in my game, I don't want to reprint the welcome message every time because it just doesn't make sense. We know we're playing rock, paper, scissors. But I do want the player to choose rock, paper, scissors again and again. So I'm just going to put a little comment here and like repeat this section. And keep in mind, what I want to do is I need to repeat all of these things. So it's not that I can't repeat just a part of it. I have to do all these things. I have to have the computer make a choice. I have to convert that choice to an RP or S for our code. Then I have to figure out who is the winner. So basically I'm going to find the start and end part of my code. So keep in mind that this is part of the section. Now this little brace matches with this brace. And again, I don't want to repeat this whole section here. I want to repeat from here. So I'm going to use the first type of loop. And again, I'm assuming you're somewhat familiar with Java and loops and things like that either from Java or from other languages. So what I want to do is I'm going to go ahead and say I want to play this game three times. So here's what I'm going to do. I'm going to say four int i equals zero, semicolon. And while i is less than three, we do i plus plus, we increment i. And so then I'm going to hit enter. And then what I need to do is I'm going to indent this entire section that I want to repeat. Now in Java, I don't absolutely have to do this, but it makes your code easier to read and to understand. And then don't forget the closing brace. So let me just show that one more time. Notice how all this is now indented inside this for loop. And how this works. And again, if you don't know what a for loop is, you know, watch my for loop video and explains all these things or my loop video. So what I'm doing is I'm saying i is zero and it is an integer. And then while i is less than three, so zero, one, two. So while i is less than three, do this each time incrementing i by one. So i plus plus adds one, two, i. So now if I compile this, okay, compilation, compilation finished successfully, I'm going to go ahead and run it. Okay, so rock papers. I'm going to go ahead and choose rock. Okay, so the computer chose rock. We've got a tie game. Okay, if you chose scissors, the user one and rock and the computer one. Okay, so you can see how it is now repeating three times. So this assumes, of course, in advance that I know how many times I want to play. Now, there's not much point in repeating if we don't keep track of the wins. So what I'm going to do is I'm going to go up to here. I'm going to make two new variables. I'm going to say int user wins. Of course, when the game starts, that's going to be zero. Player wins equals zero. Oh, sorry, using the player is the same thing. Sorry about that. The computer wins. And what I want to do is when the player wins, when the user wins, add one and when the computer wins, add one. So let's go ahead and do that. So you can see down here where we have our choices, the user one. So I'm going to say user wins plus plus and that adds one to user wins. And fortunately I have these all kind of set together so it makes it a little bit easier. And then we have here where the computer won. So computer wins plus plus. So I'm just adding this. And again, if I'm going a little too fast, just pause it or you can always play it at a slower speed depending on how you like to do things. And so we add one to computer wins. And then what we'll do is we can do something like this. We can say, you know, print, sorry, system. I've been doing a lot of Python. System.out.println and we'll say user wins, colon. Plus user wins. Not wins, wins. Plus, don't forget the space there. Computer wins. And quote, plus computer wins. And so each time, notice this is actually inside the loop. Okay, note the indentation, note the location of this brace. So this will print out the number of wins each time. Let's go ahead and run that. Okay, computer chose scissors. The user, oops, you know what I forgot to do? I forgot to compile. So it was doing the old one. And I got an error. And of course I forgot the semicolon because I've been doing tons and tons of Python lately. Okay, so compile successfully. Let's go ahead and run it. And we're gonna have to run it one more time. So rock. Okay, the computer chose rock. We got a tie, user wins zero. Let me go ahead and try rock again. Ooh, computer chose paper, the computer won. Computer wins one. Uh-oh, let's go ahead and rock. And let's see here. And we got a tie. So you can see in this case, the computer won more than I did. So what we've decided, again, this particular version of the game, what we decided is that we're gonna play it three times and we're doing best of three. Actually, I'm gonna go ahead and just add a little, I don't like the formatting of that. I'm really picky about that kind of thing. Put a little forward slash in here, just so we have a little bit of space between. So if I compile that and run it, so you can see there's a little space there, so I can hit rock. And now there's a little space here so that you can see that this is a section, right? We can see a little bit better, you know, the parts of the program, or the output of the program, I should say. So yeah, so let's do rock, rock, rock. Oh, and the user won. Okay, you see user wins one, computer wins zero. Now what I might do at this point is after the loop, term in the final winner. So what I might do is I might say if user wins, is greater than computer wins, system.out.println, the user is the ultimate winner. I gotta put congratulations or something on there. Now, if that's not the case, I can say else if, else if, let's say user wins is less than computer wins. And I'm just gonna copy that because I don't flight type the whole thing in. And I'm gonna say the computer is the ultimate winner. And then there's the third case is if it's none of these, then I would say system.out, you know, the game is tied. And that's that. So let's go ahead and test that. So again, I'm gonna compile it and it's compiled. We can run it. So I'm gonna go ahead and hit rock. Okay, user one, sweet rock. It's a tie and rock. And you can see here, computer ones, computer wins, the game is tied. So at this point, yeah, I could try again, et cetera, et cetera. And so that's really just kind of an easy way of repeating some code. And in this case, you know, taking a simple loop and applying it to, you know, rock, paper and scissors. So just a couple of things just to, you know, refer back to this. It's good, probably, it's probably a good practice or maybe it'll help you, especially if you're a beginner is to figure out which section you want to repeat. And that's why I put these comments in here to make that a little bit, a little bit clearer. And then just, you know, again, you can highlight a section of code, at least in Genie. Most editors, I think, do this. And if you hit tab, it will indent the whole thing or shift tab to un-indent the whole thing. Again, Java, you don't have to do that, but it is a preferred way of doing things. And then here I've got matching braces, so opening and closing. And then once I've completed my three, I said my three, you know, times through the loop, my three iterations, then I'm gonna go ahead and determine the final winner, like that. So I could change, let's say I wanna do best out of five. I could change this to five. But three is probably where we'll keep that. So yeah, so that's basically it. It's not super complicated. And I hope that, again, I hope if you have some background on loops that this is fairly easy to follow. As I said, tomorrow I will be going over this with my students. So I hope they enjoy this. And I hope you enjoyed this. So yeah, thanks for watching. Yeah, you know, if you can click join below to join as a paid member. If you can't subscribe, hit the bell, like, you know, comment, whatever, I appreciate it all. So thanks everybody and have a fantastic week. Take care.