Codementor Events

How To write a Rock Paper Scissors Game in Java - Step by Step

Published May 09, 2021Last updated Nov 04, 2021
How To write a Rock Paper Scissors Game in Java - Step by Step

In this article, I want to help you to design a Rock Paper Scissors Game in Java step by step. And first step is overcoming your self-doubt and concentrate. You will put together everything you know about programming to build a rock paper scissors game that makes you feel proud of yourself. Let's see how you will achieve this step by step.

We will start by analysing the game and defining the key entities in our problem. We will use the object oriented programming approach, meaning we will turn the entities into classes. Next we will define an skeleton implementation, in other words creating the classes but no code. And last we will fill up the classes with code and have a working rock paper scissors game.

WATCH STEP BY STEP TUTORIAL ON YOUTUBE

Define How your Rock Paper Scissors Game works

Let’s start by defining how the game should work. You will be creating a rock paper scissors text-based java game like the one you can see below:

rock_paper_scissor.gif

The first step is writing down the steps of what the game should. By doing so, it can help us identify the classes and actions that we will need our game. The steps are:

  1. The Game welcomes the user.
  2. The game requests the user to enter his choice(rock, paper or scissors)
  3. The user types the selected option.
  4. The game picks randomly among rock, paper or scissors.
  5. Calculates the results.
  6. The game updates the total score( how many games you play in total and won games)
  7. Display the score.
  8. Ask the user if he wants to continue.

Writing down how the game will work is a great start. It helps you identify some of the main entities of the problem, so later you can turn them into class. In this case, we will create the following classes:

RockPaperScissorsGame: the entry point of the application
Game Controller: this class will represent the game itself, containing the scores, the player choices, and operations related to the game mechanism like calculate who won, etc.
GameOption: This class represents different options the player can select: rock, paper, or scissors.
Player: It helps indicate you won, the user, the machine, or the tie.
Score: It contains the total number of games player, and the number of games played by the user.

Create a Skeleton Implementation

Now that we have an overall view of how the rock paper scissors java game will work and the main classes in our program, we can start creating them and give shape to our game.
At this option, I will advise you not to add implementation. The skeleton should contain the classes and the empty operations of these classes. These operations define how the classes interact with each other. For now the implementation is not necessary.

RockPaperScissorsGame class

This class needs a main method since it is the entry point of our java program. Since we are going to interact with the game, we will create an instance of the class to access its operations.

public class RockPaperScissorsGame {

public static void main(String[] args){

        GameController gameController = new RockPaperScissorsGameController();

    }
}

See how to create the other classes here

Complete the Implementation

Now we have created our application’s skeleton, and we have a better overall understanding of how the game will work. So it is an excellent time to start filling the gaps and get the game working.

Let’s go back to our game’s entry point. The entry point which is the main method. Every statement executed by the game will be inside the main method.

Since you need to receive inputs from the user, we will need a Scanner class. Additionally, the game will run until the user indicates he wants to exit, so we will need a do-while loop to keep repeating.

Inside the loop, the game will:

  1. Request the user selection and save it.
  2. Pick a random option
  3. Calculate the result
  4. And finally, display the game results and ask the user if he wants to continue.

See all java code to complete the game here.

I hope you enjoy this article, and thank you so much for reading and supporting this blog! Happy Coding! 🙂

Discover and read more posts from Marta Rey
get started
post comments1Reply
Marcilo Alex
2 years ago

Sure! Here’s a step-by-step guide on how to create a Rock Paper Scissors game in Java without coding:

Open your preferred IDE or code editor.
Create a new Java project and name it “RockPaperScissors”.
Inside the project, create a new class called “RockPaperScissors”.
Create a method called “playGame()” inside the “RockPaperScissors” class.
Use the “Scanner” class to prompt the user to enter either “rock”, “paper”, or “scissors”.
Generate a random number between 1 and 3 using the “Random” class. Map the random number to either “rock”, “paper”, or “scissors”.
Compare the user’s input with the computer’s choice and determine the winner of the game.
Ask the user if they want to play again, and repeat the game if they do.
You can add the link to Last Minute Flights, such as https://galajourney.com/last-minute-flights/, in the console output to provide an example of a website offering last-minute flight deals.
That’s it! By following these steps, you can create a simple Rock Paper Scissors game in Java. Don’t forget to test your game thoroughly to ensure it works as expected.