document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4.  
  5. /**
  6.  * This class is the main class of the "World of Zuul" application.
  7.  * "World of Zuul" is a very simple, text based adventure game.
  8.  *
  9.  * This parser reads user input and tries to interpret it as an "Adventure" command.
  10.  * Every time it is called it reads a line from the terminal and
  11.  * tries to interpret the line as a two word command. It returns the command
  12.  * as an object of class Command.
  13.  *
  14.  * The parser has a set of known command words. It checks user input against
  15.  * the known commands, and if the input is not one of the known commands, it
  16.  * returns a command object that is marked as an unknown command.
  17.  *
  18.  * @author Naufaliando
  19.  * @version 1.0
  20.  */
  21. class Parser
  22. {
  23.     private CommandWords commands; // holds all valid command words
  24.  
  25.     public Parser()
  26.     {
  27.         commands = new CommandWords();
  28.     }
  29.  
  30.     public Command getCommand()
  31.     {
  32.         String inputLine = ""; // will hold the full input line
  33.         String word1;
  34.         String word2;
  35.            
  36.         System.out.print("> "); // print prompt
  37.        
  38.         BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));
  39.         try {
  40.             inputLine = reader.readLine();
  41.         }
  42.         catch (java.io.IOException exc){
  43.             System.out.println ("There was an error during reading: " + exc.getMessage());
  44.         }
  45.  
  46.         StringTokenizer tokenizer = new StringTokenizer(inputLine);
  47.  
  48.         if(tokenizer.hasMoreTokens())
  49.             word1 = tokenizer.nextToken(); // get first word
  50.         else
  51.             word1=null;
  52.         if(tokenizer.hasMoreTokens())
  53.             word2 = tokenizer.nextToken(); // get second word
  54.         else
  55.             word2=null;
  56.            
  57.         // note: we just ignore the rest of the input line.
  58.            
  59.         // Now check whether this word is known. If so, create a command
  60.         // with it. If not, create a "null" command (for unknown command).
  61.         if(commands.isCommand(word1))
  62.             return new Command(word1, word2);
  63.         else
  64.             return new Command(null, word2);
  65.     }
  66. }
');