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:
- We need to accept a move from the player i.e. rock paper or scissors.
- We are also going to need the computer to select a move at random rock paper or scissors to play against the player
- Then, once we have those moves, we are going to compare them and print out the winner.
Algorithm:
- We start with randomized computer input by
- 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.
- 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].
- user move
- We take input of the move chosen by the player.
- Comparing their moves:
- First, we check if there's a tie between them using the equals() function.
- 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.