Skip to content

Instantly share code, notes, and snippets.

@ericanderson
Created December 31, 2010 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericanderson/760542 to your computer and use it in GitHub Desktop.
Save ericanderson/760542 to your computer and use it in GitHub Desktop.
import java.util.Random;
public class GuessTheNumber {
private final Integer value;
private final int min;
private final int max;
private int guesses = 0;
private boolean solved = false;
public GuessTheNumber(int min, int max, int value) {
this.min = min;
this.max = max;
this.value = value;
}
public GuessTheNumber(int min, int max) {
this(min, max, getRandomNumber(min, max));
}
public int guess(int i) {
if (solved)
throw new IllegalStateException();
guesses++;
int ret = value.compareTo(i);
if (ret == 0)
solved = true;
return ret;
}
public void resetCount() {
guesses = 0;
solved = false;
}
public int getValue() {
if (solved == false)
throw new IllegalStateException();
return value;
}
public int getMin() { return min; }
public int getMax() { return max; }
private static int getRandomNumber(int min, int max) {
return new Random().nextInt(max - min + 1) + min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment