Create Rock Paper Scissors in Java in 10 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in this video we are going to create a rock paper scissors game in java it'll be fast easy to make and great for beginners to practice your fundamentals before we get started as always the full source is available in the link down below in the description so go and get it be sure to hit that subscribe button to get more java lessons and tutorials like this every week there's just a few pieces of this program we'll need in order to make it work properly so we're going to need to accept a move from the player rock paper or scissors from the keyboard and we're also going to need the computer to select a move at random rock paper or scissors to play against the player and then once we have those moves we're going to compare them and print out who wins okay so let's just get started with the randomized computer input first so i think what we're going to do for this is create a string array called rps for rock paper or scissors and what we're going to put in that array are just three strings rps for rock paper and scissors and then the computer's move is just going to be a randomized element of that list it's going to be the rock paper scissors at random so we'll just create our list here of r p and s and there we go okay all right now to get the computer move we'll say string computer move and we're going to set it equal to an element from this array um but what the index we want to get is random we want to get a random index between 0 and 2 for the zero first or second element of this list rps and so to do that we'll say new random because we want a random generator here and we're going to need to import that java util random and then so then we'll do next int where what will pass in is going to be the size of that array rps dot length so rps.length is the length of this array which is 3 and when we pass a 3 into this random next int method it's going to give us a random number between zero and one less than the number we pass in and since we're passing in three the length of this array is going to give us a zero one or a two at random so that's exactly what we want we have a random computer move all right so now we have to get the move from the player from the user and to do that we're going to use what you might expect scanner so we've got a scanner scanner equals new scanner system dot in for keyboard input again we'll do our importing javautil scanner and now we're going to print sis out please enter your move r p or s for rock paper or scissors and then to actually get the player's move we're going to write string player move equals scanner dot next line and that is going to get the next line of input from the user via the keyboard but now what if the player types in garbage like something anything other than rp or s well we'd like to validate for that but we want to do it quickly here we're pressed for time so we're just going to write a little while loop that once we get correct input we're going to jump out of so what we can do is just write a while true loop so that'll loop forever until we write a break statement that kicks us out of it right so what we're going to do is we've got this player move from the player and actually let's go ahead and move this declaration outside of this while loop because we're going to need that player move outside of the while loop when we're done with it and so basically all we have to do here is check to see if the player entered rp or s and if they did we can break out of the loop otherwise we're going to write a complaint message to the user and make them put in something else so just if player move dot equals r or and we're just going to copy and paste this with the other two or player move equals p or player move equals s get rid of this last or here we don't need it then break and then after this if if we're still in this loop we know that the player didn't enter valid input so we need to complain to them about it so just sis out player move plus is not a valid move so it'll print back to them what they typed in and say hey that's not a valid move it'll jump back to the top of the wild loop and force them to try again okay so now we have a valid move from the player we should have a valid move from the computer and now we just need to compare them so first i think what we should do is print out what the computer chose and then print out the results of the game so what the computer chose sys out uh we can just say computer played space and then plus to concatenate the computer move so now we can see what the computer played so first what i think we can check for is a tie i think that'll be the simplest check to make so that's just if player move dot equals computer move so if those are the same we'll just print out uh the game was a tie the game was a tie all right so else else if so now we're going to go through all the different conditions that a player could have chosen it compared to what the computer chose so we're going to go through rock against the things the computer could have chosen then paper then scissors so we've got else if player move dot equals r so the player chose rock now we already know the computer didn't choose rock because the game didn't end in a tie we've already checked for that right above so we can just say if computer move dot equals p so the player chose rock and the computer chose paper then the computer wins so in other words you lose so we'll just do a sis out you lose and then otherwise else if the computer move uh was an s scissors then we can say you win you chose rock and the computer chose scissors all right so let's copy this whole else if for the paper so the player move was paper so we know the computer didn't choose paper because we already checked for a tie so if the computer shows rock then you win because you chose paper the computer shows rock and then if the computer chose scissors you lose because scissors beats paper and then let's copy this one more time for if you chose scissors then if the computer chose paper then that means you win scissors beats paper otherwise if the computer chose rock then you write you lose because rock beats scissors okay i think that's it let's run our program and do a quick test okay please enter your move rock paper or scissors i'll choose paper computer played rock and you win yes paper beats rock so awesome that worked let's try it a few more times country move rock computer plays paper and i lose but it is working okay now let's test if i put in some garbage input like uh blah it's not a valid move please enter your move garbage okay that is working the validation is working now i can put in scissors computer played scissors game was a tie all right so i think our logic is working and that's the main part of the game the hard part is is good to go but now i think i just want to i hate having to restart the game every time to play again and i would really like to just have the option to play again if i want to so what we can do is just loop through this entire program in a while loop and break out when the user doesn't want to play anymore all right so to do that way up at the top we're going to put a while loop around just about our whole program so while true we're going to do this whole thing so i highlight the whole thing move it over and end our while loop here at the very end but one part that we actually want to move out of this loop is the scanner declaration we don't want to create a new scanner each time java gets really weird if you create multiple system.n scanners so we'll remove that out of the loop we can reuse it every iteration of the loop with no problems so then way at the bottom of our program just before the end of our while loop we can print something out to the user that just says play again and then we just say you know y or n or yes or no and then we can create a string for play again and set it equal to what the user types in next so that's going to be scanner.nextline all right so we can do something pretty simple here we can just say if not play again dot equals y right so basically if they didn't say yes then we'll just assume no they don't want to play again so if it doesn't equal y just say break and that will break out of this uh overall while loop and end the program all right let's go ahead and run this again and make sure our outer loop works enter your move let's put in some garbage make sure that still works okay so i can choose rock computer played rocket was a tie play again yes now please enter your moves scissors computer played rock i lose but it is working our loop is working let's play it one more time yes enter your move um paper computer played rock i win play again no and the game ends awesome i cool i think that did it so it's a pretty neat little program right i know it seems simple there's a lot of little pieces of it that are you know part of your java fundamentals like while loops getting a random number some arrays some string comparisons a set of if and else if statements to do some logic and you got keyboard input from the user so there's a lot of little things that you did here so even though it's quite a simple little program it's fun to play and you've used a lot of your java fundamentals getting it going now if you've watched any of my videos you know there's still a step left this scanner we've got a resource leak and scanner is not closed now we got to and like on all of our programs you've got to be a good boy scout at the end of our program close your scanner just makes you feel like a fine upstanding citizen now if you enjoyed making this game or perhaps you want a little bit more of a challenge go and check out either my hangman video or my tic-tac-toe game video both of those are a decent amount more complicated but a whole lot of fun to make and i think you'll really enjoy them now if you enjoyed this video or learned something please let me know with a like and if you have questions or run in any sort of problems please let me know in the comments i'd be more than happy to help and be sure to click that subscribe button to get more java tutorials and lessons like this in the future and see you in the next video
Info
Channel: Coding with John
Views: 12,911
Rating: undefined out of 5
Keywords: java rock paper scissors, rock paper scissors java, rock paper scissors, java beginner game tutorial, java beginner project, codingwithjohn, coding with john, rock paper scissors java tutorial, java rock paper scissors( if else), java rock paper scissors loop, rock paper scissors loop, rock paper scissors loop java
Id: DyqMglmrido
Channel Id: undefined
Length: 10min 23sec (623 seconds)
Published: Wed Jan 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.