Java Reference
In-Depth Information
FIGURE 23-17
The decision tree for a guessing game after acquiring another fact
Is it in North America?
No
Ye s
Is it in Europe?
U.S.A.
Ye s
No
Brazil
England
LISTING 23-5
The class GuessingGame
import TreePackage.DecisionTreeInterface;
import TreePackage.DecisionTree;
public class GuessingGame
{
private DecisionTreeInterface<String> tree;
public GuessingGame(String question, String noAnswer, String yesAnswer)
{
DecisionTree<String> no = new DecisionTree<String>(noAnswer);
DecisionTree<String> yes = new DecisionTree<String>(yesAnswer);
tree = new DecisionTree<String>(question, no, yes);
} // end default constructor
public void play()
{
tree.reset();
while (!tree.isAnswer())
{
// ask current question
System.out.println(tree.getCurrentData());
if (Client.isUserResponseYes())
tree.advanceToYes();
else
tree.advanceToNo();
} // end while
assert tree.isAnswer(); // Assertion: leaf is reached
// make guess
System.out.println("My guess is " + tree.getCurrentData() +
". Am I right?");
if (Client.isUserResponseYes())
System.out.println("I win.");
 
Search WWH ::




Custom Search