synonyms, and take all command

This commit is contained in:
jslightham
2019-05-29 10:56:22 -04:00
parent 2d40e5954a
commit bad3490b1e
6 changed files with 90 additions and 6 deletions

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();
}
}
/**
@@ -82,4 +95,16 @@ 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

@@ -16,6 +16,7 @@ public class Player extends Entity{
public boolean addToInventory(Item item){
if(currentInventoryWeight + item.getWeight() < INVENTORY_CAPACITY){
currentInventoryWeight+= item.getWeight();
inventory.add(item);
return true;
}
@@ -26,6 +27,7 @@ public class Player extends Entity{
for(int i =0; i<inventory.size(); i++) {
if(item.equals(inventory.get(i))) {
inventory.remove(i);
currentInventoryWeight-= item.getWeight();
return;
}
}

View File

@@ -330,7 +330,24 @@ class Game {
}
break;
case "take":
if(command.hasItem()) {
boolean hasAll = false;
for(String a : command.getOtherWords()) {
if(a.equals("all"))
hasAll = true;
}
if(hasAll){
for(int i =0; i<currentRoom.getItems().size(); i++) {
if(player.addToInventory(currentRoom.getItem(i))) {
currentRoom.removeItem(i);
i--;
}else {
System.out.println("You can't carry any more!");
break;
}
System.out.println("Taken");
}
}
else if(command.hasItem()) {
Class<?> clazz;
Item object;
try {
@@ -445,11 +462,9 @@ class Game {
}else {
System.out.println("That enemy is not in this room!");
}
}else {
System.out.println("Attack what?");
}
} else {
if(command.hasItem()) {
boolean has = false;
@@ -468,6 +483,7 @@ class Game {
break;
case "read":
break;
default:
return false;
}

View File

@@ -52,6 +52,7 @@ class Parser {
words.add(tokenizer.nextToken());
}
for(int i=0; i<words.size(); i++) {
words.set(i, CommandWords.replaceSynonym(words.get(i)));
if(words.get(i).equals("open") || words.get(i).equals("unlock")) {
open = true;
}