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;
import java.util.ArrayList;
/**
* Class Command - Part of the "Zork" game.
*
@@ -22,16 +24,18 @@ package com.bayviewglen.zork;
*/
class Command {
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
* 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.
*/
public Command(String firstWord, String secondWord) {
public Command(String firstWord, ArrayList<String> otherWords, String direction) {
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
* second word.
*/
public String getSecondWord() {
return secondWord;
public ArrayList<String> getOtherWords() {
return otherWords;
}
/*
public String getSecondWord() {
return otherWords.get(0);
}
*/
/**
* Return true if this command was not understood.
@@ -61,6 +70,13 @@ class Command {
* Return true if the command has a second word.
*/
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;
import java.util.HashMap;
/*
* Author: Michael Kolling.
* Version: 1.0
@@ -12,35 +14,78 @@ package com.bayviewglen.zork;
*/
class CommandWords {
// 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.
*/
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
* 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++) {
if (validCommands[i].equals(aString))
return true;
}
// if we get here, the string was not found in the commands
return false;
*/
}
public static boolean isDirection(String aString) {
return m_words.get(aString).equals("direction");
}
/*
* Print all valid commands to System.out.
*/
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++) {
System.out.print(validCommands[i] + " ");
}
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.
*/
private boolean processCommand(Command command) {
if (command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
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"))
printHelp();
else if (commandWord.equals("go"))
@@ -173,12 +180,12 @@ class Game {
* otherwise print an error message.
*/
private void goRoom(Command command) {
if (!command.hasSecondWord()) {
if (!command.hasDirection()) {
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
String direction = command.getDirection();
// Try to leave current room.
Room nextRoom = currentRoom.nextRoom(direction);
if (nextRoom == null)

View File

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