public class GuessingGame
1: import TreePackage.DecisionTreeInterface;
2: import TreePackage.DecisionTree;
3: /**
4: A program that plays a guessing game using yes or no questions.
5:
6: @author Frank M. Carrano
7: @author Timothy M. Henry
8: @version 4.0
9: */
10: public class GuessingGame
11: {
12: private DecisionTreeInterface<String> tree;
13:
14: public GuessingGame(String question, String noAnswer, String yesAnswer)
15: {
16: DecisionTree<String> no = new DecisionTree<>(noAnswer);
17: DecisionTree<String> yes = new DecisionTree<>(yesAnswer);
18: tree = new DecisionTree<>(question, no, yes);
19: } // end default constructor
20:
21: public void play()
22: {
23: tree.reset(); // Initialize current node to root
24: while (!tree.isAnswer())
25: {
26: // Ask current question
27: System.out.println(tree.getCurrentData());
28: if (Client.isUserResponseYes())
29: tree.advanceToYes();
30: else
31: tree.advanceToNo();
32: } // end while
33: assert tree.isAnswer(); // Assertion: Leaf is reached
34:
35: // Make guess
36: System.out.println("My guess is " + tree.getCurrentData() +
37: ". Am I right?");
38: if (Client.isUserResponseYes())
39: System.out.println("I win.");
40: else
41: learn();
42: } // end play
43:
44: private void learn()
45: {
46: /* < Implementation left as a project in the next chapter. >
47: . . . */
48: } // end learn
49: } // end GuessingGame