Skip to content

Instantly share code, notes, and snippets.

@SwannHERRERA
Created February 16, 2023 16:26
Show Gist options
  • Save SwannHERRERA/e043121c9cb0f246da9a3520aa9120ec to your computer and use it in GitHub Desktop.
Save SwannHERRERA/e043121c9cb0f246da9a3520aa9120ec to your computer and use it in GitHub Desktop.
5AL2 exercices form clean architecture courses
public class App {
public static void main(String[] args) {
GuessTheNumber guessTheNumber = new GuessTheNumber();
guessTheNumber.main(args);
}
}
public class GameConfig {
public static final int MAX_ATTEMPTS = 10;
public static final int MAX_NUMBER = 100;
public static final int MIN_NUMBER = 1;
public static final boolean DEBUG = false;
}
public interface Logger {
void log(String message);
}
public class ConsoleLogger implements Logger {
@Override
public void log(String message) {
System.out.println(message);
}
}
public interface Rng {
int nextInt(int bound);
}
public class RandomRng implements Rng {
private Random generator = new Random();
@Override
public int nextInt(int bound) {
return generator.nextInt(bound);
}
}
class GuessTheNumber {
private Rng generator = new RandomRng();
// TODO : change Scanner to be a interface
private Scanner scanner = new Scanner(System.in);
private Logger logger = new ConsoleLogger();
public void main(String[] args) {
int guess = 0;
int attempts = 0;
boolean isTheGameStillInProgress = true;
displayInitalMessage();
while (isTheGameStillInProgress) {
attempts = 0;
logger.log("Guess the number (between " + GameConfig.MIN_NUMBER + " and " + GameConfig.MAX_NUMBER + ")!");
int misteryNumber = getMisteryNumber();
if (GameConfig.DEBUG) {
logger.log("debug : the expected number is " + misteryNumber);
}
while (guessIsWrong(misteryNumber, guess) && playerHasntReachMaxAttempts(attempts)) {
String input = scanner.nextLine();
attempts++;
guess = displayGuessResult(attempts, misteryNumber, input);
}
logger.log("----------------------------------------------------");
isTheGameStillInProgress = doesPlayerContinueTheGame();
}
logger.log("== Thanks for playing! ==");
scanner.close();
}
// The function does 2 thing next move is to change that. Scanner dependency
// should be link and this class will be renamed
private int displayGuessResult(int attempts, int number, String input) {
int guess = 0;
try {
guess = Integer.parseInt(input);
displayOnValidData(guess, attempts, number);
} catch (NumberFormatException e) {
displayOnInvalidData(attempts, input);
}
if (guessIsWrong(number, guess) && playerHasReachMaxAttempts(attempts)) {
logger.log("You lose after " + GameConfig.MAX_ATTEMPTS + " tries, the expected number was " + number);
}
return guess;
}
private int getMisteryNumber() {
return generator.nextInt(GameConfig.MAX_NUMBER) + GameConfig.MIN_NUMBER;
}
private boolean guessIsCorrect(int number, int userGuess) {
return number == userGuess;
}
private boolean guessIsWrong(int number, int userGuess) {
return number != userGuess;
}
private boolean playerHasntReachMaxAttempts(int attempts) {
return attempts < GameConfig.MAX_ATTEMPTS;
}
private boolean playerHasReachMaxAttempts(int attempts) {
return attempts >= GameConfig.MAX_ATTEMPTS;
}
private boolean doesPlayerContinueTheGame() {
logger.log("Do you want to try again with a new number (yes/no)?");
String userResponse = scanner.nextLine().trim().toLowerCase();
return userResponse.equals("yes");
}
private void displayInitalMessage() {
logger.log("-===========================-");
logger.log("=== GUESS THE NUMBER GAME ===");
logger.log("-===========================-");
}
private void displayOnInvalidData(int attempts, String input) {
logger.log("Your input was '" + input + "', please enter a valid integer. " + (GameConfig.MAX_ATTEMPTS - attempts)
+ "/" + GameConfig.MAX_ATTEMPTS + " tries left");
}
private void displayOnValidData(int userGuess, int attempts, int number) {
if (guessIsCorrect(number, userGuess)) {
logger.log("You found it after " + attempts + " tries!");
return;
}
String divergence = userGuess < number ? "smaller" : "greater";
logger.log("Wrong! Your number is " + divergence + " than the correct one. "
+ (GameConfig.MAX_ATTEMPTS - attempts) + "/" + GameConfig.MAX_ATTEMPTS + " tries left");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment