Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Add Comment
Please, Sign In to add comment