scg/ch11/TwentyQuestions

From FANG

Jump to: navigation, search

001 package scg.ch11;
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   /**
019    * Print the prompt on the standard output until the user answers
020    * either 'y', 'n', 'yes', or 'no'.
021    *
022    @param   prompt  the prompt to show the user
023    *
024    @return  true if user answered 'y' or 'yes'; false otherwise
025    */
026   public static boolean answersYes(String prompt{
027     String userAnswer = "";
028     // sentinel: userAnswer is a valid answer
029     while (!userAnswer.equalsIgnoreCase("y"&&
030         !userAnswer.equalsIgnoreCase("n"&&
031         !userAnswer.equalsIgnoreCase("yes"&&
032         !userAnswer.equalsIgnoreCase("no")) {
033       System.out.print(prompt);
034       System.out.print(" ");
035       userAnswer = keyboard.nextLine();
036     }
037     // if we get here with "yes" or "y" we return true. Just check first
038     // character (ignore case)
039     return userAnswer.substring(01).equalsIgnoreCase("y");
040   }
041 
042   /**
043    * Print the prompt on standard output, read the next line from the
044    * standard input and return that value.
045    *
046    @param   prompt  the prompt/question to show the user
047    *
048    @return  the {@link String} typed in by the user; it could be empty
049    */
050   public static String getLine(String prompt{
051     System.out.print(prompt);
052     System.out.print(" ");
053     return keyboard.nextLine();
054   }
055 
056   /**
057    * The main program. At this level: make sure there is a file name on
058    * the command-line, construct a new {@link TwentyQuestions} object,
059    * load and play the game.
060    *
061    @param  args
062    */
063   public static void main(String[] args{
064     if (args.length == 1{
065       String fname = args[0];
066       TwentyQuestions game = new TwentyQuestions();
067       game.load(fname);
068       game.play();
069       if (TwentyQuestions.answersYes("Save guessing data?")) {
070         game.save(fname);
071       }
072     } else {
073       System.out.println("Usage: java TwentyQuestions <fname>");
074       System.out.println("  where <fname> is the data file name");
075     }
076   }
077 
078   /** root of the DecisionTree representing the smarts */
079   private AdventureBook book;
080 
081   /**
082    * Load root (the {@link AdventureBook}) from the name file.
083    *
084    @param  fname  the name of the file from which to load the data
085    */
086   private void load(String fname{
087     book = AdventureBook.readBookFromFile(fname);
088   }
089 
090   /**
091    * Play the game. Use a PlayProcessor to traverse the decision tree,
092    * playing the game.
093    */
094   private void play() {
095     boolean playing = true;
096     while (playing{
097       book.play();
098       playing = TwentyQuestions.answersYes(
099           "Would you like to play again?");
100     }
101   }
102 
103   /**
104    * Save the {@link AdventureBook} stored in root is saved into the
105    * named file.
106    *
107    @param  fname  the name of the file in which to save the data
108    */
109   private void save(String fname{
110     AdventureBook.writeBookToFile(fname, book);
111   }
112 }
113 
114 //Uploaded on Mon Mar 29 21:38:55 EDT 2010


Download/View scg/ch11/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