1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.Console;
import java.util.Random;

public class Guesser {

        /* The highest number that can be used */
        public static final int MAX_NUMBER = 100;

        /* Our Console object we will use to read input with */
        private final Console console = System.console();

        /* Our "random" number generator */
        private final Random random = new Random();

        /* The last answer that was given */
        private Answer lastAnswer = null;

        // User given input for if our guess was too high or low
        enum Answer {
                CORRECT, HIGHER, LOWER
        }

        public Guesser() {
                // Intro
                printf("This is a game where I (the computer) will try and guess a number you choose.");
                printf("Please choose a number between 1 and %d", MAX_NUMBER);
                printf("Once you have chosen a number, press ENTER.");

                // Wait for input
                console.readLine();

                // Count the amount of tries it took us to guess
                int tries = 0;

                // The correct number (set when we correctly guess)
                int correctNumber = -1;

                // The number we guessed
                int guess = -1;

                // The bottom guessing range
                int bottomRange = 1;

                // The upper guessing range
                int upperRange = MAX_NUMBER;

                // Now we can begin :-)
                do {
                        // Take a guess :-)
                        guess = rand(bottomRange, upperRange);

                        // Keep reading an answer until it's non null
                        do {
                                System.out.printf("Is your number %d? [Yes (y), Higher (h), Lower (l)] ", guess);
                                lastAnswer = readAnswer();
                        } while (lastAnswer == null);

                        // Were we correct ??
                        if (lastAnswer == Answer.CORRECT) {
                                correctNumber = guess;
                                break;
                        }

                        // Calculate the next range
                        if (lastAnswer == Answer.HIGHER) {
                                bottomRange = guess + 1;
                        } else { // LOWER
                                upperRange = guess - 1;
                        }

                        // Failure is not an option
                        tries ++;
                } while ( lastAnswer != Answer.CORRECT );

                // We won....
                printf("Yippee! It took me %d tries to guess your number, which was %d.", tries, correctNumber);
        }

        /* Read an aswer from the user */
        private Answer readAnswer() {
                String line = console.readLine();

                // Disregard my dumb parsing... :D
                for (Answer answer : Answer.values()) {
                        if (line.startsWith("y")) {
                                return Answer.CORRECT;
                        }

                        if (line.toLowerCase().startsWith(answer.toString().substring(0, 1).toLowerCase())) {
                                return answer;
                        }
                }

                return null;
        }

        /* Generate a random number between two values */
        private int rand(int min, int max) {
                if ( min > max ) {
                        return rand(max, min);
                }

                return min + (int) (Math.random() * ((max - min) + 1));
        }

        /* Convenience method for System.out.println(String.format.... */
        private void printf(String str, Object... args) {
                System.out.println(String.format(str, args));
        }

        public static void main(String[] args) {
                new Guesser();
        }

}