Интересно мнение экспертов. Программа угадывает задуманное число, и пишет за скольо ходов она угадала.
Класс Main
public class Main {
public static void main(String[] args) {
GuessingConsoleGame game = new GuessingConsoleGame();
game.Start();
}
}
Класс GuessingConsoleGame
import java.util.Scanner;
public class GuessingConsoleGame {
public void Start() {
Scanner scanner = new Scanner(System.in);
GuessingBinary guessing = new GuessingBinary();
System.out.println("Guessing game number");
System.out.println("If we guessed your num - press [y]" +
"\nIf your number bigger than proposed by us - press [>]" +
"\nIf your number less than proposed us - press [<]");
System.out.print("Enter min border: ");
int min = scanner.nextInt();
System.out.print("Enter max border: ");
int max = scanner.nextInt();
while (max < min) {
System.out.println("Max can't be less than min. Try again");
System.out.print("Enter max border: ");
max = scanner.nextInt();
}
guessing.initialization(min, max);
while (!guessing.isTrue()) {
System.out.println("Attempt: " + guessing.getCount());
System.out.println("Your num is: " + guessing.getGuessedNum());
System.out.print(":");
guessing.guessing(scanner.next());
}
}
}
Класс GuessingBinary
public class GuessingBinary {
private int min, max;
private int count;
private int guessedNum;
private boolean isTrue;
private int half;
public void initialization(int min, int max) {
this.min = min;
this.max = max;
isTrue = false;
count = 1;
half = max / 2;
guessedNum = half;
}
public int getGuessedNum() {
return guessedNum;
}
public int getCount() {
return count;
}
public boolean isTrue() {
return isTrue;
}
public void guessing(String string) {
if (string.equals("y")) {
isTrue = true;
} else if (string.equals("<")) {
max = half;
} else if (string.equals(">")) {
min = half;
}
half = ((max - min) / 2) + min;
count++;
guessedNum = half;
}
}