001import java.util.Scanner; 002import java.util.StringTokenizer; 003 004/** 005 * This class is part of the "Pere-noel" application. 006 * "Pere Noel" is a very simple, text based adventure game. 007 * 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 Célia PRIOL & Benoît CHAUVEAU 019 */ 020public class Parser 021{ 022 private CommandWords commands; // holds all valid command words 023 private Scanner reader; // source of command input 024 025 /** 026 * Create a parser to read from the terminal window. 027 */ 028 public Parser() 029 { 030 commands = new CommandWords(); 031 reader = new Scanner(System.in); 032 } 033 034 /** 035 * @return The next command from the user. 036 */ 037 public Command getCommand(String inputLine) 038 { 039 //String inputLine = ""; // will hold the full input line 040 String word1; 041 String word2; 042 043 StringTokenizer tokenizer = new StringTokenizer(inputLine); 044 045 if(tokenizer.hasMoreTokens()) 046 word1 = tokenizer.nextToken(); // get first word 047 else 048 word1 = null; 049 if(tokenizer.hasMoreTokens()) 050 word2 = tokenizer.nextToken(); // get second word 051 else 052 word2 = null; 053 054 055 //if(commands.isCommand(word1)) 056 // return new Command(word1, word2); 057 //else 058 // return new Command(null, word2); 059 return new Command(commands.getCommandWord(word1), word2); 060 061 } 062 063 /** 064 * Print out a list of valid command words. 065 */ 066 public String showCommands() 067 { 068 return commands.showAll(); 069 } 070} 071