worked on riddle class

This commit is contained in:
Luca Carnegie
2019-05-29 14:13:10 -04:00
parent 67e7c0ba54
commit 9daad10509
5 changed files with 48 additions and 7 deletions

View File

@@ -3,6 +3,9 @@ grab, take
destroy, attack destroy, attack
kill, attack kill, attack
?, help ?, help
man, riddler
butler, riddler
helper, riddler
leave, quit leave, quit
drive, go drive, go
move, go move, go

View File

@@ -3,6 +3,7 @@ quit, verb
help, verb help, verb
jump, verb jump, verb
read, verb read, verb
talk, verb
north, direction north, direction
south, direction south, direction
west, direction west, direction

View File

@@ -108,7 +108,7 @@ class Command {
return enemy; return enemy;
} }
public boolean hasRiddler() { public boolean hasRiddler() {
return CommandWords.isRiddler(riddler);
} }
public boolean hasEnemy() { public boolean hasEnemy() {
return !enemy.equals(""); return !enemy.equals("");

View File

@@ -16,7 +16,8 @@ import java.util.Scanner;
*/ */
class CommandWords { class CommandWords {
// a constant array that holds all valid command words // a constant array that holds all valid command words
private static HashMap<String, String> m_words = new HashMap<String, String>();; private static HashMap<String, String> m_words = new HashMap<String, String>();
private static HashMap<String, String> m_synonyms = new HashMap<String, String>();
/** /**
* Constructor - initialise the command words. * Constructor - initialise the command words.
*/ */
@@ -33,6 +34,18 @@ class CommandWords {
}catch (Exception e) { }catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
try {
Scanner in = new Scanner(new File("data/synonyms.dat"));
while(in.hasNext()){
String text = in.nextLine();
String[] textarr = text.split(",");
m_synonyms.put(textarr[0], textarr[1].substring(1));
}
in.close();
}catch (Exception e) {
e.printStackTrace();
}
} }
/** /**
@@ -71,6 +84,14 @@ class CommandWords {
return false; return false;
} }
} }
public static boolean isRiddler(String aString) {
try {
return m_words.get(aString).equals("riddler");
}catch(Exception e) {
return false;
}
}
/* /*
* Print all valid commands to System.out. * Print all valid commands to System.out.
*/ */
@@ -82,4 +103,18 @@ class CommandWords {
} }
System.out.println(); System.out.println();
} }
public static String replaceSynonym(String word) {
try {
String words = m_synonyms.get(word);
if(words == null)
throw new Exception();
else
return words;
} catch(Exception e) {
return word;
}
}
} }

View File

@@ -36,7 +36,7 @@ class Parser {
String direction = ""; String direction = "";
String item = ""; String item = "";
String enemy = ""; String enemy = "";
String String riddler = "";
boolean open = false; boolean open = false;
//String word2; //String word2;
ArrayList<String> words = new ArrayList<String>(); ArrayList<String> words = new ArrayList<String>();
@@ -66,6 +66,8 @@ class Parser {
item = words.get(i); item = words.get(i);
}else if(CommandWords.isEnemy(words.get(i))){ }else if(CommandWords.isEnemy(words.get(i))){
enemy = words.get(i); enemy = words.get(i);
}else if(CommandWords.isRiddler(words.get(i))) {
riddler = words.get(i);
}else{ }else{
otherWords.add(words.get(i)); otherWords.add(words.get(i));
} }
@@ -73,11 +75,11 @@ class Parser {
//System.out.println(verb); //System.out.println(verb);
if (CommandWords.isCommand(verb)) if (CommandWords.isCommand(verb))
if(!open) if(!open)
return new Command(verb, otherWords, direction, item, enemy); return new Command(verb, otherWords, direction, item, enemy, riddler);
else else
return new Command("open", otherWords, direction, item, enemy); return new Command("open", otherWords, direction, item, enemy, riddler);
else else
return new Command(null, otherWords, direction, item, enemy); return new Command(null, otherWords, direction, item, enemy, riddler);
} }
/** /**