Java Console Games Tutorial (number guessing by human)

Java Console Gaming

Welcome to the tutorial, this tutorial is all about the small console game which is small to play and write.

suppose this program as the practice program, and try doing this on your own, this is highly recommended.
if you are stuck somewhere or having any trouble you are free to look here on the page.

Features and Rule of the game:

  • Computer asks for a number in variable max number.
  • Computer randomly generates a number and put this number as the guessed number.
  • The computer asks the user to give a number between 0 to max number and reply back with the suitable answer like whether the entered number is lower or higher than the guessed number.
  • you should also assign a variable for the max guess allowed which are 6 per game.
  • When the user wins or lost the game a check for play again will be asked.
  • The game is broken in two files GuessingGame.java and GuessingGameTester.java.
  • GuessingGame.java holds mostly main logic for the game. And GuessingGameTester.java contains the main driver function to run the game.
All the doubts are cleared after the program screenshots...
game

Try to create a program like this, at least try once otherwise I am here.

To create this game
first, create a java class named GuessingGame.java

and paste the following code.


import java.util.Random;


public class GuessingGame {
private static final int MAXGUESSESALLOWED = 6;
private int answer;
Random generator;
private boolean gameOver;
private int differential;
private int max;
private int numGuessesTaken;

// accessor and Mutator
public int getAnswer(){
return this.answer;
}
public void setAnswer(int num){
this.answer = num;
}
public int getMax(){
return this.max;
}
public void setMax(int num){
this.max = num;
}
public int getNumGuessesTaken(){
return this.numGuessesTaken;
}
public void setNumGuessesTaken(int num){
this.numGuessesTaken = num;
}
public int getDifferential(){
return this.differential;
}
public void setDifferential(int num){
this.differential = num;
}
public int MAXGUESSESALLOWED(){
return MAXGUESSESALLOWED;
}

// Constructor and metgod
public GuessingGame(){
this.max = 0;
generator = new Random();
}
public GuessingGame(int maxValue){
this.max = maxValue;
generator = new Random();
}
public void newGame(){
answer = generator.nextInt(max);
gameOver = false;
differential = max;
numGuessesTaken = 0;
}
public String guess(int guessedValue){
numGuessesTaken ++;
String ans = "",second = "";

if(differential > (answer - guessedValue)){
second = "Getting Warmer";
}else{
second = "Getting Cooler";
}

if(answer > guessedValue){
differential = answer - guessedValue;
ans = "Too Low";
}else if(answer < guessedValue){
differential = answer - guessedValue;
ans = "Too High";
}else if(answer == guessedValue){
ans = "Congratulation";
this.gameOver = true;
return ans;
}
if(guessedValue < 0 && guessedValue > max){
ans = "Guess is out of Range";
}

if(numGuessesTaken >= MAXGUESSESALLOWED){
gameOver = true;
second = "You Lose";
}
return ans + "\n"+second;
}
public boolean isGameOver(){
return gameOver;
}
}

Next, create a new java class GuessingGameTester.java
and paste the following code.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class GuessingGameTester {
public static void main(String[] args) throws IOException {
String choice = "";
Scanner inp = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(!choice.equals("n")){
System.out.println("Welcome To Guessing Game");
System.out.println("Enter the maximum number");
int number = inp.nextInt();
GuessingGame game;
game = new GuessingGame(number);
game.newGame();
while(!game.isGameOver()){
System.out.println("Enter your guess, remember it must be between 0 and "+game.getMax());
int numb = inp.nextInt();
String ans;
ans = game.guess(numb);
System.out.println(ans);

}
System.out.println("Would you like to play again, Enter y for Yes and n for No");
String ch = br.readLine();
choice = ch;
}
}
}

And now try to run this program, it's all working, isn't it?
Any problem ask me in the comment section.

Thanks for being here...
check my next post for the guessing game where computer guesses the number.
if you liked this post please share this post among your friends and classmates.

Comments

Popular posts from this blog

Third Equation of motion, How to implement using C language

Second Equation of motion, How to implement using C language

First Equation of motion, How to implement using C language