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.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;
}
*/
}