Rock Paper Scissors in Java

Introduction

We are going to create a simple program to play rock paper scissors in Java. Basic game to simply play and kill time. Most of us already know this game because we all played it in our childhood. Instructions on how to play are also given in the project itself.

Create a new java project in eclipse. Create a new package “rps” (Nothing special but Rock Paper Scissors) in the src directory. All the coming up classes have to be in this class.

Project Overview: Rock Paper Scissors in Java

Project NameRock Paper Scissor in Java
AbstractIt’s a GUI-based project used with the Swing for the
Rock Paper Scissors game.
Language/s UsedJava
IDEEclipse IDE
Java Version
(Recommended)
Java SE 18.0. 2.1
TypeDesktop Application
Recommended forIntermediates of Java

Coding Rock Paper Scissors in Java

So let’s get started with the code.

As an overview, there are a total of 4 classes. These are the Main, Front page, Game page, and help page. Let’s get deep into this.

1. Main Class

Name: “Main.java

It does especially nothing but calls the main page. It is the class that is first executed on the run of the program.

package rps;
public class Main {
public static void main(String[] args) {
FrontPage.front();
}
}

2. Front Page

Name: “FrontPage.java

It is the front page of the program. It will be shown during the execution of the program. Containing a logo, play, help, and quit buttons. The play button is to start the game, the Help button is for the instruction and the Quit is used to exit the application.

package rps;

import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class FrontPage extends JFrame{
	public static void front() {
		JFrame frame = new JFrame();
		Font big = new Font("Segoe Script", Font.PLAIN, 150);
		Font small = new Font("Segoe Script", Font.PLAIN, 25);
		
		//-----------------HEAD----------------------------------
		JLabel r = new JLabel("R");
		r.setBounds(30, 20, 150, 150);
		r.setFont(big);
		r.setForeground(Color.decode("#373737"));
		JLabel p = new JLabel("P");
		p.setBounds(140, 20, 150, 150);
		p.setFont(big);
		p.setForeground(Color.decode("#373737"));
		JLabel s = new JLabel("S");
		s.setBounds(250, 20, 150, 150);
		s.setFont(big);
		s.setForeground(Color.decode("#373737"));
		JLabel ock = new JLabel("ock");
		ock.setBounds(70, 115, 50, 25);
		ock.setFont(small);
		ock.setForeground(Color.decode("#373737"));
		JLabel aper = new JLabel("aper");
		aper.setBounds(185, 115, 100, 25);
		aper.setFont(small);
		aper.setForeground(Color.decode("#373737"));
		JLabel cissors = new JLabel("cissors");
		cissors.setBounds(330, 115, 100, 25);
		cissors.setFont(small);
		cissors.setForeground(Color.decode("#373737"));
		frame.add(r);
		frame.add(p);
		frame.add(s);
		frame.add(ock);
		frame.add(aper);
		frame.add(cissors);
		//-------------------------------------------------------
		
		//-------------------------PLAY BUTTON--------------------------------
		JButton play = new JButton("PLAY");
		play.setBounds(150, 190, 150, 50);
		play.setFont(small);
		play.setForeground(Color.decode("#e5dfe8"));
		play.setBackground(Color.decode("#373737"));
		play.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Game game= new Game();
				game.gameWindow();
			}
		});
		frame.add(play);
		//-------------------------------------------------------------
		
		//----------------------------HOWTO-----------------------------------
		JButton help = new JButton("HELP");
		help.setBounds(150, 270, 150, 50);
		help.setFont(small);
		help.setForeground(Color.decode("#e5dfe8"));
		help.setBackground(Color.decode("#373737"));
		help.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Help help = new Help();
				help.helpWindow();
			}
		});
		frame.add(help);
		//---------------------------------------------------------------------
		
		//-------------------------------QUIT----------------------------------
		JButton quit = new JButton("QUIT");
		quit.setBounds(150, 350, 150, 50);
		quit.setFont(small);
		quit.setForeground(Color.decode("#e5dfe8"));
		quit.setBackground(Color.decode("#373737"));
		quit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		frame.add(quit);
		//---------------------------------------------------------------------
		
		//------------FRAME---------------------------------------------
		frame.setSize(450,450);
		frame.setResizable(false);
		frame.setLayout(null);
		frame.setUndecorated(true);
		frame.setLocationRelativeTo(null);  
		frame.setVisible(true);
		frame.setFocusable(true);
		frame.getContentPane().setBackground(Color.decode("#e5dfe8"));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//----------------------------------------------------------------
	}
}

Each area is commented into blocks for easy understanding of the code.

