HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Guess number game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamenumberguess

Problem

I'm making a simple guess a number game. In game, user should input minimum and maximum value and program should generate random number between this numbers, later user should try and guess the number.

This is one of my first programs, so I'd like to check my coding techniques, what should I do differently etc.

I have getInterval() methode that I'm not sure I've done in best way, first I thought I should past two arguments (min and max) and change them in methode, but looks like I can do that like that in Java, so I've done it this way.

I'm going to make this game a bit more advanced later, but just want to check if this part is ok and if I should change some things before I move on.

```
import java.util.Random;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
int min=0;
int max=0;
min = getInterval("Min"); // Get minimum for guessing interval
max = getInterval("Max"); // Get Maximum for guessing interval
int randomNumber = getRandom(min, max); // generate random number between Min and Max
playRandom(min, max, randomNumber); // play the game
}

public static int getInterval(String text){
System.out.println("Please Input " + text + " number for guessing: ");
Scanner inputMin = new Scanner(System.in);
return Integer.parseInt(inputMin.nextLine());
}

public static int getRandom(int min, int max){
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}

public static void playRandom(int min, int max, int randomNumber){
boolean guess = false;
int numGuesses = 1;
while (!guess){
System.out.println("Enter your guess: ");
Scanner inputGuess = new Scanner(System.in);
int userGuess = Integer.parseInt(inputGuess.nextLine());

if (userGuess randomNumber){
System.out.println("Too high!");

Solution

Use a class to manage state instead of passing it around.

Right now your program has 3 variables that represent the programs state. min, max and randomNumber.

The state is being passed around to wherever it's needed as function arguments:

public static int getRandom(int min, int max) {...}
getRandom(min, max);

public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);


You could simplify these method calls and method signatures by using a class that holds this state as fields, and making these methods part of the class.

public class Game {
    private int min;
    private int max;
    private int randomNumber;

    ...
    public void getInterval() {...} // parameter no longer needed
    public void getRandom() {...}
    public void playRandom() {...}
}


Instead of returning the min and max from getInterval, you would assign the result to these fields:

public void getInterval() {
    Scanner inputMin = new Scanner(System.in);
    System.out.println("Please Input Min number for guessing: ");        

    // return Integer.parseInt(inputMin.nextLine());
    this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.

    System.out.println("Please Input Max number for guessing: ");
    this.max = Integer.parseInt(input.nextLine());
}


Then in getRandom, you would use the fields instead of method parameters:

// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.


The result would be simpler method calls and signatures, since there is no more need for the arguments:

Game game = new Game();

public void getRandom() {...}
game.getRandom();

public void playRandom() {...}
game.playRandom();


The state that was previously passed around is now managed internally by the class.

Code Snippets

public static int getRandom(int min, int max) {...}
getRandom(min, max);

public static void playRandom(int min, int max, int randomNumber) {...}
playRandom(min, max, randomNumber);
public class Game {
    private int min;
    private int max;
    private int randomNumber;

    ...
    public void getInterval() {...} // parameter no longer needed
    public void getRandom() {...}
    public void playRandom() {...}
}
public void getInterval() {
    Scanner inputMin = new Scanner(System.in);
    System.out.println("Please Input Min number for guessing: ");        

    // return Integer.parseInt(inputMin.nextLine());
    this.min = Integer.parseInt(input.nextLine()); // the "this." is optional here.

    System.out.println("Please Input Max number for guessing: ");
    this.max = Integer.parseInt(input.nextLine());
}
// return rand.nextInt((max - min) + 1) + min;
// this.randomNumber = rand.nextInt((this.max - this.min) + 1) + this.min;
randomNumber = rand.nextInt((max - min) + 1) + min; // leaving out "this." here.
Game game = new Game();

public void getRandom() {...}
game.getRandom();

public void playRandom() {...}
game.playRandom();

Context

StackExchange Code Review Q#131695, answer score: 12

Revisions (0)

No revisions yet.