What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Rock Paper Scissors Program in Java (full code)

  • May 24, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Harshita Rajput
Rock Paper Scissors Program in Java (full code)

We all played the interesting game of rock, paper, scissors in our childhood.  But have you ever thought that one day you will learn to code the same game? In this article, we will explain how to implement Rock, Paper, Scissors Game in Java with full program code.

What is the Rock, Papers, Scissors?

It is a two-player game where both simultaneously choose their move out of available moves i.e. rock, paper, and scissors. Based on their moves and rules, the winner is decided. The rules are that Rock beats scissors but loses to paper, Paper beats rock but loses to scissors, and Scissors beats paper but loses to rock.

Major steps to code the game:

  1. We need to accept a move from the player i.e. rock paper or scissors.
  2. We are also going to need the computer to select a move at random rock paper or scissors to play against the player
  3. Then, once we have those moves, we are going to compare them and print out the winner.

Algorithm:

  1. We start with randomized computer input by
    1. first, create an array of strings and we store the moves i.e. rock paper scissors in that. So the length of the array = 3.
    2. the computer move would be the randomized element of that list and for that, we use the Random() function on the indices of the array so that we get the randomized index between [0,2].
  2. user move
    1. We take input of the move chosen by the player.
  3. Comparing their moves:
    1. First, we check if there's a tie between them using the equals() function.
    2. Now we check all other possible combinations of rock, paper, and scissors using the if else condition and decide who is the winner.

Rock Paper Scissors code in Java

Now that you know the basics, below is the complete code to implement Rock, Paper, Scissors game in Java. We have used a simple if-else statement for this program. 

import java.util.*;

public class RockPaperScissor {
	
	public static void main(String[] args) {
	
		Scanner scn = new Scanner(System.in);
		
		while(true) {
			
		//1. RANDOMIZED COMPUTER MOVE
		
			// array of string containing available moves.
			String [] availableMoves = {"Rock", "Paper", "Scissors"};
		
			// using Random() function on indices of array so that it chooses a random move.
			String computerMove = availableMoves[new Random().nextInt(availableMoves.length)];
		
			System.out.println("Computer has chosen it's move.");
			System.out.println();
			System.out.println("Now it's your turn to choose. Good Luck!");
			System.out.println();
		
		//2. PLAYER MOVE
		
			//input
			String userMove;
		
			// loop until the user chooses the correct move
			while(true) {
				System.out.println("Please choose your move from these available moves : 'Rock' 'Paper' 'Scissors' ");
				System.out.println("Enter the move you chose : ");
				userMove = scn.nextLine();
			
				// checking if user's move is one of the available moves or not
				if(userMove.equals("Rock") || userMove.equals("Paper") || userMove.equals("Scissors")){
					System.out.println();
					break;
				}
			
				// if user didn't enter a valid input
				System.out.println();
				System.out.println("Invalid Move!!");
				System.out.println("Please enter the move from the available moves only!");
				System.out.println();
			}
		
			//printing what computer chose
			System.out.println("Computer chose : " + computerMove);
		
		//3. COMPARING THE MOVES & DECIDING THE WINNER
		
			// checking for a tie
		
			if(userMove.equals(computerMove)) {
				System.out.println("Its a tie!");
			}
		
			//checking for all other moves possible
		
			else if(userMove.equals("Rock")) {
			
				if(computerMove.equals("Paper")) {
					System.out.println("Computer won!");
					System.out.println("Better luck next time!");
				} 
				else if(computerMove.equals("Scissors")) {
					System.out.println("You won!");
					System.out.println("Congratulations!");
				}
			}
		
			else if(userMove.equals("Paper")) {
			
				if(computerMove.equals("Rock")) {
					System.out.println("You won!");
					System.out.println("Congratulations!");
				} 
				else if(computerMove.equals("Scissors")) {
					System.out.println("Computer won!");
					System.out.println("Better luck next time!");
				}
			}
		
			else if(userMove.equals("Scissors")) {
			
				if(computerMove.equals("Paper")) {
					System.out.println("You won!");
					System.out.println("Congratulations!");
				} 
				else if(computerMove.equals("Rock")) {
					System.out.println("Computer won!");
					System.out.println("Better luck next time!");
				}
			}
			
			System.out.println();
			String playAgain;
			System.out.println("Do you want to play again? ");
			
			// loop until the user chooses the correct option
			while(true) {
				
				System.out.println("Type 'yes' or 'no' ");
				playAgain = scn.nextLine();
				
				if(playAgain.equals("yes") || playAgain.equals("Yes") || playAgain.equals("no") || playAgain.equals("No")) {
					System.out.println();
					System.out.println("*****************************************************************************");
					System.out.println();
					break;
				}
				System.out.println();
				System.out.println("Invalid Input");
				System.out.println();
			}
			
			if(playAgain.equals("no") || playAgain.equals("No")) {
				break;
			}
		}
	}
}

 

Output 1 (Computer Won):

Computer has chosen it's move.

Now it's your turn to choose. Good Luck!

Please choose your move from these available moves : 'Rock' 'Paper' 'Scissors' 
Enter the move you chose : 
Paper

Computer chose : Scissors
Computer won!
Better luck next time!

Do you want to play again? 
Type 'yes' or 'no' 
Yes

*****************************************************************************

 

Output 2 (Player wins):

Computer has chosen it's move.

Now it's your turn to choose. Good Luck!

Please choose your move from these available moves : 'Rock' 'Paper' 'Scissors' 
Enter the move you chose : 
Rock

Computer chose : Scissors
You won!
Congratulations!

Do you want to play again? 
Type 'yes' or 'no' 
yes

*****************************************************************************

 

Output 3 (It's a Tie):

Computer has chosen it's move.

Now it's your turn to choose. Good Luck!

Please choose your move from these available moves : 'Rock' 'Paper' 'Scissors' 
Enter the move you chose : 
Rock

Computer chose : Rock
Its a tie!

Do you want to play again? 
Type 'yes' or 'no' 
No

*****************************************************************************

 

Time & Space Complexity

The time complexity of the above code comes out to be O(1). As no looping or recursion statements are present in code that depends on the size of the input. This code executes a fixed set of operations every time regardless of input size. The space complexity would also be O(1). The amount of memory space used by the program remains constant regardless of the size of the input.

But there are many other java projects for beginners to practice their new skills.

Conclusion  

Games like these can be easily coded provided we know the logic and basic syntax of the language we are to write code in. By using simple if-else statements we can build up a Rock, Paper, Scissors Game in Java. 

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Harshita Rajput
I am a highly motivated individual with a deep passion for technology and programming. My exceptional performance in programming contests, and my dedication to solving data structures and algorithmic problems on global platforms, demonstrate my commitment to the field of technology.