first view of Rock Paper Scissors in Java
Home Page

3. Game menu

Name: “Game.java

The main part of the application. It is the window that runs the game, calculates scores, declares winners, and does all the stuff related to the game.

package rps;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.util.Random;

public class Game {
	
	JLabel rndm;
	JLabel win;
	JLabel loss;
	JFrame frame;
	JButton rock;
	JButton paper;
	JButton scissor;
	JButton again;
	JLabel status;
	JLabel option;
	JLabel col;
	JLabel you;
	
	
	public void gameWindow() {
		frame = new JFrame();
		Font small = new Font("Segoe Script", Font.PLAIN, 12);
		Font score = new Font("Segoe Script", Font.BOLD, 50);
		
		//--------------------BACK BUTTON-------------------------
		JLabel back = new JLabel("< BACK");
		back.setBounds(20, 20, 150, 30);
		back.setForeground(Color.decode("#373737"));
		back.setFont(new Font("Segoe Script", Font.BOLD, 20));
		frame.add(back);
		back.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		//----------------------------------------------------------
		
		//-----------------SCOREBOARD------------------------------
		you = new JLabel("YOU      COMPUTER");
		you.setBounds(165, 150, 150, 50);
		you.setForeground(Color.decode("#373737"));
		you.setFont(small);
		win = new JLabel("0");
		win.setBounds(165, 185, 150, 50);
		win.setForeground(Color.decode("#373737"));
		win.setFont(score);
		col = new JLabel(":");
		col.setBounds(210, 185, 150, 50);
		col.setForeground(Color.decode("#373737"));
		col.setFont(score);
		loss = new JLabel("0");
		loss.setBounds(235, 185, 150, 50);
		loss.setForeground(Color.decode("#373737"));
		loss.setFont(score);
		frame.add(win);
		frame.add(loss);
		frame.add(col);
		frame.add(you);
		//---------------------------------------------------------
		
		//---------------STATUS------------------------------------
		status = new JLabel();
		status.setBounds(125, 230, 250, 50);
		status.setForeground(Color.decode("#373737"));
		status.setFont(new Font("Segoe Script", Font.BOLD, 50));
		frame.add(status);
		//---------------------------------------------------------
		
		//------------------OPTION TEXT----------------------------
		option = new JLabel();
		option.setBounds(180, 300, 150, 30);
		option.setForeground(Color.decode("#373737"));
		option.setFont(new Font("Segoe Script", Font.BOLD, 20));
		frame.add(option);
		//----------------------------------------------------------
		
		//------------------RANDOM TEXT--------------------------------
		rndm = new JLabel();
		rndm.setBounds(180, 100, 150, 30);
		rndm.setForeground(Color.decode("#373737"));
		rndm.setFont(new Font("Segoe Script", Font.BOLD, 20));
		frame.add(rndm);
		//--------------------------------------------------------------
		
		//-----------------ROCK BUTTON----------------------------------
		rock = new JButton("ROCK");
		rock.setBounds(50, 375, 100, 50);
		rock.setFont(small);
		rock.setForeground(Color.decode("#e5dfe8"));
		rock.setBackground(Color.decode("#373737"));
		frame.add(rock);
		rock.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				option.setText("ROCK");
				check(operation(0));
			}
		});
		//-------------------------------------------------------
		
		//---------------PAPER BUTTON--------------------------------
		paper = new JButton("PAPER");
		paper.setBounds(175, 375, 100, 50);
		paper.setFont(small);
		paper.setForeground(Color.decode("#e5dfe8"));
		paper.setBackground(Color.decode("#373737"));
		frame.add(paper);
		paper.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				option.setText("PAPER");
				check(operation(1));
			}
		});
		//------------------------------------------------------------
		
		//--------------------SCISSORS BUTTON-------------------------
		scissor = new JButton("SCISSORS");
		scissor.setBounds(300, 375, 100, 50);
		scissor.setFont(small);
		scissor.setForeground(Color.decode("#e5dfe8"));
		scissor.setBackground(Color.decode("#373737"));
		frame.add(scissor);
		scissor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				option.setText("SCISSORS");
				check(operation(2));
			}
		});
		//-------------------------------------------------------------
		
		//-----------PLAY AGAIN-----------------------------------------
		again = new JButton("PLAY AGAIN");
		again.setBounds(50, 375, 350, 50);
		again.setFont(small);
		again.setForeground(Color.decode("#e5dfe8"));
		again.setBackground(Color.decode("#373737"));
		again.setVisible(false);
		frame.add(again);
		again.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
				Game game = new Game();
				game.gameWindow();
			}
		});
		//--------------------------------------------------------------
		
		//------------------------FRAME--------------------------------
		frame.setSize(450,450);
		frame.setResizable(false);
		frame.setLayout(null);
		frame.setUndecorated(true);
		frame.setLocationRelativeTo(null);  
		frame.setVisible(true);
		frame.setFocusable(true);
		frame.getContentPane().setBackground(Color.decode("#e5dfe8"));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//-----------------------------------------------------------------
	}
	
	//---------GENERATING OPPONENT AND VALUATING RESULT-------------------
	private int operation(int op) {
		Random random = new Random();
		int k = random.nextInt(3);
		if(k==0)
			rndm.setText("ROCK");
		else if(k==1)
			rndm.setText("PAPER");
		else
			rndm.setText("SCISSORS");
		if(op==k)
			return 0;
		else if(k==0 && op==2)
			return -1;
		else if(k==2 && op==0)
			return 1;
		else if(k<op)
			return 1;
		else
			return -1;
	}
	//-------------------------------------------------------------------------------
	
	int won = 0, lost = 0;
	//--------UPDATING SCORES AND CHECKING STATUS---------------------------
	private void check(int i) {
		if(i==-1)
			lost++;
		else if(i==1)
			won++;
		win.setText(String.valueOf(won));
		loss.setText(String.valueOf(lost));
		if(won == 5) {
			rndm.setVisible(false);
			option.setVisible(false);
			win.setBounds(165, 80, 150, 50);
			col.setBounds(210, 80, 150, 50);
			loss.setBounds(235, 80, 150, 50);
			status.setText("WON!!!");
			again.setVisible(true);
			rock.setVisible(false);
			paper.setVisible(false);
			scissor.setVisible(false);
			you.setVisible(false);
		}
		if(lost == 5) {
			rndm.setVisible(false);
			option.setVisible(false);
			win.setBounds(165, 80, 150, 50);
			col.setBounds(210, 80, 150, 50);
			loss.setBounds(235, 80, 150, 50);
			status.setText("LOST!!!");
			again.setVisible(true);
			rock.setVisible(false);
			paper.setVisible(false);
			scissor.setVisible(false);
			you.setVisible(false);
		}
	}
	//-----------------------------------------------------------------------
}
Game While Playing
Game While Playing
Game on ending
Game on ending

