scg/ch11/Answer

From FANG

Jump to: navigation, search

01 package scg.ch11;
02 
03 import java.util.Scanner;
04 
05 /**
06  * An Answer page in an {@link AdventureBook}. An Answer has no
07  * children, no following pages. Instead it just has a guess (the text
08  * on the page). The {@link #play()} method can construct a yes/no
09  * question out of the guess. 
10  */
11 public class Answer
12   extends AdventurePage {
13   /**
14    * Construct a new {@link Answer} object by reading fields from the
15    * given file.
16    *
17    @param  scanner  an open {@link Scanner} associated with an input
18    *                  file containing a saved {@link AdventureBook}
19    */
20   public Answer(AdventureBook book, Scanner scanner{
21     this(book, scanner.nextLine(), scanner.nextInt());
22     scanner.skip(".*\n");// skip to the end of the line
23   }
24 
25   /**
26    * Construct an answer with the given text
27    *
28    @param  book       the {@link AdventureBook} to which this page
29    *                    belongs
30    @param  text       the text of the question
31    @param  parentNdx  the index number of the parent page (how we got
32    *                    here)
33    */
34   public Answer(AdventureBook book, String text, int parentNdx{
35     super(book, text, parentNdx);
36   }
37 
38   /**
39    * Play this {@link Answer}: generate a guess question and ask the
40    * player; if the guess is right, return true (the game was won by the
41    * computer); otherwise get information from the player to improved
42    * the quality of the {@link AdventureBook} to which this answer
43    * belongs and return false (the game was lost by the computer).
44    *
45    @return  true if machine guessed right; false otherwise
46    */
47   @Override
48   public boolean play() {
49     String guess = "Is it a(n) " + getText() + "?";
50     if (TwentyQuestions.answersYes(guess)) {
51       System.out.println("I am so smart! I win!!!");
52       return true;
53     } else {
54       System.out.println("Darn, you stumped me.");
55       getParent().userProvidesNewAnswer(this);
56       return false;
57     }
58   }
59 
60   /**
61    * Get a string representation of this answer. A: before the text
62    * content of the string
63    *
64    @return  string representation of this Answer
65    */
66   @Override
67   public String toString() {
68     return "A: " + getText();
69   }
70 }
71 
72 //Uploaded on Mon Mar 29 21:42:01 EDT 2010


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