Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
How to Build a Number Guessing Game in Java

Ultimate Guide: How to Build a Number Guessing Game in Java! Step-by-Step Tutorial with FREE Source Code!”

Posted on September 1, 2023January 2, 2025 By Rishabh saini No Comments on Ultimate Guide: How to Build a Number Guessing Game in Java! Step-by-Step Tutorial with FREE Source Code!”

How to Build a Number Guessing Game

Step 1: Setting Up Your Development Environment

  1. Download and install the Java Development Kit (JDK) from the official Oracle website.
  2. Choose an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA and install it.
How to Build a Number Guessing Game
How to Build a Number Guessing Game

Table of Contents

  • How to Build a Number Guessing Game
  • Step 1: Setting Up Your Development Environment
  • Step 2: Create a New Java Project
  • Features For
  • Free Complete Code for How to Build a Number Guessing Game
  • Run the Game

Step 2: Create a New Java Project

“Embark on a Journey to Craft a Basic Number Guessing Game in Java – The Perfect Starting Point for Nurturing Logic in App Development!”

Project Name:How to Build a Number Guessing Game
Abstract:It’s a GUI-based project used with the swing library to organize all the elements that work under the Number Guessing Game.
Language Used:Java
IDE:VS Code ,Eclipse
Java version (Recommended):Java SE 17.0 or above
Database:File
Type:Desktop Application
Recommended for:Beginners of Java
Time to build:30-60 Min
How to Build a Number Guessing Game

Features For

  • A secret number between 1 and 5 is created.
  • If the player correctly guesses the number on the first try, the score is increased by 5.
  • If you predict correctly on the second try, you get a +2, else you get a +1.
  • The current score, highest soreness, and number of rounds played are all shown.

Free Complete Code for How to Build a Number Guessing Game

Make a folder for the project and a file called Game.java. You may get the project’s required photos here.

Now, save three files in the same folder with the names current_score.txt, high_score.txt, and num_game.txt. The folder structure should look like this.

Ultimate Guide: How to Build a Number Guessing Game in Java! Step-by-Step Tutorial with FREE Source Code!"
How to Build a Number Guessing Game
How to Build a Number Guessing Game in Java
How to Build a Number Guessing Game

Now paste the below lines of code into the file Game.java to create Number Guessing Game in Java.

import javax.swing.*;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;

class Play extends JPanel {

    // Declaring the Game class for changing the scene
    final Game game;

    RandomNumber randomNumber = new RandomNumber();

    ScoringSystem scoringSystem = new ScoringSystem();

    ScoreFiles scoreFiles = new ScoreFiles();

    public Play(Game game){
        this.game = game;

        // Layout to be used in this panel
        BoxLayout boxlayout = new BoxLayout(this, BoxLayout.Y_AXIS);
        this.setLayout(boxlayout);
        createGUI();
    }

