DEV Community

hellocodeclub
hellocodeclub

Posted on

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 a 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.

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.

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();

    }
}
Enter fullscreen mode Exit fullscreen mode

SEE HOW TO CREATE ALL 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! 🙂

Oldest comments (0)