001    import java.io.BufferedReader;
002    import java.io.InputStreamReader;
003    import java.util.StringTokenizer;
004    
005    /**
006     * This class is the main class of the "World of Zuul" application. 
007     * "World of Zuul" is a very simple, text based adventure game.  
008     *
009     * This parser reads user input and tries to interpret it as an "Adventure"
010     * command. Every time it is called it reads a line from the terminal and
011     * tries to interpret the line as a two word command. It returns the command
012     * as an object of class Command.
013     *
014     * The parser has a set of known command words. It checks user input against
015     * the known commands, and if the input is not one of the known commands, it
016     * returns a command object that is marked as an unknown command.
017     * 
018     * @author  Michael Kolling and David J. Barnes
019     * @version 2011.07.31
020     */
021    
022    public class Parser 
023    {
024    
025        private CommandWords commands;  // holds all valid command words
026    
027        public Parser() 
028        {
029            commands = new CommandWords();
030        }
031    
032        public Command getCommand() 
033        {
034            String inputLine = "";   // will hold the full input line
035            String word1;
036            String word2;
037    
038            System.out.print("> ");     // print prompt
039    
040            BufferedReader reader = 
041                new BufferedReader(new InputStreamReader(System.in));
042            try {
043                inputLine = reader.readLine();
044            }
045            catch(java.io.IOException exc) {
046                System.out.println ("There was an error during reading: "
047                                    + exc.getMessage());
048            }
049    
050            StringTokenizer tokenizer = new StringTokenizer(inputLine);
051    
052            if(tokenizer.hasMoreTokens())
053                word1 = tokenizer.nextToken();      // get first word
054            else
055                word1 = null;
056            if(tokenizer.hasMoreTokens())
057                word2 = tokenizer.nextToken();      // get second word
058            else
059                word2 = null;
060    
061            // note: we just ignore the rest of the input line.
062    
063            Command command = commands.get(word1);
064            if(command != null) {
065                command.setSecondWord(word2);
066            }
067            return command;
068        }
069    
070        /**
071         * Print out a list of valid command words.
072         */
073        public void showCommands()
074        {
075            commands.showAll();
076        }
077    }