Created
December 31, 2010 00:31
-
-
Save ericanderson/760542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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