// Animals.java: Animal guissing game // // AUTHOR // Michael J. Gourlay // Programming Langauges, CS 3155, Fall 1998 // // DESCRIPTION // Think of an animal. This program will then ask a series of // yes-or-no questions to try to figure out the animal. // // NOTES // Use this line in an HTML file for applet use: // import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; public class Animals extends Applet { Label welcomeL = new Label("Welcome to the animal-guessing applet."); Label pleaseL = new Label( "Please think of an animal and answer the series of questions."); Label questionL = new Label("??"); Button yesB = new Button("Yes"); YesBhClass yesBH = new YesBhClass(); Button noB = new Button("No"); NoBhClass noBH = new NoBhClass(); Hashtable ht = new Hashtable(); final String INITIAL_ANSWERS = "A"; String question = ""; String answers = INITIAL_ANSWERS; // Methods private void addHorizontalLine(Color c) { // Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as // a horizontal line to separate one group of components from the next. Canvas line = new Canvas( ); line.setSize(10000,1); line.setBackground(c); add(line); } private void addNewLine( ) { // Add a horizontal line in the background color. The line itself is // invisible, but it serves to force the next Component onto a new line. addHorizontalLine(getBackground( )); } private void setLabel(String s) { questionL.setText(s); // System.out.println(s); // for debugging // Attempt to redo the GUI layout to accomodate label size change. questionL.getParent().invalidate(); questionL.getParent().validate(); } private int readDataFile () { // Read questions/answers database from URL URL u = null; InputStreamReader isr = null; BufferedReader br = null; try { // u = new URL("http://csel.cs.colorado.edu/~main/Animals.dat"); u = new URL("http://michelangelo.colorado-research.com/~gourlay/software/Java/Animals/Animals.dat"); } catch (MalformedURLException e) { setLabel("URL failed: " + e); return 1; // tell caller we failed } try { br = new BufferedReader (new InputStreamReader(u.openConnection().getInputStream())); } catch (IOException e) { setLabel("BufferedReader failed: " + e); return 2; // tell caller we failed } // Read the answer/question strings and store in Hashtable try { String answer_line; String question_line; while((answer_line = br.readLine()) != null) { // This assumes a question will always follow an answer, // because no EOF check is done with this readLine. question_line = br.readLine(); // setLabel(question_line + ":" + answer_line); // debugging ht.put(answer_line, question_line); // store in hash table } } catch (IOException e) { setLabel("readLine failed:" + e); return 3; // tell caller we failed } return 0; // tell caller we succeeded } public void init () { // init: called when the applet starts // Establish layout of GUI widgets. add(welcomeL); addNewLine(); add(pleaseL); addHorizontalLine(Color.blue); add(questionL); addNewLine(); add(yesB); yesB.addActionListener(yesBH); add(noB); noB.addActionListener(noBH); if(readDataFile() != 0) { // readDataFile failed so bail out now. // Presumably, readDataFile set the label to an error message. // Disable buttons. This serves two purposes: // (1) Inidicates that there is nothing the applet can do. // (2) Prevents applet from trying to access empty HashTable. yesB.setEnabled(false); noB.setEnabled(false); return; } // Set the initial question. // This assumes that "answers" was initialized. setQuestion(); } public void setQuestion() { // Given the current set of answers, set the current question. if(question.startsWith("I think")) { // Previous question was a final question, so restart the system. answers = INITIAL_ANSWERS; question = (String) ht.get(answers); setLabel("Let's play again. " + question); } else { question = (String) ht.get(answers); setLabel(question); } } class YesBhClass implements java.awt.event.ActionListener { public void actionPerformed(ActionEvent ev) { // This gets called when "Yes" button is pressed. answers = answers + "Y" ; setQuestion(); } } class NoBhClass implements java.awt.event.ActionListener { public void actionPerformed(ActionEvent ev) { // This gets called when "No" button is pressed. answers = answers + "N" ; setQuestion(); } } }