The Rule of the Game are as follows:
- Rock beats Scissors.
- Scissors beats Paper.
- Paper beats Rock.
- Who ever wins 5 times first is the Winner.
- In this example the game will be between a user and the computer.
Learn Core Java Properly through my Private Core Java Online Training to be able to write games in java easily.
Steps of the Rock Paper Scissors Java Program
- We will create a class Player.java that represents the user who is going to play the game with the computer.
- The class Computer.java represents the Computer that is playing with the user.
- Driver.java is a helper class containing the Rock Paper Scissor constant variables and the business logic code.
- RPS.java is the class containing the main function where the program execution will begin.
- We will ask the user to enter his name. Then select one among ROCK PAPER SCISSORS.
- Then we will let the computer randomly select ROCK PAPER SCISSORS.
- We will use the business logic function to find out who wins. And every time we will display the selections made and who won.
- The can decide to end the game any time.
- We will keep a track of who wind 5 times first. And then display the winner.
Player.java will represent the User playing with the computer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import java.util.Scanner; public class Player { private String name;// Represents the Players name Scanner sc = new Scanner(System.in); // sc will help to take inputs from keyboard public String getName() { return name; } public void setName(String name) { this.name = name; } public void askName() {// Will let the user enter his name System.out.println("Please enter your name"); name = sc.next(); } public int getInput() { // This function lets the user do the selection among // ROCK PAPER SCISSORS // and return what has been selected System.out.println("Select ROCK PAPER SCISSOR"); String input = sc.next(); input = input.toUpperCase(); char c = input.charAt(0); // checking the 1st character in the input if (c == 'R') return Driver.ROCK; else if (c == 'P') return Driver.PAPER; else if (c == 'S') return Driver.SCISSORS; else { getInput(); return 0; } } public boolean playAgain() { // This function will ask if the user wants to // play again and return true or false accordingly. sc = new Scanner(System.in); System.out.print("Do you want to play again? "); String userInput = sc.nextLine(); userInput = userInput.toUpperCase(); return userInput.charAt(0) == 'Y'; } } |
Computer.java will represent the computer that is playing with the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Random; public class Computer { // This function helps the computer do the selection among ROCK, // PAPER,SCISSORS // we have used Random class from java.util. public int getInput() { Random random = new Random(); int input = random.nextInt(3) + 1; return input; } } |
Driver.java is a helper class containing constants and business logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class Driver { //These instance variables will act as constants through out the project public static final int ROCK = 1; public static final int PAPER = 2; public static final int SCISSORS = 3; //This function will help to display who selected what public static void display(String who, int s) { switch (s) { case Driver.ROCK: System.out.print(who + " selected ROCK "); break; case Driver.PAPER: System.out.print(who + " selected PAPER "); break; case Driver.SCISSORS: System.out.print(who + " selected SCISSORS "); break; default: break; } } //This function contains the actual buisness logic //and help to decide who won public static int compareSelections(int userSelection, int computerSelection) { if(userSelection==computerSelection) return 0; switch (userSelection) { case ROCK: return (computerSelection == SCISSORS ? 1 : -1); case PAPER: return (computerSelection == ROCK ? 1 : -1); case SCISSORS: return (computerSelection == PAPER ? 1 : -1); } return 0; } } |
RPS.java contains the main method. This is the starting point of program execution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | public class RPS { //Instance variables declaration private Player player; private Computer computer; private int playerScore; private int computerScore; private int numberOfGames; //Default Constructor initializing the instance variables public RPS() { player = new Player(); computer = new Computer(); playerScore = 0; computerScore = 0; numberOfGames = 0; } public static void main(String[] args) { RPS rps = new RPS();//Initialization occured. rps.getplayerName();//Getting user name rps.startGame(); } //This function is called recursively till the player wants to play. //It is even exited if the user of computer completes the first 5 wins. public void startGame() { int playerinput = player.getInput(); Driver.display(player.getName(), playerinput); // Get moves int computerinput = computer.getInput(); Driver.display("computer", computerinput); // Compare moves and determine winner int compareResult=Driver.compareSelections(playerinput, computerinput); switch (compareResult) { case 0: // Tie System.out.println("Tie!"); break; case 1: // player wins System.out.println(player.getName()+ " Beats " + "computer" +" You won!"); playerScore++; break; case -1: // Computer wins System.out.println("Computer" +" Beats "+ player.getName()+" You Lost!"); computerScore++; break; } numberOfGames++; if(playerScore==5) { System.out.println(player.getName()+" HAS WON THE GAME............."); new RPS(); } if(computerScore==5) { System.out.println("Computer "+" HAS WON THE GAME............"); new RPS(); } // Ask the player to play again if (player.playAgain()) { System.out.println(); startGame(); } else { printStats(); } } //helps to get the users name public void getplayerName() { player.askName(); } //Prints the result. public void printStats() { System.out.println("Number of games played is "+numberOfGames); System.out.println(player.getName()+"'s score "+playerScore); System.out.println("Computers score "+computerScore); } } |
Output of the Rock Paper Scissors Java Game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | Please enter your name Chinmay Select ROCK PAPER SCISSOR ROCK Chinmay selected ROCK computer selected SCISSORS Chinmay Beats computer You won! Do you want to play again? y Select ROCK PAPER SCISSOR Scissor Chinmay selected SCISSORS computer selected PAPER Chinmay Beats computer You won! Do you want to play again? y Select ROCK PAPER SCISSOR ROCK Chinmay selected ROCK computer selected PAPER Computer Beats Chinmay You Lost! Do you want to play again? y Select ROCK PAPER SCISSOR ROCK Chinmay selected ROCK computer selected PAPER Computer Beats Chinmay You Lost! Do you want to play again? y Select ROCK PAPER SCISSOR SCISSORS Chinmay selected SCISSORS computer selected SCISSORS Tie! Do you want to play again? y Select ROCK PAPER SCISSOR PAPER Chinmay selected PAPER computer selected ROCK Chinmay Beats computer You won! Do you want to play again? y Select ROCK PAPER SCISSOR ROCK Chinmay selected ROCK computer selected SCISSORS Chinmay Beats computer You won! Do you want to play again? y Select ROCK PAPER SCISSOR PAPER Chinmay selected PAPER computer selected SCISSORS Computer Beats Chinmay You Lost! Do you want to play again? y Select ROCK PAPER SCISSOR ROCK Chinmay selected ROCK computer selected SCISSORS Chinmay Beats computer You won! Chinmay HAS WON THE GAME............. Do you want to play again? n Number of games played is 9 Chinmay's score 5 Computers score 3 |