Number guessing game with computer
Welcome, this tutorial is all about the small console game which is easy to play and code.
TLDR; Basically in this post we will create a java console program which will guess a number and asks us to guess that number in certain steps, ofcourse it will give us hints.
Rules of the game
- Computer will ask for a maximum number up to which generate a random number, then computer will randomly generate a number.
- Computer now will ask the user to input a number between 0 to maximum number, and it will reply with suitable answers.
- Total number of allowed steps are 6.
- Replies are going to be like one of the following.
- Number you have guessed is much higher.
- Number you have guessed is close to number but higher.
- You have guessed correct number hurray you won.
- Number you have guessed is close to number but lower.
- Number you have guessed is much lower.
- Anything other than winning message game will repeat itself. If number of steps exhausted you lost the game.
Suggestion
Suppose this program as the practice program, and try doing this on your own, this is highly recommended, any issues look for help in this page. For understanding the rules you can reread the rules again.
Features or architecture or game
- Computer will ask for a maximum number up to which generate a random number.
- Computer will randomly generate a number and store this number as the guessed number.
- The computer asks the user to give an input between 0 to max number, and reply 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 divided in two files GuessingGame.java and GuessingGameTester.java .
- GuessingGame.java contains main logic for the game, and GuessingGameTester.java contains the main driver function to run the game.
If any doubts check screenshots
Implementation
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 method
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;
}
}
}
Now try to run this program, it’s all working, isn’t it?
If you have any issue ask me in the comment section.
Thanks for being here…
if you have liked this post please share this post among your friends or classmates.
Happy coding Bye