scg/ch12/TwentyQuestions

From FANG

Jump to: navigation, search

001 package scg.ch12;
002 
003 import java.util.Scanner;
004 
005 /**
006  * The TwentyQuestions game. Run with a command-line argument of the
007  * data file with the questions and answers. This class provides two
008  public static input methods, {@link #answersYes(String)} which gets a
009  * yes or no answer (returning true or false) and {@link
010  * #getLine(String)} which gets a line from standard input. These two
011  * input methods should be used by other classes to prompt the user for
012  * the input.
013  */
014 public class TwentyQuestions {
015   /** scanner wrapped around standard input */
016   static Scanner keyboard = new Scanner(System.in);
017 
018   /** root of the DecisionTree representing the smarts */
019   private AdventureBook book;
020 
021   /**
022    * Print the prompt on the standard output until the user answers
023    * either 'y', 'n', 'yes', or 'no'.
024    *
025    @param   prompt  the prompt to show the user
026    *
027    @return  true if user answered 'y' or 'yes'; false otherwise
028    */
029   public static boolean answersYes(String prompt{
030     String userAnswer = "";
031     // sentinel: userAnswer is a valid answer
032     while (!userAnswer.equalsIgnoreCase("y"&&
033         !userAnswer.equalsIgnoreCase("n"&&
034         !userAnswer.equalsIgnoreCase("yes"&&
035         !userAnswer.equalsIgnoreCase("no")) {
036       System.out.print(prompt);
037       System.out.print(" ");
038       userAnswer = keyboard.nextLine();
039     }
040     // if we get here with "yes" or "y" we return true. Just check first
041     // character (ignore case)
042     return userAnswer.substring(01).equalsIgnoreCase("y");
043   }
044 
045   /**
046    * Print the prompt on standard output, read the next line from the
047    * standard input and return that value.
048    *
049    @param   prompt  the prompt/question to show the user
050    *
051    @return  the {@link String} typed in by the user; it could be empty
052    */
053   public static String getLine(String prompt{
054     System.out.print(prompt);
055     System.out.print(" ");
056     return keyboard.nextLine();
057   }
058 
059   /**
060    * The main program. At this level: make sure there is a file name on
061    * the command-line, construct a new {@link TwentyQuestions} object,
062    * load and play the game.
063    *
064    @param  args
065    */
066   public static void main(String[] args{
067     String fname = null;
068     if (args.length == 1{
069       fname = args[0];
070     } else {
071       System.out.println("Usage: java TwentyQuestions <fname>");
072       System.out.println("  where <fname> is the data file name");
073       System.out.println();
074       System.out.println(
075         "Alternatively, what file would you like to load?");
076       Scanner scanner = new Scanner(System.in);
077       fname = scanner.nextLine();
078     }
079 
080     TwentyQuestions game = new TwentyQuestions();
081     if (game.load(fname)) {
082       game.play();
083       if (TwentyQuestions.answersYes("Save guessing data?")) {
084         game.save(fname);
085       }
086     }
087   }
088 
089   /**
090    * Load root (the {@link AdventureBook}) from the name file.
091    *
092    @param   fname  the name of the file from which to load the data
093    *
094    @return  true if load went well, false otherwise
095    */
096   private boolean load(String fname{
097     book = AdventureBook.readBookFromFile(fname);
098     return (book != null);
099   }
100 
101   /**
102    * Play the game. Use a PlayProcessor to traverse the decision tree,
103    * playing the game.
104    */
105   private void play() {
106     boolean playing = true;
107     while (playing{
108       book.play();
109       playing = TwentyQuestions.answersYes(
110           "Would you like to play again?");
111     }
112   }
113 
114   /**
115    * Save the {@link AdventureBook} stored in root is saved into the
116    * named file.
117    *
118    @param  fname  the name of the file in which to save the data
119    */
120   private void save(String fname{
121     AdventureBook.writeBookToFile(fname, book);
122   }
123 }
124 
125 //Uploaded on Mon Mar 29 21:40:13 EDT 2010


Download/View scg/ch12/TwentyQuestions.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games