A Game to Boost Your Java Object Oriented Programming Skills

Written by codesnipeet | Published 2020/09/14
Tech Story Tags: java | java-development | projects | oop | java-console-app | programming | latest-tech-stories | gaming

TLDR A Game to Boost Your Java Object Oriented Programming Skills. The game is a simple black screen console game where first the program will automatically generate a random number. 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. 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.via the TL;DR App

This is a simple black screen console 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 a 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. How to define Class and Object?
  2. How to define the accessors of the instance variable
  3. How to makes methods?
  4. How objects access class methods?

Please read the Introduction part of Object Oriented Programming in java, then go forward....:)

Background Study of guessing game in java

We will create three classes to create this simple game.
  1. GameLauncher.java
  2. GussesGame.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 insidestartGame ().

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 it works and created.

Written by codesnipeet | Competitive programmer and web developer. Owner at codesnipeet.com
Published by HackerNoon on 2020/09/14