How To Make a Rock, Paper, Scissors Game in Java

Two people are playing a game called Rock, Paper, Scissors.Both people would say "rock, paper, scissors" and then simultaneously form one of three objects with an outstretched hand.The hand formations determine the winner.Scissors beats paper, paper beats rock.If both players play the same hand formation, it's a tie.We will use Java to create a simple game where one player is the user and the other is a computer.

Step 1: The main class is called RockPaperScissors.

We are going to write the game in this class.You can choose to call it Game or Main.The main method and the constructor should be written in method declarations.The public class RockPaperScissors has a static void main.

Step 2: There is an enumeration for the hand gestures.

An enumeration allows us to define our constants, which means that using the enumeration is a better design than using strings.We will call our type Move with the values.There is a private enum Move Rock, Paper, Scissors.

Step 3: There are two private classes User and Computer.

Our players will be represented by these classes.You can make these classes public.The User class will prompt the user for either rock, paper, or scissors, so we will need to write a getMove method.The computer class needs to have a getMove method to make a move.placeholders will be put in these methods.The User class requires a method that sets up the Scanner object.The Scanner will be put as a private field for the user to use.We need to write an import statement for the class at the top of our code.The Computer class does not require a constructor, so we don't need to write one when we start the Computer object.Here is what the RockPaperScissors class looks like now.

Step 4: Write the method for the class.

A random Move will be returned by this method.The values method can be used to get an array of Move enumerations.We need a random index between 0 and the length of the values array to choose aRandom Move enumeration.We can use the nextInt method of the Random class to do this.When we get the random index, we can move it from our values array.Return moves[ index]), Random random, int index, and move values are used.

Step 5: Write the method for the User class.

The method will return a Move based on the user's input.The user will be expected to write "rock", "paper" or "scissors".The user needs to be prompted for an input: System.out.print.").The nextLine method of the Scanner object can be used to get the user input.We need to check if the user has submitted a valid move, but we can be less strict if they have spelled a word wrong.We won't care about the case if the first letter of the user input is "R", "P" or "S".If the user hasn't entered a correct input, we will prompt them again.We will return a move based on what the user has put in.Prompt the user System.out.print "Rock, paper, or scissors?""); // Get the user input string userInput, user Input to Upper Case, and char firstLetter.Again, Prompt.Return getMove.

Step 6: The User class requires a playAgain method.

The user should be able to play the game many times.In order to determine if the user wants to play again, we need to write a method that returns a boolean.The method we are using is similar to the one we used in the constructor to get a "Yes" or "No" from the user.If the first letter is Y, we can determine if the user wants to play again.The user does not want to play again if there is any other input."Do you want to play again?"Return userInput.charAt(0) if 'Y' is desired.

Step 7: The RockPaperScissors class can connect the User and Computer classes.

We can focus on our actual game now that we have finished writing the User and Computer classes.Private fields can be created for the User and Computer classes in the RockPaperScissors class.When we're playing the game, we will need to access these fields.The RockPaperScissors class has a constructor.We need to initiate the score in userScore and computerScore fields as 0 in the constructor to keep track of it.We need to keep track of the number of games, which will be a field initiated as 0.

Step 8: There is a method that tells us which move wins in each case.

If the moves are the same and the current move beats the other move, we need to write a compareMoves method.The winner of the game will be determined by this.If the moves are the same, we will return 0 in order to implement this method.Write a switch statement if you want to return 1 or -1.If this move beats the other move, the move will be returned 1 if it loses.1 : -1); case PAPER: returnThe case requires return (otherMove) and paper.Should never reach here return 0;

Step 9: The RockPaperScissors class has a start game method.

The method will be used to play the game.Put a System.out.println in the method.There is a public void start Game.");

Step 10: The user and computer can be moved.

To get the user and the computer's moves, use the getMove method from the User and Computer classes.System.out.println("nYou played " + userMove + ")"; System.out.println"

Step 11: Determine if the user won or lost by comparing the two moves.

The compareMoves method can be used to determine if the user won or not.The user score should be incremented by 1.The computer score should be incremented by 1 if the user loses.Don't increment any of the scores if there was a tie.The number of games played should be increased.case 0: // Tie System.out.println."); break; case 1: User wins System.out.println.You won!The computer won the System.out.println.You didn't win."); computerScore; break; numberOfGames

Step 12: Ask if the user would like to play again.

If the user wants to play again, call start Game.The statistics of the game can be printed out with printGameStats.The method will be written in the next step.If system.out.println, start Game, and else are used.

Step 13: Write the method.

The method will show the statistics of the game: number of wins, losses, ties, games played, and percentage of games won by the user.The percentage of games won is determined by the number of wins and ties.System.out.printf is used to print out text.Private void print game statistics int wins and losses, and number of games - userScore and computerScore.

Step 14: The game begins in the main class.

In the main class, create an instance of the RockPaperScissors class and call the start game method.There is a public static void main.

Step 15: Take a test of your game.

Now that we have finished writing the Rock, Paper, Scissors game, it's time to test everything out!

Related Posts:

  1. How do you play rock paper?
  2. What scissors are best for cutting hair?
  3. Can you flush paper towels safely?
  4. How to Play Candyland the Board Game: Rules and Instructions is available in PDF.