Если клиент «угадывает» часть, с которой вы боретесь, простейшая реализация состоит в том, чтобы просто продолжать брать половину между наименьшим числом, которое вам не нужно, чтобы быть «слишком высоким», и самым большим числом, которое вы знаете, чтобы быть «слишком низким»что-то вроде:
public class NumberGuessingClientExample {
public static void main(String[] args) {
new NumberGuessingClient(new ServerStub()).findNumber();
}
public static class NumberGuessingClient {
private int min = Integer.MIN_VALUE;
private int max = Integer.MAX_VALUE;
private final NumberGuessingServer server;
public NumberGuessingClient(NumberGuessingServer server) {
this.server = server;
}
public int findNumber() {
Result result = null;
int value = nextGuess();
while ((result = server.makeGuess(value)) != Result.CORRECT) {
System.out.println(value + " = " + result);
if (result == Result.TOO_HIGH) {
max = value - 1;
} else if (result == Result.TOO_LOW) {
min = value + 1;
}
value = nextGuess();
}
System.out.println(value);
return value;
}
private int nextGuess() {
return (int) ((((long) max - (long) min) / 2) + min);
}
}
public interface NumberGuessingServer {
Result makeGuess(int value);
}
public static class ServerStub implements NumberGuessingServer {
private static final int RESULT = 3;
public Result makeGuess(int value) {
if (value > RESULT) {
return Result.TOO_HIGH;
}
if (value < RESULT) {
return Result.TOO_LOW;
}
return Result.CORRECT;
}
}
public enum Result {
CORRECT, TOO_HIGH, TOO_LOW
}
}