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

@@ -1,7 +1,7 @@
Room name: Circle Room Room name: Circle Room
Room Description: You are in a circular room. The windows to the east are covered with boards that let in just enough light to see.<br>There seems to be some writing on the wall. Room Description: You are in a circular room. The windows to the east are covered with boards that let in just enough light to see.<br>There seems to be some writing on the wall.
Locked: false Locked: false
Items:Lockpick,Milk, Items:Lockpick,Milk,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword,Sword
Riddle: "Who is never hungry during Christmas?", "The turkey because he is always stuffed." Riddle: "Who is never hungry during Christmas?", "The turkey because he is always stuffed."
Exit Rooms: W-Apple Hallway Exit Rooms: W-Apple Hallway

View File

@@ -1 +1,41 @@
This is for the synonyms for the parser suicide, die
grab, take
destroy, attack
kill, attack
?, help
leave, quit
drive, go
move, go
glance, look
glimpse, look
peek, look
gander, look
charge, attack
raid, attack
strike, attack
drown, die
expire, die
perish, die
bounce, jump
dive, jump
fall, jump
rise, jump
free, unlock
release, unlock
leave, drop
raid, attack
bite, eat
chew, eat
devour, eat
dine, eat
feed, eat
ingest, eat
inhale, eat
nibble, eat
swallow, eat
absorb, eat
advice, help
aid, help
benefit, help
scram, go
hightail, go

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

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

View File

@@ -330,7 +330,24 @@ class Game {
} }
break; break;
case "take": 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; Class<?> clazz;
Item object; Item object;
try { try {
@@ -445,11 +462,9 @@ class Game {
}else { }else {
System.out.println("That enemy is not in this room!"); System.out.println("That enemy is not in this room!");
} }
}else { }else {
System.out.println("Attack what?"); System.out.println("Attack what?");
} }
} else { } else {
if(command.hasItem()) { if(command.hasItem()) {
boolean has = false; boolean has = false;
@@ -468,6 +483,7 @@ class Game {
break; break;
case "read": case "read":
break;
default: default:
return false; return false;
} }

View File

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