Guessing Game in JAVA using OOP concept

We will build a number guessing game to generate a random number.

In the last episode, I discussed Java objects and classes. To understand objects and classes, We will create a funny game name java guessing game in this episode so that we can easily understand objects and classes.

Guessing Game in JAVA

How the game works

This is a simple game where first the program will automatically generate a random number, and all the players who play the game here will continue to guess the number one by one and match it with the previous program. The game will end if it matches the program’s generated number. Whoever matches will be the winner of this game.

Here we will add another interesting feature which is counter. What is a counter? It’s not just a recorder that will keep recording how many times the player is guessing and how many guesses after which a winner is actually found.

After creating the game, we will learn:

  1. What is class?
  2. What is an object? How to make?
  3. How objects access a class method?

Background Study of guessing game in java

We will create three classes to create this simple game.

  1. GameLauncher.java
  2. GuessGame.java
  3. Player.java

GameLauncher.java: The job of this class is to start the game. There will be a main() method here. We will create an object of GuessGame class inside the main method and call the startGame () method. Calling startGame () will start the game.

GuessGame.java: In this class we will create the object of Player class. And in this class we will create the startGame () method. All logic will be implemented inside startGame ().


Player.java: This class will have the guess () method which will generate a random number for each player.

So let’s get started,

Player.java:

package com.guessinggame;

public class Player {
    int number = 0;

    public void guess(){
        double rand = (Math.random()*100);
        number = (int) rand;
        System.out.println("I am guessing "+number);
    }
}

GuessGame.java:

package com.guessinggame;

public class GuessGame {
    Player p1;
    Player p2;
    Player p3;
    int counter;


    public void startGame(){
        counter =0;
        p1 = new Player();
        p2 = new Player();
        p3 = new Player();

        int guessp1 = 0;
        int guessp2 = 0;
        int guessp3 = 0;

        boolean p1IsRight = false;
        boolean p2IsRight = false;
        boolean p3IsRight = false;

        double rand2 = (Math.random()*100);
        int targetNumber = (int) rand2;
        System.out.print("I am thinking "+targetNumber);

        while (true){
            counter++;
            System.out.println("Number to guess is "+targetNumber);
            p1.guess();
            p2.guess();
            p3.guess();

            guessp1 = p1.number;
            System.out.println("P1 guessed "+guessp1);
            guessp2 = p2.number;
            System.out.println("P2 guessed "+guessp2);
            guessp3 = p3.number;
            System.out.println("P3 guessed "+guessp3);

            if (guessp1 == targetNumber){
                p1IsRight = true;
            }
            if (guessp2 == targetNumber){
                p2IsRight = true;
            }
            if (guessp3 == targetNumber){
                p3IsRight = true;
            }

            if (p1IsRight || p2IsRight || p3IsRight){
                System.out.println("Winner Found!!!");
                System.out.println("Palayer 1 got it right ? "+ p1IsRight);
                System.out.println("Palayer 2 got it right ? "+ p2IsRight);
                System.out.println("Palayer 3 got it right ? "+ p3IsRight);
                System.out.println("Game is over!!");
                System.out.println("Total guessed times is :"  +counter);
                break;
            }else {
                System.out.println("No match Found!,Try again...");
            }


        }
    }
}

GameLauncher.java

package com.guessinggame;

public class GameLauncher {

    public static void main(String[] args){
        GuessGame guessGame = new GuessGame();
        guessGame.startGame();
    }
}

wow, great! now you know the objects and classes and how they works and created.