The game shows the user option below and the computer option above. The score will update on every click and when any one of the two reaches 5 points, it will stop the game and shows the options to play again or go back. The score is also exhibited here.

4. Help Menu

Name: “Help.java

It displays the instruction page on how to play the game. Nothing but the rules of the game, how it played, etc…

package rps;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Help {
	public void helpWindow() {
		JFrame frame = new JFrame();
		Font text = new Font("Copperplate Gothic Bold", Font.PLAIN, 12);
		
		//--------------------BACK BUTTON-------------------------
		JLabel back = new JLabel("< BACK");
		back.setBounds(20, 20, 150, 30);
		back.setForeground(Color.decode("#373737"));
		back.setFont(new Font("Segoe Script", Font.BOLD, 20));
		frame.add(back);
		back.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		//----------------------------------------------------------
		
		//---------------TEXT---------------------------------------
		JLabel txt = new JLabel();
		txt.setFont(text);
		txt.setBounds(10, 60, 430, 370);
		String str = "<html>RULES :<br/> 1. Paper defeats rock.<br/>2. Rock defeats scissors.<br/>3. Scissors defeats paper.<br/><br/>The first to score 5 will win the game."
				+ "The status will be show in the screen. You can use the play again button to play again or back to return back to main menu.</html>";
		txt.setText(str);
		txt.setForeground(Color.decode("#373737"));
		frame.add(txt);
		//----------------------------------------------------------
		
		//-----------------FRAME-----------------------------------
		frame.setSize(450,450);
		frame.setResizable(false);
		frame.setLayout(null);
		frame.setUndecorated(true);
		frame.setLocationRelativeTo(null);  
		frame.setVisible(true);
		frame.setFocusable(true);
		frame.getContentPane().setBackground(Color.decode("#e5dfe8"));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//--------------------------------------------------------------
		
		
	}
}
help/instruction page
The help page

How does it work?

Let’s have some discussion on how the whole thing works. The random class in java.util package is utilized here. We generate a random number between 0 and 2. Each number is assigned to each symbol. The winner is selected on the if conditions in the “operation” function in the “Game” class.

Output:


Also Read:

Share:

Author: Ayush Purawr