patternjavaMinor
High/Low Guessing Game
Viewed 0 times
gamehighguessinglow
Problem
This is my first by-self written code. Looking for advice on what not to or to do so I could deal with any bad habits now. It's a guessing game and works perfectly fine. I just wanted to know if more experienced programmers would see any huge mistakes in the coding style.
Basically it generates a number and then asks for the user input. If the two match you win. I tried playing around with arrays, multiple classes and methods and I was curious if there is anything wrong here.
Class: GuessGame
Class: Handler
```
package base;
import java.util.Random;
public class Handler {
public int guessLeft = 5;
public int guessCount = 0;
public int[] guesses = new int[guessLeft + 1];
public boolean gameOver = false;
public Random rand = new Random();
public int goal = rand.nextInt(100);
public Handler(){
}
public String PrintGuesses(){
String str = " ";
for(int i = 1; i goal){
Syste
Basically it generates a number and then asks for the user input. If the two match you win. I tried playing around with arrays, multiple classes and methods and I was curious if there is anything wrong here.
Class: GuessGame
package base;
import java.util.Scanner;
public class GuessGame {
public static Scanner scanKeyboard = new Scanner(System.in);
public static void main(String[] args) {
Handler gameHandler = new Handler();
System.out.println("Start guessing! You have " + gameHandler.guessLeft + " guesses left.");
System.out.println(gameHandler.goal);
while(!gameHandler.gameOver){
if (gameHandler.guessLeft == 0) {
gameHandler.gameOver = true;
gameHandler.gameOver();
} else if(gameHandler.isGuessRight()){
gameHandler.gameWin();
gameHandler.gameOver = true;
} else{
gameHandler.guessCount++;
System.out.println("Give me your " + gameHandler.guessCount + ". guess!");
gameHandler.guesses[gameHandler.guessCount] = scanKeyboard.nextInt();
gameHandler.guessLeft--;
}
}
scanKeyboard.close();
}Class: Handler
```
package base;
import java.util.Random;
public class Handler {
public int guessLeft = 5;
public int guessCount = 0;
public int[] guesses = new int[guessLeft + 1];
public boolean gameOver = false;
public Random rand = new Random();
public int goal = rand.nextInt(100);
public Handler(){
}
public String PrintGuesses(){
String str = " ";
for(int i = 1; i goal){
Syste
Solution
Naming
Methods should be named
The
So it would be better to name them as such, for example
Visibility
A good general rule of thumb:
you shouldn't manipulate the fields of objects directly like this:
All the fields in
This is related to good encapsulation and information hiding:
other classes should not need to know how the game handler works,
they should be able to interact with it using public interface methods.
General coding style
Handler is too generic and doesn't explain what this class is about. GameHandler would be better. Or maybe GameManager even better.Methods should be named
camelCase, so this violates the convention: PrintGuessesThe
gameOver and gameWin methods' main job is to print messages.So it would be better to name them as such, for example
printGameOverMessage, printGameWinMessage.Visibility
A good general rule of thumb:
you shouldn't manipulate the fields of objects directly like this:
gameHandler.gameOver = trueAll the fields in
Handler should be private:- They should not be writable by other classes
- Other classes should not need to see these implementation details
This is related to good encapsulation and information hiding:
other classes should not need to know how the game handler works,
they should be able to interact with it using public interface methods.
General coding style
- The formatting is a bit inconsistent. Use your IDE's function to reformat the code nicely and consistently.
- The empty public constructor of
Handleris unnecessary, you can delete it, the compiler will add it automatically
Context
StackExchange Code Review Q#73533, answer score: 7
Revisions (0)
No revisions yet.