Guest User

Untitled

a guest
Jul 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class GuessTheNumber {
  4.  
  5. private final Integer value;
  6. private final int min;
  7. private final int max;
  8. private int guesses = 0;
  9. private boolean solved = false;
  10.  
  11. public GuessTheNumber(int min, int max, int value) {
  12. this.min = min;
  13. this.max = max;
  14. this.value = value;
  15. }
  16.  
  17. public GuessTheNumber(int min, int max) {
  18. this(min, max, getRandomNumber(min, max));
  19.  
  20. }
  21.  
  22. public int guess(int i) {
  23. if (solved)
  24. throw new IllegalStateException();
  25.  
  26. guesses++;
  27.  
  28. int ret = value.compareTo(i);
  29.  
  30. if (ret == 0)
  31. solved = true;
  32.  
  33. return ret;
  34. }
  35.  
  36. public void resetCount() {
  37. guesses = 0;
  38. solved = false;
  39. }
  40.  
  41. public int getValue() {
  42. if (solved == false)
  43. throw new IllegalStateException();
  44.  
  45. return value;
  46. }
  47.  
  48. public int getMin() { return min; }
  49. public int getMax() { return max; }
  50.  
  51. private static int getRandomNumber(int min, int max) {
  52. return new Random().nextInt(max - min + 1) + min;
  53. }
  54. }
Add Comment
Please, Sign In to add comment