/* * zork.java * * Created on November 6, 2001, 9:47 AM */ /** * * @author Robert W. Harrison and the Fall 2001 CSc 2310 class * * @version 0.0 */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Vector; import java.lang.*; public class zork extends java.lang.Object { Vector state_vector; actor me; /** Creates new zork */ public zork() { state_vector = new Vector(10); // set up a new state vector and then add the scenes to it. message("What do you want to call yourself? "); String name = readLine(); name = name.trim(); actor.kinds(); message("\nSo "+name+" what are you? "); String kind ; do{ // believe it or not this is a really legitimate use of do... while kind = readLine(); kind = kind.trim();} while( !actor.valid_kind(kind)); me = new actor(name,kind); initial_scene begin = new initial_scene(state_vector,me); begin.run(); } /** simple_NLP is a simple Natural Language Parser * it returns true if the input_line contains the key_words in the right * order. so if * Open the blinking door is input and * "open", "door" are in the input it returns true. * if you want the green door vs. a red door you would use * "open" "green" "door". * * note that non-english sentences will pass through this routine * as in cat open mouse door has done * it is case insensative because is uses equalsIgnoreCase() */ public static boolean simple_NLP( String input_line, String key_words[]) { int i; int max_token = key_words.length; int to,from; if( input_line.length() < 1) return false; Vector lexed = new Vector(); // stores the words // first extract every substring in input_line that has a blank on either side. from = 0; to = 0; while( input_line.charAt(from) == ' ' && from < input_line.length() ) from ++; // skip ' ' if( from >= input_line.length()) return false; // check for blank and empty lines while( to >=0 ) { to = input_line.indexOf(' ',from); if( to > 0){ lexed.addElement(input_line.substring(from,to)); from = to; while( input_line.charAt(from) == ' ' && from < input_line.length()-1 ) from ++; }else{ lexed.addElement( input_line.substring(from)); } } // // if we get here we have a vector of strings that correspond to the words in the input. // // so now we look for matches in order boolean status =false; to = 0; for( i=0; i< lexed.size(); i++) { String s = (String)lexed.elementAt(i); if( s.equalsIgnoreCase( key_words[to]) ) { to++; if( to >= key_words.length) { status = true; break;} } } return status; } public static void message(String s) { System.out.print(s); System.out.flush();} private static BufferedReader line_buffer = new BufferedReader(new InputStreamReader(System.in)); public static String readLine() { try{ return line_buffer.readLine();} catch( IOException e) {message("zork.readLine() error on reading"); return null;} } public static void main (String Ignored[]) { zork the_game = new zork(); // this instance is a nifty trick. } }