worked on parser

This commit is contained in:
jslightham
2019-04-24 21:25:48 -04:00
parent 5023bddaad
commit 9805c8ea84
4 changed files with 107 additions and 18 deletions

View File

@@ -1,5 +1,7 @@
package com.bayviewglen.zork; package com.bayviewglen.zork;
import java.util.ArrayList;
/** /**
* Class Command - Part of the "Zork" game. * Class Command - Part of the "Zork" game.
* *
@@ -22,16 +24,18 @@ package com.bayviewglen.zork;
*/ */
class Command { class Command {
private String commandWord; private String commandWord;
private String secondWord; private String direction;
private ArrayList<String> otherWords;
/** /**
* Create a command object. First and second word must be supplied, but * Create a command object. First and second word must be supplied, but
* either one (or both) can be null. The command word should be null to * either one (or both) can be null. The command word should be null to
* indicate that this was a command that is not recognised by this game. * indicate that this was a command that is not recognised by this game.
*/ */
public Command(String firstWord, String secondWord) { public Command(String firstWord, ArrayList<String> otherWords, String direction) {
commandWord = firstWord; commandWord = firstWord;
this.secondWord = secondWord; this.otherWords = otherWords;
this.direction = direction;
} }
/** /**
@@ -46,9 +50,14 @@ class Command {
* Return the second word of this command. Returns null if there was no * Return the second word of this command. Returns null if there was no
* second word. * second word.
*/ */
public String getSecondWord() { public ArrayList<String> getOtherWords() {
return secondWord; return otherWords;
} }
/*
public String getSecondWord() {
return otherWords.get(0);
}
*/
/** /**
* Return true if this command was not understood. * Return true if this command was not understood.
@@ -61,6 +70,13 @@ class Command {
* Return true if the command has a second word. * Return true if the command has a second word.
*/ */
public boolean hasSecondWord() { public boolean hasSecondWord() {
return (secondWord != null); return otherWords.size() > 0;
}
public boolean hasDirection() {
return CommandWords.isDirection(commandWord);
}
public String getDirection() {
return direction;
} }
} }

View File

@@ -1,5 +1,7 @@
package com.bayviewglen.zork; package com.bayviewglen.zork;
import java.util.HashMap;
/* /*
* Author: Michael Kolling. * Author: Michael Kolling.
* Version: 1.0 * Version: 1.0
@@ -12,35 +14,78 @@ package com.bayviewglen.zork;
*/ */
class CommandWords { class CommandWords {
// a constant array that holds all valid command words // a constant array that holds all valid command words
private static final String validCommands[] = { "go", "quit", "help", "eat", "jump" }; private static HashMap<String, String> m_words = new HashMap<String, String>();;
/** /**
* Constructor - initialise the command words. * Constructor - initialise the command words.
*/ */
public CommandWords() { public CommandWords() {
// nothing to do at the moment... m_words.put("go", "verb");
m_words.put("quit", "verb");
m_words.put("help", "verb");
m_words.put("jump", "verb");
m_words.put("north", "direction");
m_words.put("south", "direction");
m_words.put("west", "direction");
m_words.put("east", "direction");
m_words.put("n", "direction");
m_words.put("w", "direction");
m_words.put("s", "direction");
m_words.put("e", "direction");
m_words.put("up", "direction");
m_words.put("down", "direction");
} }
/** /**
* Check whether a given String is a valid command word. Return true if it * Check whether a given String is a valid command word. Return true if it
* is, false if it isn't. * is, false if it isn't.
**/ **/
public boolean isCommand(String aString) { public static boolean isCommand(String aString) {
try {
return (m_words.get(aString).equals("verb") || m_words.get(aString).equals("direction"));
}catch(Exception e) {
return false;
}
/*
for (int i = 0; i < validCommands.length; i++) { for (int i = 0; i < validCommands.length; i++) {
if (validCommands[i].equals(aString)) if (validCommands[i].equals(aString))
return true; return true;
} }
// if we get here, the string was not found in the commands // if we get here, the string was not found in the commands
return false; return false;
*/
}
public static boolean isDirection(String aString) {
return m_words.get(aString).equals("direction");
} }
/* /*
* Print all valid commands to System.out. * Print all valid commands to System.out.
*/ */
public void showAll() { public void showAll() {
for (String i : m_words.keySet()) {
if(m_words.get(i).equals("verb")){
System.out.print(i + " ");
}
}
System.out.println();
/*
for (int i = 0; i < validCommands.length; i++) { for (int i = 0; i < validCommands.length; i++) {
System.out.print(validCommands[i] + " "); System.out.print(validCommands[i] + " ");
} }
System.out.println(); System.out.println();
*/
} }
/*
public boolean isVerb(String string) {
try {
if(m_words.get(string).equals("verb"))
return true;
} catch(Exception e) {
return false;
}
return false;
}
*/
} }