    void createGUI() {

        // Declare the variables needed
        JLabel playScore, gameText, mysteryNumber, statusImage;
        JButton continueButton, enterButton;
        JTextField inputText;
        JPanel gridPanel;
        int random = randomNumber.generateNumber();

        // Setting up and Display the Score in the Current Game
        playScore = new JLabel("Score: " + scoreFiles.intScore("current_score.txt") + "   Games: " + scoreFiles.intScore("num_game.txt"));
        playScore.setFont(new Font("MV Boli", Font.BOLD, 24));
        playScore.setForeground(new Color(253,233,180));
        playScore.setBorder(new EmptyBorder(20,0,0,0));
        playScore.setAlignmentX(CENTER_ALIGNMENT);
        add(playScore);

        gameText = new JLabel("Guess the Number");
        gameText.setForeground(new Color(253,233,180));
        gameText.setFont(new Font("Ink Free", Font.BOLD, 30));
        gameText.setBorder(new EmptyBorder(30,0,0,0));
        gameText.setAlignmentX(CENTER_ALIGNMENT);
        add(gameText);

        JLabel gameText2 = new JLabel("between 1 and 5");
        gameText2.setForeground(new Color(253,233,180));
        gameText2.setFont(new Font("Ink Free", Font.BOLD, 30));
        gameText2.setBorder(new EmptyBorder(5,0,0,0));
        gameText2.setAlignmentX(CENTER_ALIGNMENT);
        add(gameText2);

        mysteryNumber = new JLabel("?");
        mysteryNumber.setIcon(new ImageIcon("mystery_square.png"));
        mysteryNumber.setHorizontalTextPosition(JLabel.CENTER);
        mysteryNumber.setFont(new Font("MV Boli", Font.BOLD, 100));
        mysteryNumber.setForeground(new Color(0X62355F));
        mysteryNumber.setAlignmentX(CENTER_ALIGNMENT);
        add(mysteryNumber);

        statusImage = new JLabel("Input a number");
        statusImage.setForeground(new Color(253,233,180));
        statusImage.setFont(new Font("MV Boli", Font.BOLD, 30));
        statusImage.setAlignmentX(CENTER_ALIGNMENT);
        statusImage.setBorder(BorderFactory.createEmptyBorder(-10, 0, 15, 0));
        add(statusImage);

        gridPanel = new JPanel();
        gridPanel.setMaximumSize(new Dimension(260, 50));
        gridPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
        gridPanel.setLayout(new GridLayout(0, 2));
        gridPanel.setOpaque(false);

        inputText = new JTextField();
        inputText.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        inputText.setBackground(new Color(253,233,180));
        inputText.setForeground(new Color(0X62355F));
        inputText.setHorizontalAlignment(JTextField.CENTER);
        inputText.setFont(new Font("MV Boli", Font.BOLD, 28));
        gridPanel.add(inputText);

        enterButton = new JButton("Enter");
        enterButton.setForeground(new Color(0X62355F));
        enterButton.setFont(new Font("MV Boli", Font.BOLD, 25));
        enterButton.setBackground(new Color(253,233,170));
        enterButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        enterButton.setHorizontalAlignment(JTextField.CENTER);
        enterButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        gridPanel.add(enterButton);
        add(gridPanel);

        continueButton = new JButton("Continue Playing?");
        continueButton.setForeground(new Color(0X62355F));
        continueButton.setBackground(new Color(253,233,180));
        continueButton.setFont(new Font("MV Boli", Font.BOLD, 20));
        continueButton.setPreferredSize(new Dimension(250, 50));
        continueButton.setBorder(BorderFactory.createLineBorder(new Color(125, 95, 123), 3));
        continueButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        continueButton.setAlignmentX(CENTER_ALIGNMENT);
        continueGame(continueButton);
        add(continueButton);

        
        JButton backButton1 = new JButton("Back to Menu");
        backButton1.setFont(new Font("MV Boli", Font.BOLD, 20));
        backButton1.setForeground(new Color(0X62355F));
        backButton1.setBackground(new Color(253,233,180));
        backButton1.setPreferredSize(new Dimension(200, 50));
        backButton1.setBorder(BorderFactory.createLineBorder(new Color(125, 95, 123), 3));
        backButton1.setCursor(new Cursor(Cursor.HAND_CURSOR));
        backButton1.setAlignmentX(CENTER_ALIGNMENT);
        linkMenu(backButton1);
        add(backButton1);

        // When user click the enter button
        enterButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                changeStatus(inputText, mysteryNumber, random, statusImage, gridPanel, continueButton, backButton1);
            }
        });

        // When user hits the button key while inputting in text field
        inputText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeStatus(inputText, mysteryNumber, random, statusImage, gridPanel, continueButton, backButton1);
            }
        });
    }

    public void linkMenu(JButton jLabel){
        jLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int dialogResult = JOptionPane.showConfirmDialog(null, "Want to Stop the Game?", "Are you Sure", JOptionPane.YES_NO_OPTION);
                if(dialogResult == 0) {
                    // Insert the current score and number of games played to High Score when quiting the game
                    scoreFiles.compareScore("high_score.txt", "current_score.txt", "num_game.txt");
                    game.showView(new Menu(game));
                }
            }
        });
    }

    // Method to continue a game
    public void continueGame(JButton jLabel){
        jLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                game.showView(new Play(game));
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(new ImageIcon("background.png").getImage(), 0, 0, null);
    }

    void changeStatus(JTextField input, JLabel mysterynum, int randnum, JLabel status, JPanel gridPanel, JButton contButton, JButton backButton){
        if (String.valueOf(randnum).equals(input.getText())) {
            status.setText("Correct!");
            status.setFont(new Font("MV Boli", Font.BOLD, 30));
            status.setForeground(new Color(253,233,180));
            status.setPreferredSize(new Dimension(200, 50));

            mysterynum.setText(input.getText());  // Change the value of ? to the number
            
            gridPanel.setVisible(false); //Hide the Panel with the enter button and text field
            
            contButton.setVisible(true); // Set and Show the continue button (for playing again)
            scoringSystem.scoreAttempt();
            scoreFiles.write("current_score.txt", scoreFiles.intScore("current_score.txt") + scoringSystem.getScore()); // Set how many games played continuously
            scoreFiles.write("num_game.txt", scoreFiles.intScore("num_game.txt") + 1);
            
        } else {
            try {
                // Convert the user input number (string) to int
                int textToInt = Integer.parseInt(input.getText());
                // Comparing the user input to the random number
                if(textToInt > 5 || textToInt < 1) {
                    // If the user input higher than 50 and lower than 1, executed this block of code

                    status.setText("Out of Range");
                    status.setFont(new Font("MV Boli", Font.BOLD, 30));
                    status.setForeground(new Color(253,233,180));
                    status.setPreferredSize(new Dimension(200, 50));

                } else if (textToInt > randnum ){
                    // If the user input higher than random number, executed this block of code
                    status.setText("Too High!! Try Again");
                    status.setFont(new Font("MV Boli", Font.BOLD, 30));
                    status.setForeground(new Color(253,233,180));
                    status.setPreferredSize(new Dimension(200, 50));
                } else if(textToInt < randnum){
                    // If the user input lower than random number, executed this block of code
                    status.setText("Too Low!! Try Again");
                    status.setFont(new Font("MV Boli", Font.BOLD, 30));
                    status.setForeground(new Color(253,233,180));
                    status.setPreferredSize(new Dimension(200, 50));
                }
            } catch (NumberFormatException ex) {
                status.setText("Enter a Number");
                status.setFont(new Font("MV Boli", Font.BOLD, 30));
                status.setForeground(new Color(253,233,180));
                status.setPreferredSize(new Dimension(200, 50));
            }
        }
        input.setText("");
        scoringSystem.incrementAttempt();
    }

}

