Let's use Java code to make a rock, paper, scissors game! This classic game is possible to code in Java using basic concepts. If you want to learn more about making games in Java, this tutorial can be good inspiration for how to make simple games with code before getting into any complicated graphics or game user interfaces.
All you'll need is basic familiarity with Java, and experience using foundational concepts like conditionals and loops. Watch the tutorial video to see how we code this game step-by-step, and continue reading this post for more details.
Table of Contents
- Project Intro: 0:21
- Demo: 0:44
- General order of steps: 1:26
- Step 1: 1:56
- Step 2: 2:10
- Step 3: 2:56
- Step 4: 6:44
- Step 5-6: 9:33
- Extra features/suggestions: 13:13
Who is this project for?
This project falls under our Juni Java Level 1 coding class for kids. This beginner Java tutorial is for students that want a easy challenge project, about 50~75 lines long. You should review variables, input and output, and conditionals beforehand to get the most out of this project.
For learning outcomes, you'll get a lot of practice with conditionals, loops, and random numbers.
Click run to see the project yourself!
You can also view my project solution code if you get stuck.
Things to keep in mind:
- The user inputs their move by typing either rock, paper, or scissors.
- The opponent’s move is chosen randomly and the program prints out whether you won, tied, or lost.
- The program keeps asking the user to input their move after each round until they type in quit, which is when the program ends.
- If you type in something random, then the program prints “Your move isn’t valid!”
Steps to Build this Project:
- Ask the user to enter in their move.
- Check if the user entered a valid move.
- Randomly generate the opponent’s move.
- Use conditionals to figure out if you won, lost, or tied.
- Use a loop to continue asking the user for their move.
- Check if the user wants to quit.
How do we do each of these steps?
Step 1: Ask the user to enter in their move.
- First, we import the Scanner class to help us get input from the user, by adding
import java.util.Scannerto the top of our program.
- Then, we create our Scanner variable.
- Next, we print out a message asking the user to type in rock, paper, or scissors using
- We store their input in a String called
Step 2: Check if the user entered a valid move.
Use a conditional to check whether
myMove is equal to rock, paper, or scissors. If it isn’t equal to rock, paper, or scissors, then we can print out a message to the user stating that their move wasn’t valid!
Step 3: Randomly generate the opponent’s move.
In order to randomly generate the opponent’s move, we can use
Math.random() only generates a decimal in between 0 and 1, including 0 and excluding 1. But, since there are 3 possible moves in rock, paper, scissors, we need 3 possible random numbers! How can we do this?
Hint: We can use
Math.random() * 3to generate a random number in between 0 and 3, including 0 and excluding 3. We can then cast this value to an int so that our random numbers only include 0, 1, and 2 – no decimals in between.
Now we need to translate our random number, which can be 0, 1, or 2, into a String
opponentMove which is either rock, paper, or scissors.
Hint: Use conditionals to perform the following check: if the random number is 0, then the opponent’s move will be rock; if the random number is 1, then the opponent’s move will be paper; if the random number is 2, then the opponent’s move will be scissors.
Step 4: Use conditionals to figure out if you won, lost, or tied.
Let’s first check if it was a tie. If the opponent’s move was the same as your move, then it is a tie.
Hint: We can use
.equals()to check equality between two strings. If
myMoveis equal to
opponentMove, then it is a tie.
We can now go into an
else if and check if the user has won. Let’s go through all of the possible situations in which the user wins: the user has rock and the opponent has scissors, the user has scissors and the opponent has paper, or the user has paper and the opponent has rock.
- We can use
||to make one giant if statement!
- Let’s translate what we just said into code: if (
myMoveequals rock and
opponentMoveequals scissors) or (
myMoveequals scissors and
opponentMoveequals paper) or (
myMoveequals paper and
opponentMoveequals rock), then print that the user has won. Else, we can print that the user has lost.
Step 5: Use a loop to continue asking the user for their move.
We want to keep asking the user to enter their move. How can we do this?
Hint: We can put all of our code for asking the user for their move, checking if they entered a valid move, randomly generating the opponent’s move, and figuring out whether the user won, tied, or lost in a
But now our game will run forever! How do we make the while loop stop?
In order for a
while(true) loop to stop, we need a break statement in the body of the loop. We can ask the user if they want to quit the game.
Step 6: Check if the user wants to quit.
- In our print statement asking the user for their move, we can also ask them to type quit if they want to end the game.
- Inside our while loop, let’s use an if statement to check whether or not the user entered quit.
- If the user did type quit, we can exit out of the while loop using a break statement.
Challenge Yourself with Extra Features
Check out the bonus extended project features below, or see the solution code if you get stuck.
Add a score counter that counts the number of wins and losses.
- Outside of our while loop, create two int variables: one for wins and one for losses.
- Increment wins and losses when the user wins and loses, respectively.
- We can use shorthand notation ++ to increment wins and losses.
- After each game, print out the wins and losses.
- Once the user quits, use conditionals to print out whether the user won more games, lost more games, or won and lost an equal number of games.
- Make your own moves! For example, “fire defeats all MWAHAHA!” (reference to Friends).
Great job! Check out more tutorials.
Thanks for watching and hope you had fun making this Rock, Paper, Scissors project with me!
Every week, we’ll be posting project tutorials like this one, for different coding languages and experience levels.
Visit our coding projects blog page to find our other tutorials in more coding languages! You can also subscribe to our newsletter using the form to the right of this page for more updates on our programs and newest coding tutorials.
Need more help, or want to keep learning?
Looking up your coding questions is one of the best ways to learn! Another great way to learn is from an experienced coder or instructor.
Juni Learning Computer Science Instructors work closely with students ages 8-18, and are specially trained to adapt to each child's unique learning style, pace, and interests.
Read more about our online coding classes for kids, or speak with a Juni Advisor today by calling (650) 263-4306 or emailing firstname.lastname@example.org to learn which course is best for your student’s coding journey.