View File

@@ -133,11 +133,18 @@ class Game {
* ends the game, true is returned, otherwise false is returned. * ends the game, true is returned, otherwise false is returned.
*/ */
private boolean processCommand(Command command) { private boolean processCommand(Command command) {
if (command.isUnknown()) { if (command.isUnknown()) {
System.out.println("I don't know what you mean..."); System.out.println("I don't know what you mean...");
return false; return false;
} }
String commandWord = command.getCommandWord(); String commandWord = command.getCommandWord();
switch(commandWord) {
case "n": case "s": case "e": case "w": case "north": case "south": case "west": case "east": case "up": case "down":
goRoom(command);
break;
}
if (commandWord.equals("help")) if (commandWord.equals("help"))
printHelp(); printHelp();
else if (commandWord.equals("go")) else if (commandWord.equals("go"))
@@ -173,12 +180,12 @@ class Game {
* otherwise print an error message. * otherwise print an error message.
*/ */
private void goRoom(Command command) { private void goRoom(Command command) {
if (!command.hasSecondWord()) { if (!command.hasDirection()) {
// if there is no second word, we don't know where to go... // if there is no second word, we don't know where to go...
System.out.println("Go where?"); System.out.println("Go where?");
return; return;
} }
String direction = command.getSecondWord(); String direction = command.getDirection();
// Try to leave current room. // Try to leave current room.
Room nextRoom = currentRoom.nextRoom(direction); Room nextRoom = currentRoom.nextRoom(direction);
if (nextRoom == null) if (nextRoom == null)

View File

@@ -19,7 +19,9 @@ package com.bayviewglen.zork;
*/ */
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import com.bayviewglen.zork.CommandWords;
class Parser { class Parser {
private CommandWords commands; // holds all valid command words private CommandWords commands; // holds all valid command words
@@ -30,9 +32,12 @@ class Parser {
public Command getCommand() { public Command getCommand() {
String inputLine = ""; // will hold the full input line String inputLine = ""; // will hold the full input line
String word1; String verb = "";
String word2; String direction = "";
System.out.print("> "); // print prompt //String word2;
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> otherWords = new ArrayList<String>();
System.out.print("> "); // print prompt
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try { try {
inputLine = reader.readLine(); inputLine = reader.readLine();
@@ -40,6 +45,20 @@ class Parser {
System.out.println("There was an error during reading: " + exc.getMessage()); System.out.println("There was an error during reading: " + exc.getMessage());
} }
StringTokenizer tokenizer = new StringTokenizer(inputLine); StringTokenizer tokenizer = new StringTokenizer(inputLine);
while(tokenizer.hasMoreTokens()) {
words.add(tokenizer.nextToken());
}
for(int i=0; i<words.size(); i++) {
if(CommandWords.isCommand(words.get(i))) {
verb = words.get(i);
if(CommandWords.isDirection(words.get(i))) {
direction = words.get(i);
}
}else {
otherWords.add(words.get(i));
}
}
/*
if (tokenizer.hasMoreTokens()) if (tokenizer.hasMoreTokens())
word1 = tokenizer.nextToken(); // get first word word1 = tokenizer.nextToken(); // get first word
else else
@@ -51,10 +70,12 @@ class Parser {
// note: we just ignore the rest of the input line. // note: we just ignore the rest of the input line.
// Now check whether this word is known. If so, create a command // Now check whether this word is known. If so, create a command
// with it. If not, create a "nil" command (for unknown command). // with it. If not, create a "nil" command (for unknown command).
if (commands.isCommand(word1)) */
return new Command(word1, word2); //System.out.println(verb);
if (CommandWords.isCommand(verb))
return new Command(verb, otherWords, direction);
else else
return new Command(null, word2); return new Command(null, otherWords, direction);
} }
/** /**