class ScoringSystem {
    int score, attempts;

    public ScoringSystem() {
        this.attempts = 1;
    }

    public int getScore() {
        return score;
    }

    public int getAttempts() {
        return attempts;
    }

    public void scoreAttempt(){
        if(this.attempts == 1)
            this.score += 5;
        else if(this.attempts == 2)
            this.score += 2;
        else
            this.score++;
    }

    public void incrementAttempt(){
        this.attempts++;
    }
}

class ScoreFiles {

    public String showScore(String filename){
        String score = "0";
        try{
            File text = new File(filename);
            Scanner scan = new Scanner(text);
            score = scan.nextLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return score;
    }
    public String showGames(String filename){
        String attempt = "0";
        try{
            File text = new File(filename);
            Scanner scan = new Scanner(text);
            scan.nextLine();
            attempt = scan.nextLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return attempt;
    }

    // Convert the showScore method to int
    public int intScore(String filename){
        return Integer.parseInt(showScore(filename));
    }

    // Convert the showsGames method to int
    public int intGames(String filename){
        return Integer.parseInt(showGames(filename));
    }

    // Used in current_score.txt and num_game.txt
    public void write(String filename, int score){
        try{
            FileWriter score_writer = new FileWriter(filename);
            score_writer.write(String.valueOf(score));
            score_writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Overwrites the text file
    // Used in high_score.txt
    public void writeScoreAttempts(String filename, int score, int attemtps){
        try{
            FileWriter score_writer = new FileWriter(filename);
            score_writer.write(String.valueOf(score));
            score_writer.write("\n");
            score_writer.write(String.valueOf(attemtps));
            score_writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Compare the score if it is a high score
    public void compareScore(String high_score, String current_score, String current_played_games){
        if (intScore(high_score) < intScore(current_score)){
            writeScoreAttempts(high_score, intScore(current_score), intScore(current_played_games));
        }
        else if(intScore(high_score) == intScore(current_score)){
            if (intGames(high_score) > intScore(current_played_games)){
                writeScoreAttempts(high_score, intScore(current_score), intScore(current_played_games));
            }
        }
    }
}

class RandomNumber {
    final int min = 1, max = 5;

    // Generate a Random Number between 1 and 5
    public int generateNumber(){
        return (int)Math.floor(Math.random()*(max-min+1)+min);
    }
}

class Menu extends JPanel {
    final Game game;
    ScoreFiles scoreFiles = new ScoreFiles();

    public Menu(Game game){
        this.game = game;

        BoxLayout boxlayout = new BoxLayout(this, BoxLayout.Y_AXIS);
        this.setLayout(boxlayout);

        createGUI();
    }

    public void createGUI(){

        JLabel logoImage, scoreLabel, scoreText, playButton;

        logoImage = new JLabel("Guess the Number", JLabel.CENTER);
        logoImage.setFont(new Font("MV Boli", Font.BOLD, 33));
        logoImage.setForeground(new Color(253,233,180));
        logoImage.setBorder(new EmptyBorder(100, 0, 0, 0));
        logoImage.setAlignmentX(CENTER_ALIGNMENT);
        add(logoImage);

        scoreLabel = new JLabel("High Score", JLabel.CENTER);
        scoreLabel.setFont(new Font("Ink Free", Font.BOLD, 45));
        scoreLabel.setForeground(new Color(253,233,180));
        scoreLabel.setBorder(new EmptyBorder(85, 0, 0, 0));
        scoreLabel.setAlignmentX(CENTER_ALIGNMENT);
        add(scoreLabel);

        scoreText = new JLabel(scoreFiles.showScore("high_score.txt") + " points for " + scoreFiles.showGames("high_score.txt") + " games");
        scoreText.setFont(new Font("MV Boli", Font.BOLD, 18));
        scoreText.setForeground(new Color(253,233,180));
        scoreText.setBorder(new EmptyBorder(10,0,0,0));
        scoreText.setAlignmentX(CENTER_ALIGNMENT);
        add(scoreText);

        playButton = new JLabel(new ImageIcon("start.png"));
        playButton.setBorder(new EmptyBorder(60, 0, 0, 0));
        playButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        playButton.setAlignmentX(CENTER_ALIGNMENT);
        linkPlay(playButton);
        add(playButton);

    }

    public void linkPlay(JLabel jLabel){
        jLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // Set the data inside to zero
                scoreFiles.write("current_score.txt", 0);
                scoreFiles.write("num_game.txt", 0);
                game.showView(new Play(game));
            }
        });
    }

    // For changing the background of JPanel
    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(new ImageIcon("background.png").getImage(), 0, 0, null);
    }
}

class Game extends JFrame {

    JPanel viewPanel;

    public Game() {

        // Initialize the viewPanel the moment Game Class is called
        viewPanel = new JPanel(new BorderLayout());

        // Setting up the Game
        this.setTitle("Guess the Number"); // Title
        this.setPreferredSize(new Dimension(350, 660)); // Dimension
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Close the Application when clicking x
        this.add(viewPanel, BorderLayout.CENTER); // Add the JPanel in this frame
        showView(new Menu(this));
        this.setVisible(true); // To view the window
        this.pack(); // To size the components(button, img) optimally
        this.setResizable(false); // To avoid resizing the window
        this.setLocationRelativeTo(null); // To set the window in the center
    }

    public void showView(JPanel jPanel){
        viewPanel.removeAll();
        viewPanel.add(jPanel, BorderLayout.CENTER);
        viewPanel.revalidate();
        viewPanel.repaint();
    }
}


class Main {
    public static void main(String[] args) {
        new Game();
    }
}
How to Build a Number Guessing Game in Java
How to Build a Number Guessing Game

Run the Game

  1. Run your Java program to start the game.
  2. Enter your guesses and follow the prompts until you guess the correct number.

Congratulations! You’ve successfully created a captivating number guessing game in Java. Users who follow your clickbait title will be delighted to find the step-by-step guide and free source code to build their own game.


“Thank you for investing your time in reading our article, ‘How to Build a Number Guessing Game.’ We hope you’ve gained valuable insights and knowledge on creating this exciting game in Java. Feel empowered to explore further and continue your journey in programming and game development. Happy coding! How to Build a Number Guessing Game :

We appreciate you visiting our website. :https://updategadh.com/

Check out our Data Science section if you’re seeking for a Data Science tutorial in Python.

Other related Post :-

  • https://updategadh.com/python-projects/how-to-build-an-ai-system-step-by-step-guide/
  • https://updategadh.com/ai/8-ways-ai-is-used-in-education/
  • https://updategadh.com/python-projects/the-power-of-data-visualization-with-python/
Post Views: 847
Free Projects Tags:Java, Java Project

Post navigation

Previous Post: “Unleash Your Python Superpowers: The Ultimate Guide to Setting Up VS Code for Python ! “
Next Post: Top 5 Java Projects With Free Source Code

More Related Articles

Web-based Inventory and POS System in PHP Free Source Code - Web-based Inventory and POS System in PHP Free Source Code Web-based Inventory and POS System in PHP Free Source Code Free Projects
Online Book Buying and Selling Portal using SpringBoot Online Book Buying and Selling Portal using SpringBoot, Hibernate, MySQL : Best J2EE Free Projects
What is a Simple-Store in PHP With Source Code - What is a Simple-Store in PHP With Source Code What is a Simple-Store in PHP With Source Code Free Projects

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. News Portal Project in PHP and MySql Free Source Code
  5. Flipkart Clone using PHP And MYSQL Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme