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
kill, attack
?, help
man, riddler
butler, riddler
helper, riddler
leave, quit
drive, go
move, go

View File

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

View File

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

View File

@@ -16,7 +16,8 @@ import java.util.Scanner;
*/
class CommandWords {
// 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.
*/
@@ -33,6 +34,18 @@ class CommandWords {
}catch (Exception e) {
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;
}
}
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.
*/
@@ -82,4 +103,18 @@ class CommandWords {
}
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 item = "";
String enemy = "";
String
String riddler = "";
boolean open = false;
//String word2;
ArrayList<String> words = new ArrayList<String>();
@@ -66,6 +66,8 @@ class Parser {
item = words.get(i);
}else if(CommandWords.isEnemy(words.get(i))){
enemy = words.get(i);
}else if(CommandWords.isRiddler(words.get(i))) {
riddler = words.get(i);
}else{
otherWords.add(words.get(i));
}
@@ -73,11 +75,11 @@ class Parser {
//System.out.println(verb);
if (CommandWords.isCommand(verb))
if(!open)
return new Command(verb, otherWords, direction, item, enemy);
return new Command(verb, otherWords, direction, item, enemy, riddler);
else
return new Command("open", otherWords, direction, item, enemy);
return new Command("open", otherWords, direction, item, enemy, riddler);
else
return new Command(null, otherWords, direction, item, enemy);
return new Command(null, otherWords, direction, item, enemy, riddler);
}
/**