parser moved outside
This commit is contained in:
parent
6e374918b5
commit
008818c357
14 changed files with 618 additions and 576 deletions
|
@ -16,7 +16,7 @@ mvn clean package
|
||||||
Example inputs can be found in RunThroughResults.txt as to how to beat the sample game. To run the sample game execute:
|
Example inputs can be found in RunThroughResults.txt as to how to beat the sample game. To run the sample game execute:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
mvn exc:java
|
mvn exec:java
|
||||||
```
|
```
|
||||||
|
|
||||||
This command will load the `sampleGame.xml` XML game spec.
|
This command will load the `sampleGame.xml` XML game spec.
|
|
@ -5,12 +5,9 @@
|
||||||
|
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
import org.w3c.dom.CharacterData;
|
import com.github.dtschust.zork.parser.ZorkGame;
|
||||||
import org.w3c.dom.*;
|
import com.github.dtschust.zork.parser.ZorkReader;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
@ -18,478 +15,16 @@ import java.util.Scanner;
|
||||||
/* And away we go*/
|
/* And away we go*/
|
||||||
public class Zork {
|
public class Zork {
|
||||||
public String userInput = "";
|
public String userInput = "";
|
||||||
/*Hashmaps to store the instance of the game*/
|
|
||||||
public HashMap<String, ZorkRoom> Rooms = new HashMap<>();
|
|
||||||
public HashMap<String, ZorkItem> Items = new HashMap<>();
|
|
||||||
public HashMap<String, ZorkContainer> Containers = new HashMap<>();
|
|
||||||
public HashMap<String, ZorkObject> Objects = new HashMap<>();
|
|
||||||
public HashMap<String, ZorkCreature> Creatures = new HashMap<>();
|
|
||||||
public HashMap<String, String> Inventory = new HashMap<>();
|
|
||||||
public HashMap<String, String> ObjectLookup = new HashMap<>();
|
|
||||||
public String currentRoom;
|
public String currentRoom;
|
||||||
public File file;
|
public ZorkGame game;
|
||||||
Scanner source = new Scanner(System.in);
|
Scanner source = new Scanner(System.in);
|
||||||
|
|
||||||
public Zork(String filename) {
|
public Zork(String filename) {
|
||||||
int i, j, k, l, x, y, z;
|
|
||||||
|
|
||||||
|
|
||||||
file = new File(filename);
|
ZorkReader reader = new ZorkReader(filename);
|
||||||
if (!file.canRead()) {
|
|
||||||
System.out.println("Error opening file. Exiting...");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
/* Open the xml file*/
|
|
||||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
|
||||||
Document doc = builder.parse(file);
|
|
||||||
|
|
||||||
Element rootElement = doc.getDocumentElement();
|
|
||||||
|
|
||||||
/* Every single first generation child is a room, container, creature, or item. So load them in*/
|
|
||||||
NodeList nodes = rootElement.getChildNodes();
|
|
||||||
for (k = 0; k < nodes.getLength(); k++) {
|
|
||||||
Node node = nodes.item(k);
|
|
||||||
Element element;
|
|
||||||
if (node instanceof Element) {
|
|
||||||
element = (Element) node;
|
|
||||||
String tagType = element.getTagName();
|
|
||||||
|
|
||||||
/* If it's a room ... */
|
|
||||||
if (tagType.equals("room")) {
|
|
||||||
ZorkRoom tempRoom = new ZorkRoom();
|
|
||||||
|
|
||||||
/*Get all possible Room attributes*/
|
|
||||||
|
|
||||||
NodeList name = element.getElementsByTagName("name");
|
|
||||||
tempRoom.name = getString((Element) name.item(0));
|
|
||||||
NodeList type = element.getElementsByTagName("type");
|
|
||||||
if (type.getLength() > 0) {
|
|
||||||
tempRoom.type = getString((Element) type.item(0));
|
|
||||||
} else {
|
|
||||||
tempRoom.type = "regular";
|
|
||||||
}
|
|
||||||
NodeList status = element.getElementsByTagName("status");
|
|
||||||
if (status.getLength() > 0) {
|
|
||||||
tempRoom.status = getString((Element) type.item(0));
|
|
||||||
} else {
|
|
||||||
tempRoom.status = "";
|
|
||||||
}
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
tempRoom.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList items = element.getElementsByTagName("item");
|
|
||||||
for (j = 0; j < items.getLength(); j++) {
|
|
||||||
Element item = (Element) items.item(j);
|
|
||||||
String itemName = getString(item);
|
|
||||||
tempRoom.item.put(itemName, itemName);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList creatures = element.getElementsByTagName("creature");
|
|
||||||
for (j = 0; j < creatures.getLength(); j++) {
|
|
||||||
Element creature = (Element) creatures.item(j);
|
|
||||||
String creatureName = getString(creature);
|
|
||||||
tempRoom.creature.put(creatureName, creatureName);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList triggers = element.getElementsByTagName("trigger");
|
|
||||||
for (j = 0; j < triggers.getLength(); j++) {
|
|
||||||
ZorkTrigger tempTrigger = new ZorkTrigger();
|
|
||||||
Element trigger = (Element) triggers.item(j);
|
|
||||||
NodeList commands = trigger.getElementsByTagName("command");
|
|
||||||
for (l = 0; l < commands.getLength(); l++) {
|
|
||||||
Element command = (Element) commands.item(l);
|
|
||||||
ZorkCommand tempCommand = new ZorkCommand();
|
|
||||||
tempCommand.command = getString(command);
|
|
||||||
tempTrigger.conditions.add(tempCommand);
|
|
||||||
tempTrigger.hasCommand = true;
|
|
||||||
}
|
|
||||||
NodeList conditions = trigger.getElementsByTagName("condition");
|
|
||||||
for (l = 0; l < conditions.getLength(); l++) {
|
|
||||||
Element condition = (Element) conditions.item(l);
|
|
||||||
NodeList object = condition.getElementsByTagName("object");
|
|
||||||
NodeList has = condition.getElementsByTagName("has");
|
|
||||||
if (has.getLength() > 0) {
|
|
||||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
|
||||||
tempConditionHas.has = getString((Element) has.item(0));
|
|
||||||
tempConditionHas.object = getString((Element) object.item(0));
|
|
||||||
NodeList owner = condition.getElementsByTagName("owner");
|
|
||||||
tempConditionHas.owner = getString((Element) owner.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionHas);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
|
||||||
tempConditionStatus.object = getString((Element) object.item(0));
|
|
||||||
NodeList sstatus = condition.getElementsByTagName("status");
|
|
||||||
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
NodeList ttype = element.getElementsByTagName("type");
|
|
||||||
if (ttype.getLength() > 0) {
|
|
||||||
tempTrigger.type = getString((Element) ttype.item(0));
|
|
||||||
} else {
|
|
||||||
tempTrigger.type = "single";
|
|
||||||
}
|
|
||||||
NodeList prints = trigger.getElementsByTagName("print");
|
|
||||||
for (l = 0; l < prints.getLength(); l++) {
|
|
||||||
Element print = (Element) prints.item(l);
|
|
||||||
tempTrigger.print.add(getString(print));
|
|
||||||
}
|
|
||||||
NodeList actions = trigger.getElementsByTagName("action");
|
|
||||||
for (l = 0; l < actions.getLength(); l++) {
|
|
||||||
Element action = (Element) actions.item(l);
|
|
||||||
tempTrigger.action.add(getString(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
tempRoom.trigger.add(tempTrigger);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList containers = element.getElementsByTagName("container");
|
|
||||||
for (j = 0; j < containers.getLength(); j++) {
|
|
||||||
Element container = (Element) containers.item(j);
|
|
||||||
String containerName = getString(container);
|
|
||||||
tempRoom.container.put(containerName, containerName);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList borders = element.getElementsByTagName("border");
|
|
||||||
for (j = 0; j < borders.getLength(); j++) {
|
|
||||||
Element border = (Element) borders.item(j);
|
|
||||||
String borderdirection = getString((Element) border.getElementsByTagName("direction").item(0));
|
|
||||||
String bordername = getString((Element) border.getElementsByTagName("name").item(0));
|
|
||||||
tempRoom.border.put(borderdirection, bordername);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/
|
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
|
||||||
Objects.put(tempRoom.name, tempRoom);
|
|
||||||
ObjectLookup.put(tempRoom.name, "room");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If it's an item... */
|
|
||||||
else if (tagType.equals("item")) {
|
|
||||||
ZorkItem tempItem = new ZorkItem();
|
|
||||||
|
|
||||||
/* Get all possible item attributes*/
|
|
||||||
NodeList name = element.getElementsByTagName("name");
|
|
||||||
if (name.getLength() > 0)
|
|
||||||
tempItem.name = getString((Element) name.item(0));
|
|
||||||
|
|
||||||
NodeList status = element.getElementsByTagName("status");
|
|
||||||
if (status.getLength() > 0) {
|
|
||||||
tempItem.status = getString((Element) status.item(0));
|
|
||||||
} else {
|
|
||||||
tempItem.status = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
if (description.getLength() > 0)
|
|
||||||
tempItem.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList writing = element.getElementsByTagName("writing");
|
|
||||||
if (writing.getLength() > 0)
|
|
||||||
tempItem.writing = getString((Element) writing.item(0));
|
|
||||||
|
|
||||||
NodeList turnon = element.getElementsByTagName("turnon");
|
|
||||||
if (turnon.getLength() > 0) {
|
|
||||||
NodeList prints = element.getElementsByTagName("print");
|
|
||||||
for (j = 0; j < prints.getLength(); j++) {
|
|
||||||
tempItem.turnOnPrint.add(getString((Element) prints.item(j)));
|
|
||||||
}
|
|
||||||
NodeList actions = element.getElementsByTagName("action");
|
|
||||||
for (j = 0; j < actions.getLength(); j++) {
|
|
||||||
tempItem.turnOnAction.add(getString((Element) actions.item(j)));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList triggers = element.getElementsByTagName("trigger");
|
|
||||||
for (j = 0; j < triggers.getLength(); j++) {
|
|
||||||
ZorkTrigger tempTrigger = new ZorkTrigger();
|
|
||||||
Element trigger = (Element) triggers.item(j);
|
|
||||||
NodeList commands = trigger.getElementsByTagName("command");
|
|
||||||
for (l = 0; l < commands.getLength(); l++) {
|
|
||||||
Element command = (Element) commands.item(l);
|
|
||||||
ZorkCommand tempCommand = new ZorkCommand();
|
|
||||||
tempCommand.command = getString(command);
|
|
||||||
tempTrigger.conditions.add(tempCommand);
|
|
||||||
tempTrigger.hasCommand = true;
|
|
||||||
}
|
|
||||||
NodeList conditions = trigger.getElementsByTagName("condition");
|
|
||||||
for (l = 0; l < conditions.getLength(); l++) {
|
|
||||||
Element condition = (Element) conditions.item(l);
|
|
||||||
NodeList object = condition.getElementsByTagName("object");
|
|
||||||
NodeList has = condition.getElementsByTagName("has");
|
|
||||||
if (has.getLength() > 0) {
|
|
||||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
|
||||||
tempConditionHas.has = getString((Element) has.item(0));
|
|
||||||
tempConditionHas.object = getString((Element) object.item(0));
|
|
||||||
NodeList owner = condition.getElementsByTagName("owner");
|
|
||||||
tempConditionHas.owner = getString((Element) owner.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionHas);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
|
||||||
tempConditionStatus.object = getString((Element) object.item(0));
|
|
||||||
NodeList sstatus = condition.getElementsByTagName("status");
|
|
||||||
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
NodeList ttype = element.getElementsByTagName("type");
|
|
||||||
if (ttype.getLength() > 0) {
|
|
||||||
tempTrigger.type = getString((Element) ttype.item(0));
|
|
||||||
} else {
|
|
||||||
tempTrigger.type = "single";
|
|
||||||
}
|
|
||||||
NodeList prints = trigger.getElementsByTagName("print");
|
|
||||||
for (l = 0; l < prints.getLength(); l++) {
|
|
||||||
Element print = (Element) prints.item(l);
|
|
||||||
tempTrigger.print.add(getString(print));
|
|
||||||
}
|
|
||||||
NodeList actions = trigger.getElementsByTagName("action");
|
|
||||||
for (l = 0; l < actions.getLength(); l++) {
|
|
||||||
Element action = (Element) actions.item(l);
|
|
||||||
tempTrigger.action.add(getString(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
tempItem.trigger.add(tempTrigger);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
|
|
||||||
Items.put(tempItem.name, tempItem);
|
|
||||||
Objects.put(tempItem.name, tempItem);
|
|
||||||
ObjectLookup.put(tempItem.name, "item");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If it's a container... */
|
|
||||||
else if (tagType.equals("container")) {
|
|
||||||
ZorkContainer tempCont = new ZorkContainer();
|
|
||||||
|
|
||||||
/*Get all possible container attributes*/
|
|
||||||
|
|
||||||
NodeList name = element.getElementsByTagName("name");
|
|
||||||
if (name.getLength() > 0)
|
|
||||||
tempCont.name = getString((Element) name.item(0));
|
|
||||||
|
|
||||||
NodeList status = element.getElementsByTagName("status");
|
|
||||||
if (status.getLength() > 0)
|
|
||||||
tempCont.status = getString((Element) status.item(0));
|
|
||||||
|
|
||||||
/*Initially assume a closed container*/
|
|
||||||
tempCont.isOpen = false;
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
if (description.getLength() > 0)
|
|
||||||
tempCont.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList accepts = element.getElementsByTagName("accept");
|
|
||||||
for (j = 0; j < accepts.getLength(); j++) {
|
|
||||||
/* If container has an accepts attribute, then it is always open*/
|
|
||||||
tempCont.isOpen = true;
|
|
||||||
tempCont.accept.add(getString((Element) accepts.item(j)));
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList citems = element.getElementsByTagName("item");
|
|
||||||
for (j = 0; j < citems.getLength(); j++) {
|
|
||||||
Element item = (Element) citems.item(j);
|
|
||||||
String itemName = getString(item);
|
|
||||||
tempCont.item.put(itemName, itemName);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList triggers = element.getElementsByTagName("trigger");
|
|
||||||
for (j = 0; j < triggers.getLength(); j++) {
|
|
||||||
ZorkTrigger tempTrigger = new ZorkTrigger();
|
|
||||||
Element trigger = (Element) triggers.item(j);
|
|
||||||
NodeList commands = trigger.getElementsByTagName("command");
|
|
||||||
for (l = 0; l < commands.getLength(); l++) {
|
|
||||||
Element command = (Element) commands.item(l);
|
|
||||||
ZorkCommand tempCommand = new ZorkCommand();
|
|
||||||
tempCommand.command = getString(command);
|
|
||||||
tempTrigger.conditions.add(tempCommand);
|
|
||||||
tempTrigger.hasCommand = true;
|
|
||||||
}
|
|
||||||
NodeList conditions = trigger.getElementsByTagName("condition");
|
|
||||||
for (l = 0; l < conditions.getLength(); l++) {
|
|
||||||
Element condition = (Element) conditions.item(l);
|
|
||||||
NodeList object = condition.getElementsByTagName("object");
|
|
||||||
NodeList has = condition.getElementsByTagName("has");
|
|
||||||
if (has.getLength() > 0) {
|
|
||||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
|
||||||
tempConditionHas.has = getString((Element) has.item(0));
|
|
||||||
tempConditionHas.object = getString((Element) object.item(0));
|
|
||||||
NodeList owner = condition.getElementsByTagName("owner");
|
|
||||||
tempConditionHas.owner = getString((Element) owner.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionHas);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
|
||||||
tempConditionStatus.object = getString((Element) object.item(0));
|
|
||||||
NodeList sstatus = condition.getElementsByTagName("status");
|
|
||||||
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
NodeList ttype = element.getElementsByTagName("type");
|
|
||||||
if (ttype.getLength() > 0) {
|
|
||||||
tempTrigger.type = getString((Element) ttype.item(0));
|
|
||||||
} else {
|
|
||||||
tempTrigger.type = "single";
|
|
||||||
}
|
|
||||||
NodeList prints = trigger.getElementsByTagName("print");
|
|
||||||
for (l = 0; l < prints.getLength(); l++) {
|
|
||||||
Element print = (Element) prints.item(l);
|
|
||||||
tempTrigger.print.add(getString(print));
|
|
||||||
}
|
|
||||||
NodeList actions = trigger.getElementsByTagName("action");
|
|
||||||
for (l = 0; l < actions.getLength(); l++) {
|
|
||||||
Element action = (Element) actions.item(l);
|
|
||||||
tempTrigger.action.add(getString(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
tempCont.trigger.add(tempTrigger);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
|
||||||
Containers.put(tempCont.name, tempCont);
|
|
||||||
Objects.put(tempCont.name, tempCont);
|
|
||||||
ObjectLookup.put(tempCont.name, "container");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* And finally, if it's a creature...*/
|
|
||||||
else if (tagType.equals("creature")) {
|
|
||||||
ZorkCreature tempCreature = new ZorkCreature();
|
|
||||||
|
|
||||||
/* Get all possible creature attributes*/
|
|
||||||
|
|
||||||
NodeList name = element.getElementsByTagName("name");
|
|
||||||
if (name.getLength() > 0)
|
|
||||||
tempCreature.name = getString((Element) name.item(0));
|
|
||||||
|
|
||||||
NodeList status = element.getElementsByTagName("status");
|
|
||||||
if (status.getLength() > 0)
|
|
||||||
tempCreature.status = getString((Element) status.item(0));
|
|
||||||
|
|
||||||
NodeList description = element.getElementsByTagName("description");
|
|
||||||
if (description.getLength() > 0)
|
|
||||||
tempCreature.description = getString((Element) description.item(0));
|
|
||||||
|
|
||||||
NodeList vulns = element.getElementsByTagName("vulnerability");
|
|
||||||
for (j = 0; j < vulns.getLength(); j++) {
|
|
||||||
String vulnString = getString((Element) vulns.item(j));
|
|
||||||
tempCreature.vulnerability.put(vulnString, vulnString);
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList attacks = element.getElementsByTagName("attack");
|
|
||||||
for (j = 0; j < attacks.getLength(); j++) {
|
|
||||||
Element attack = (Element) attacks.item(j);
|
|
||||||
NodeList conditions = attack.getElementsByTagName("condition");
|
|
||||||
for (l = 0; l < conditions.getLength(); l++) {
|
|
||||||
Element condition = (Element) conditions.item(l);
|
|
||||||
NodeList object = condition.getElementsByTagName("object");
|
|
||||||
NodeList has = condition.getElementsByTagName("has");
|
|
||||||
if (has.getLength() > 0) {
|
|
||||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
|
||||||
tempConditionHas.has = getString((Element) has.item(0));
|
|
||||||
tempConditionHas.object = getString((Element) object.item(0));
|
|
||||||
NodeList owner = condition.getElementsByTagName("owner");
|
|
||||||
tempConditionHas.owner = getString((Element) owner.item(0));
|
|
||||||
tempCreature.conditions.add(tempConditionHas);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
|
||||||
tempConditionStatus.object = getString((Element) object.item(0));
|
|
||||||
NodeList sstatus = condition.getElementsByTagName("status");
|
|
||||||
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
|
||||||
tempCreature.conditions.add(tempConditionStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
NodeList prints = attack.getElementsByTagName("print");
|
|
||||||
for (l = 0; l < prints.getLength(); l++) {
|
|
||||||
Element print = (Element) prints.item(l);
|
|
||||||
tempCreature.print.add(getString(print));
|
|
||||||
}
|
|
||||||
NodeList actions = attack.getElementsByTagName("action");
|
|
||||||
for (l = 0; l < actions.getLength(); l++) {
|
|
||||||
Element action = (Element) actions.item(l);
|
|
||||||
tempCreature.action.add(getString(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeList triggers = element.getElementsByTagName("trigger");
|
|
||||||
for (j = 0; j < triggers.getLength(); j++) {
|
|
||||||
ZorkTrigger tempTrigger = new ZorkTrigger();
|
|
||||||
Element trigger = (Element) triggers.item(j);
|
|
||||||
NodeList commands = trigger.getElementsByTagName("command");
|
|
||||||
for (l = 0; l < commands.getLength(); l++) {
|
|
||||||
Element command = (Element) commands.item(l);
|
|
||||||
ZorkCommand tempCommand = new ZorkCommand();
|
|
||||||
tempCommand.command = getString(command);
|
|
||||||
tempTrigger.conditions.add(tempCommand);
|
|
||||||
tempTrigger.hasCommand = true;
|
|
||||||
}
|
|
||||||
NodeList conditions = trigger.getElementsByTagName("condition");
|
|
||||||
for (l = 0; l < conditions.getLength(); l++) {
|
|
||||||
Element condition = (Element) conditions.item(l);
|
|
||||||
NodeList object = condition.getElementsByTagName("object");
|
|
||||||
NodeList has = condition.getElementsByTagName("has");
|
|
||||||
if (has.getLength() > 0) {
|
|
||||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
|
||||||
tempConditionHas.has = getString((Element) has.item(0));
|
|
||||||
tempConditionHas.object = getString((Element) object.item(0));
|
|
||||||
NodeList owner = condition.getElementsByTagName("owner");
|
|
||||||
tempConditionHas.owner = getString((Element) owner.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionHas);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
|
||||||
tempConditionStatus.object = getString((Element) object.item(0));
|
|
||||||
NodeList sstatus = condition.getElementsByTagName("status");
|
|
||||||
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
|
||||||
tempTrigger.conditions.add(tempConditionStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
NodeList ttype = element.getElementsByTagName("type");
|
|
||||||
if (ttype.getLength() > 0) {
|
|
||||||
tempTrigger.type = getString((Element) ttype.item(0));
|
|
||||||
} else {
|
|
||||||
tempTrigger.type = "single";
|
|
||||||
}
|
|
||||||
NodeList prints = trigger.getElementsByTagName("print");
|
|
||||||
for (l = 0; l < prints.getLength(); l++) {
|
|
||||||
Element print = (Element) prints.item(l);
|
|
||||||
tempTrigger.print.add(getString(print));
|
|
||||||
}
|
|
||||||
NodeList actions = trigger.getElementsByTagName("action");
|
|
||||||
for (l = 0; l < actions.getLength(); l++) {
|
|
||||||
Element action = (Element) actions.item(l);
|
|
||||||
tempTrigger.action.add(getString(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
tempCreature.trigger.add(tempTrigger);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
|
||||||
Creatures.put(tempCreature.name, tempCreature);
|
|
||||||
Objects.put(tempCreature.name, tempCreature);
|
|
||||||
ObjectLookup.put(tempCreature.name, "creature");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("Invalid XML file, exiting");
|
|
||||||
System.exit(-1);
|
|
||||||
//e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
game = reader.build();
|
||||||
/*Phew, we're finally done with that ... let's get playing!!*/
|
/*Phew, we're finally done with that ... let's get playing!!*/
|
||||||
|
|
||||||
/* Some temporary initialization variables*/
|
/* Some temporary initialization variables*/
|
||||||
|
@ -501,7 +36,7 @@ public class Zork {
|
||||||
|
|
||||||
|
|
||||||
/* Print out the first entrance description, starting the game!*/
|
/* Print out the first entrance description, starting the game!*/
|
||||||
System.out.println(Rooms.get(currentRoom).description);
|
System.out.println(game.Rooms.get(currentRoom).description);
|
||||||
|
|
||||||
/* There is no stopping in Zork, until we're done!!*/
|
/* There is no stopping in Zork, until we're done!!*/
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -525,15 +60,7 @@ public class Zork {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get a string from an element (XML parsing stuff)*/
|
|
||||||
public static String getString(Element e) {
|
|
||||||
Node child = e.getFirstChild();
|
|
||||||
if (child instanceof CharacterData) {
|
|
||||||
CharacterData cd = (CharacterData) child;
|
|
||||||
return cd.getData();
|
|
||||||
}
|
|
||||||
return "?";
|
|
||||||
}
|
|
||||||
|
|
||||||
/* I love how basic java main functions are sometimes.*/
|
/* I love how basic java main functions are sometimes.*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -561,21 +88,21 @@ public class Zork {
|
||||||
/* Take */
|
/* Take */
|
||||||
else if (input.startsWith("take") && input.split(" ").length >= 1) {
|
else if (input.startsWith("take") && input.split(" ").length >= 1) {
|
||||||
tempString = input.split(" ")[1];
|
tempString = input.split(" ")[1];
|
||||||
if ((Rooms.get(currentRoom)).item.get(tempString) != null) {
|
if ((game.Rooms.get(currentRoom)).item.get(tempString) != null) {
|
||||||
Inventory.put(tempString, tempString);
|
game.Inventory.put(tempString, tempString);
|
||||||
ZorkRoom tempRoom = (Rooms.get(currentRoom));
|
ZorkRoom tempRoom = (game.Rooms.get(currentRoom));
|
||||||
tempRoom.item.remove(tempString);
|
tempRoom.item.remove(tempString);
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
System.out.println("Item " + tempString + " added to inventory.");
|
System.out.println("Item " + tempString + " added to inventory.");
|
||||||
} else {
|
} else {
|
||||||
/*Search all containers in the current room for the item!*/
|
/*Search all containers in the current room for the item!*/
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (String key : Rooms.get(currentRoom).container.keySet()) {
|
for (String key : game.Rooms.get(currentRoom).container.keySet()) {
|
||||||
tempContainer = Containers.get(key);
|
tempContainer = game.Containers.get(key);
|
||||||
if (tempContainer != null && tempContainer.isOpen && tempContainer.item.get(tempString) != null) {
|
if (tempContainer != null && tempContainer.isOpen && tempContainer.item.get(tempString) != null) {
|
||||||
Inventory.put(tempString, tempString);
|
game.Inventory.put(tempString, tempString);
|
||||||
tempContainer.item.remove(tempString);
|
tempContainer.item.remove(tempString);
|
||||||
Containers.put(tempContainer.name, tempContainer);
|
game.Containers.put(tempContainer.name, tempContainer);
|
||||||
System.out.println("Item " + tempString + " added to inventory.");
|
System.out.println("Item " + tempString + " added to inventory.");
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
@ -587,7 +114,7 @@ public class Zork {
|
||||||
}
|
}
|
||||||
/* Open Exit (you should be so lucky)*/
|
/* Open Exit (you should be so lucky)*/
|
||||||
else if (input.equals("open exit")) {
|
else if (input.equals("open exit")) {
|
||||||
if (Rooms.get(currentRoom).type.equals("exit")) {
|
if (game.Rooms.get(currentRoom).type.equals("exit")) {
|
||||||
System.out.println("Game Over");
|
System.out.println("Game Over");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
} else {
|
} else {
|
||||||
|
@ -597,9 +124,9 @@ public class Zork {
|
||||||
/* Open a container */
|
/* Open a container */
|
||||||
else if (input.startsWith("open") && input.split(" ").length >= 1) {
|
else if (input.startsWith("open") && input.split(" ").length >= 1) {
|
||||||
tempString = input.split(" ")[1];
|
tempString = input.split(" ")[1];
|
||||||
String found = Rooms.get(currentRoom).container.get(tempString);
|
String found = game.Rooms.get(currentRoom).container.get(tempString);
|
||||||
if (found != null) {
|
if (found != null) {
|
||||||
tempContainer = Containers.get(tempString);
|
tempContainer = game.Containers.get(tempString);
|
||||||
tempContainer.isOpen = true;
|
tempContainer.isOpen = true;
|
||||||
containerInventory(tempContainer.item, tempString);
|
containerInventory(tempContainer.item, tempString);
|
||||||
} else {
|
} else {
|
||||||
|
@ -610,8 +137,8 @@ public class Zork {
|
||||||
else if (input.startsWith("read") && input.split(" ").length >= 1) {
|
else if (input.startsWith("read") && input.split(" ").length >= 1) {
|
||||||
tempString = input.split(" ")[1];
|
tempString = input.split(" ")[1];
|
||||||
ZorkItem tempItem;
|
ZorkItem tempItem;
|
||||||
if (Inventory.get(tempString) != null) {
|
if (game.Inventory.get(tempString) != null) {
|
||||||
tempItem = Items.get(tempString);
|
tempItem = game.Items.get(tempString);
|
||||||
if (tempItem.writing != null && tempItem.writing != "") {
|
if (tempItem.writing != null && tempItem.writing != "") {
|
||||||
System.out.println(tempItem.writing);
|
System.out.println(tempItem.writing);
|
||||||
} else {
|
} else {
|
||||||
|
@ -624,11 +151,11 @@ public class Zork {
|
||||||
/* Drop an item*/
|
/* Drop an item*/
|
||||||
else if (input.startsWith("drop") && input.split(" ").length >= 1) {
|
else if (input.startsWith("drop") && input.split(" ").length >= 1) {
|
||||||
tempString = input.split(" ")[1];
|
tempString = input.split(" ")[1];
|
||||||
if (Inventory.get(tempString) != null) {
|
if (game.Inventory.get(tempString) != null) {
|
||||||
ZorkRoom tempRoom = Rooms.get(currentRoom);
|
ZorkRoom tempRoom = game.Rooms.get(currentRoom);
|
||||||
tempRoom.item.put(tempString, tempString);
|
tempRoom.item.put(tempString, tempString);
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
Inventory.remove(tempString);
|
game.Inventory.remove(tempString);
|
||||||
System.out.println(tempString + " dropped.");
|
System.out.println(tempString + " dropped.");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Error");
|
System.out.println("Error");
|
||||||
|
@ -638,10 +165,10 @@ public class Zork {
|
||||||
else if (input.startsWith("put") && input.split(" ").length >= 4) {
|
else if (input.startsWith("put") && input.split(" ").length >= 4) {
|
||||||
tempString = input.split(" ")[1];
|
tempString = input.split(" ")[1];
|
||||||
String destination = input.split(" ")[3];
|
String destination = input.split(" ")[3];
|
||||||
if (Rooms.get(currentRoom).container.get(destination) != null && Containers.get(destination).isOpen && Inventory.get(tempString) != null) {
|
if (game.Rooms.get(currentRoom).container.get(destination) != null && game.Containers.get(destination).isOpen && game.Inventory.get(tempString) != null) {
|
||||||
tempContainer = Containers.get(destination);
|
tempContainer = game.Containers.get(destination);
|
||||||
tempContainer.item.put(tempString, tempString);
|
tempContainer.item.put(tempString, tempString);
|
||||||
Inventory.remove(tempString);
|
game.Inventory.remove(tempString);
|
||||||
System.out.println("Item " + tempString + " added to " + destination + ".");
|
System.out.println("Item " + tempString + " added to " + destination + ".");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Error");
|
System.out.println("Error");
|
||||||
|
@ -651,8 +178,8 @@ public class Zork {
|
||||||
else if (input.startsWith("turn on") && input.split(" ").length >= 3) {
|
else if (input.startsWith("turn on") && input.split(" ").length >= 3) {
|
||||||
tempString = input.split(" ")[2];
|
tempString = input.split(" ")[2];
|
||||||
ZorkItem tempItem;
|
ZorkItem tempItem;
|
||||||
if (Inventory.get(tempString) != null) {
|
if (game.Inventory.get(tempString) != null) {
|
||||||
tempItem = Items.get(tempString);
|
tempItem = game.Items.get(tempString);
|
||||||
System.out.println("You activate the " + tempString + ".");
|
System.out.println("You activate the " + tempString + ".");
|
||||||
if (tempItem != null) {
|
if (tempItem != null) {
|
||||||
for (y = 0; y < tempItem.turnOnPrint.size(); y++) {
|
for (y = 0; y < tempItem.turnOnPrint.size(); y++) {
|
||||||
|
@ -674,9 +201,9 @@ public class Zork {
|
||||||
tempString = input.split(" ")[1];
|
tempString = input.split(" ")[1];
|
||||||
ZorkCreature tempCreature;
|
ZorkCreature tempCreature;
|
||||||
String weapon = input.split(" ")[3];
|
String weapon = input.split(" ")[3];
|
||||||
if (Rooms.get(currentRoom).creature.get(tempString) != null) {
|
if (game.Rooms.get(currentRoom).creature.get(tempString) != null) {
|
||||||
tempCreature = Creatures.get(tempString);
|
tempCreature = game.Creatures.get(tempString);
|
||||||
if (tempCreature != null && Inventory.get(weapon) != null) {
|
if (tempCreature != null && game.Inventory.get(weapon) != null) {
|
||||||
if (tempCreature.attack(this, weapon)) {
|
if (tempCreature.attack(this, weapon)) {
|
||||||
System.out.println("You assault the " + tempString + " with the " + weapon + ".");
|
System.out.println("You assault the " + tempString + " with the " + weapon + ".");
|
||||||
for (y = 0; y < tempCreature.print.size(); y++) {
|
for (y = 0; y < tempCreature.print.size(); y++) {
|
||||||
|
@ -710,8 +237,8 @@ public class Zork {
|
||||||
ZorkContainer tempContainer;
|
ZorkContainer tempContainer;
|
||||||
|
|
||||||
/*Check Room triggers*/
|
/*Check Room triggers*/
|
||||||
for (x = 0; x < Rooms.get(currentRoom).trigger.size(); x++) {
|
for (x = 0; x < game.Rooms.get(currentRoom).trigger.size(); x++) {
|
||||||
tempTrigger = Rooms.get(currentRoom).trigger.get(x);
|
tempTrigger = game.Rooms.get(currentRoom).trigger.get(x);
|
||||||
if (tempTrigger.evaluate(this)) {
|
if (tempTrigger.evaluate(this)) {
|
||||||
for (y = 0; y < tempTrigger.print.size(); y++) {
|
for (y = 0; y < tempTrigger.print.size(); y++) {
|
||||||
System.out.println(tempTrigger.print.get(y));
|
System.out.println(tempTrigger.print.get(y));
|
||||||
|
@ -723,16 +250,16 @@ public class Zork {
|
||||||
skip = true;
|
skip = true;
|
||||||
}
|
}
|
||||||
if (tempTrigger.type.equals("single")) {
|
if (tempTrigger.type.equals("single")) {
|
||||||
Rooms.get(currentRoom).trigger.remove(x);
|
game.Rooms.get(currentRoom).trigger.remove(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check items in the containers in the room */
|
/* Check items in the containers in the room */
|
||||||
for (String key : Rooms.get(currentRoom).container.keySet()) {
|
for (String key : game.Rooms.get(currentRoom).container.keySet()) {
|
||||||
tempContainer = Containers.get(key);
|
tempContainer = game.Containers.get(key);
|
||||||
for (String key2 : tempContainer.item.keySet()) {
|
for (String key2 : tempContainer.item.keySet()) {
|
||||||
ZorkItem tempItem = Items.get(key2);
|
ZorkItem tempItem = game.Items.get(key2);
|
||||||
for (x = 0; x < tempItem.trigger.size(); x++) {
|
for (x = 0; x < tempItem.trigger.size(); x++) {
|
||||||
tempTrigger = tempItem.trigger.get(x);
|
tempTrigger = tempItem.trigger.get(x);
|
||||||
if (tempTrigger.evaluate(this)) {
|
if (tempTrigger.evaluate(this)) {
|
||||||
|
@ -773,8 +300,8 @@ public class Zork {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check all creatures in the room */
|
/* Check all creatures in the room */
|
||||||
for (String key : Rooms.get(currentRoom).creature.keySet()) {
|
for (String key : game.Rooms.get(currentRoom).creature.keySet()) {
|
||||||
ZorkCreature tempCreature = Creatures.get(key);
|
ZorkCreature tempCreature = game.Creatures.get(key);
|
||||||
for (x = 0; x < tempCreature.trigger.size(); x++) {
|
for (x = 0; x < tempCreature.trigger.size(); x++) {
|
||||||
tempTrigger = tempCreature.trigger.get(x);
|
tempTrigger = tempCreature.trigger.get(x);
|
||||||
if (tempTrigger.evaluate(this)) {
|
if (tempTrigger.evaluate(this)) {
|
||||||
|
@ -795,8 +322,8 @@ public class Zork {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check items in inventory */
|
/* Check items in inventory */
|
||||||
for (String key : Inventory.keySet()) {
|
for (String key : game.Inventory.keySet()) {
|
||||||
ZorkItem tempItem = Items.get(key);
|
ZorkItem tempItem = game.Items.get(key);
|
||||||
for (x = 0; x < tempItem.trigger.size(); x++) {
|
for (x = 0; x < tempItem.trigger.size(); x++) {
|
||||||
tempTrigger = tempItem.trigger.get(x);
|
tempTrigger = tempItem.trigger.get(x);
|
||||||
if (tempTrigger.evaluate(this)) {
|
if (tempTrigger.evaluate(this)) {
|
||||||
|
@ -817,8 +344,8 @@ public class Zork {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check items in room */
|
/* Check items in room */
|
||||||
for (String key : Rooms.get(currentRoom).item.keySet()) {
|
for (String key : game.Rooms.get(currentRoom).item.keySet()) {
|
||||||
ZorkItem tempItem = Items.get(key);
|
ZorkItem tempItem = game.Items.get(key);
|
||||||
for (x = 0; x < tempItem.trigger.size(); x++) {
|
for (x = 0; x < tempItem.trigger.size(); x++) {
|
||||||
tempTrigger = tempItem.trigger.get(x);
|
tempTrigger = tempItem.trigger.get(x);
|
||||||
if (tempTrigger.evaluate(this)) {
|
if (tempTrigger.evaluate(this)) {
|
||||||
|
@ -854,10 +381,10 @@ public class Zork {
|
||||||
fullDirection = "west";
|
fullDirection = "west";
|
||||||
}
|
}
|
||||||
|
|
||||||
destination = (Rooms.get(currentRoom)).border.get(fullDirection);
|
destination = (game.Rooms.get(currentRoom)).border.get(fullDirection);
|
||||||
if (destination != null) {
|
if (destination != null) {
|
||||||
currentRoom = destination;
|
currentRoom = destination;
|
||||||
System.out.println(Rooms.get(currentRoom).description);
|
System.out.println(game.Rooms.get(currentRoom).description);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Can't go that way.");
|
System.out.println("Can't go that way.");
|
||||||
}
|
}
|
||||||
|
@ -872,25 +399,25 @@ public class Zork {
|
||||||
object = action.split(" ")[1];
|
object = action.split(" ")[1];
|
||||||
String newStatus = action.split(" ")[3];
|
String newStatus = action.split(" ")[3];
|
||||||
|
|
||||||
objectType = ObjectLookup.get(object);
|
objectType = game.ObjectLookup.get(object);
|
||||||
if (objectType.equals("room")) {
|
if (objectType.equals("room")) {
|
||||||
ZorkRoom tempRoom = Rooms.get(object);
|
ZorkRoom tempRoom = game.Rooms.get(object);
|
||||||
tempRoom.status = newStatus;
|
tempRoom.status = newStatus;
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
} else if (objectType.equals("container")) {
|
} else if (objectType.equals("container")) {
|
||||||
ZorkContainer tempContainer = Containers.get(object);
|
ZorkContainer tempContainer = game.Containers.get(object);
|
||||||
tempContainer.status = newStatus;
|
tempContainer.status = newStatus;
|
||||||
Containers.put(tempContainer.name, tempContainer);
|
game.Containers.put(tempContainer.name, tempContainer);
|
||||||
|
|
||||||
} else if (objectType.equals("creature")) {
|
} else if (objectType.equals("creature")) {
|
||||||
ZorkCreature tempCreature = Creatures.get(object);
|
ZorkCreature tempCreature = game.Creatures.get(object);
|
||||||
tempCreature.status = newStatus;
|
tempCreature.status = newStatus;
|
||||||
Creatures.put(tempCreature.name, tempCreature);
|
game.Creatures.put(tempCreature.name, tempCreature);
|
||||||
|
|
||||||
} else if (objectType.equals("item")) {
|
} else if (objectType.equals("item")) {
|
||||||
ZorkItem tempItem = Items.get(object);
|
ZorkItem tempItem = game.Items.get(object);
|
||||||
tempItem.status = newStatus;
|
tempItem.status = newStatus;
|
||||||
Items.put(tempItem.name, tempItem);
|
game.Items.put(tempItem.name, tempItem);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -905,10 +432,10 @@ public class Zork {
|
||||||
else if (action.startsWith("Add")) {
|
else if (action.startsWith("Add")) {
|
||||||
String destination = action.split(" ")[3];
|
String destination = action.split(" ")[3];
|
||||||
object = action.split(" ")[1];
|
object = action.split(" ")[1];
|
||||||
objectType = ObjectLookup.get(object);
|
objectType = game.ObjectLookup.get(object);
|
||||||
String destinationType = ObjectLookup.get(destination);
|
String destinationType = game.ObjectLookup.get(destination);
|
||||||
if (destinationType.equals("room")) {
|
if (destinationType.equals("room")) {
|
||||||
ZorkRoom tempRoom = Rooms.get(destination);
|
ZorkRoom tempRoom = game.Rooms.get(destination);
|
||||||
if (objectType.equals("item"))
|
if (objectType.equals("item"))
|
||||||
tempRoom.item.put(object, object);
|
tempRoom.item.put(object, object);
|
||||||
else if (objectType.equals("creature"))
|
else if (objectType.equals("creature"))
|
||||||
|
@ -917,14 +444,14 @@ public class Zork {
|
||||||
tempRoom.container.put(object, object);
|
tempRoom.container.put(object, object);
|
||||||
else
|
else
|
||||||
System.out.println("Error");
|
System.out.println("Error");
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
} else if (destinationType.equals("container")) {
|
} else if (destinationType.equals("container")) {
|
||||||
ZorkContainer tempContainer = Containers.get(destination);
|
ZorkContainer tempContainer = game.Containers.get(destination);
|
||||||
if (objectType.equals("item"))
|
if (objectType.equals("item"))
|
||||||
tempContainer.item.put(object, object);
|
tempContainer.item.put(object, object);
|
||||||
else
|
else
|
||||||
System.out.println("Error");
|
System.out.println("Error");
|
||||||
Containers.put(tempContainer.name, tempContainer);
|
game.Containers.put(tempContainer.name, tempContainer);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Error");
|
System.out.println("Error");
|
||||||
}
|
}
|
||||||
|
@ -933,52 +460,52 @@ public class Zork {
|
||||||
/* Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky */
|
/* Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky */
|
||||||
else if (action.startsWith("Delete")) {
|
else if (action.startsWith("Delete")) {
|
||||||
object = action.split(" ")[1];
|
object = action.split(" ")[1];
|
||||||
objectType = ObjectLookup.get(object);
|
objectType = game.ObjectLookup.get(object);
|
||||||
Objects.remove(object);
|
game.Objects.remove(object);
|
||||||
if (objectType.equals("room")) {
|
if (objectType.equals("room")) {
|
||||||
ZorkRoom tempRoom;
|
ZorkRoom tempRoom;
|
||||||
for (String key : Rooms.keySet()) {
|
for (String key : game.Rooms.keySet()) {
|
||||||
tempRoom = Rooms.get(key);
|
tempRoom = game.Rooms.get(key);
|
||||||
for (String key2 : tempRoom.border.keySet()) {
|
for (String key2 : tempRoom.border.keySet()) {
|
||||||
if (tempRoom.border.get(key2).equals(object)) {
|
if (tempRoom.border.get(key2).equals(object)) {
|
||||||
tempRoom.border.remove(key2);
|
tempRoom.border.remove(key2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
}
|
}
|
||||||
} else if (objectType.equals("item")) {
|
} else if (objectType.equals("item")) {
|
||||||
ZorkRoom tempRoom;
|
ZorkRoom tempRoom;
|
||||||
for (String key : Rooms.keySet()) {
|
for (String key : game.Rooms.keySet()) {
|
||||||
tempRoom = Rooms.get(key);
|
tempRoom = game.Rooms.get(key);
|
||||||
if (tempRoom.item.get(object) != null) {
|
if (tempRoom.item.get(object) != null) {
|
||||||
tempRoom.item.remove(object);
|
tempRoom.item.remove(object);
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ZorkContainer tempContainer;
|
ZorkContainer tempContainer;
|
||||||
for (String key : Containers.keySet()) {
|
for (String key : game.Containers.keySet()) {
|
||||||
tempContainer = Containers.get(key);
|
tempContainer = game.Containers.get(key);
|
||||||
if (tempContainer.item.get(object) != null) {
|
if (tempContainer.item.get(object) != null) {
|
||||||
tempContainer.item.remove(object);
|
tempContainer.item.remove(object);
|
||||||
Containers.put(tempContainer.name, tempContainer);
|
game.Containers.put(tempContainer.name, tempContainer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (objectType.equals("container")) {
|
} else if (objectType.equals("container")) {
|
||||||
ZorkRoom tempRoom;
|
ZorkRoom tempRoom;
|
||||||
for (String key : Rooms.keySet()) {
|
for (String key : game.Rooms.keySet()) {
|
||||||
tempRoom = Rooms.get(key);
|
tempRoom = game.Rooms.get(key);
|
||||||
if (tempRoom.container.get(object) != null) {
|
if (tempRoom.container.get(object) != null) {
|
||||||
tempRoom.container.remove(object);
|
tempRoom.container.remove(object);
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (objectType.equals("creature")) {
|
} else if (objectType.equals("creature")) {
|
||||||
ZorkRoom tempRoom;
|
ZorkRoom tempRoom;
|
||||||
for (String key : Rooms.keySet()) {
|
for (String key : game.Rooms.keySet()) {
|
||||||
tempRoom = Rooms.get(key);
|
tempRoom = game.Rooms.get(key);
|
||||||
if (tempRoom.creature.get(object) != null) {
|
if (tempRoom.creature.get(object) != null) {
|
||||||
tempRoom.creature.remove(object);
|
tempRoom.creature.remove(object);
|
||||||
Rooms.put(tempRoom.name, tempRoom);
|
game.Rooms.put(tempRoom.name, tempRoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1007,10 +534,10 @@ public class Zork {
|
||||||
/* Print out the inventory when user types i */
|
/* Print out the inventory when user types i */
|
||||||
public void inventory() {
|
public void inventory() {
|
||||||
String output = "Inventory: ";
|
String output = "Inventory: ";
|
||||||
if (Inventory.isEmpty()) {
|
if (game.Inventory.isEmpty()) {
|
||||||
System.out.println("Inventory: empty");
|
System.out.println("Inventory: empty");
|
||||||
} else {
|
} else {
|
||||||
for (String key : Inventory.keySet()) {
|
for (String key : game.Inventory.keySet()) {
|
||||||
output += key + ", ";
|
output += key + ", ";
|
||||||
}
|
}
|
||||||
output = output.substring(0, output.length() - 2);
|
output = output.substring(0, output.length() - 2);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
/* Special Command condition*/
|
/* Special Command condition*/
|
||||||
class ZorkCommand extends ZorkCondition {
|
public class ZorkCommand extends ZorkCondition {
|
||||||
String command;
|
public String command;
|
||||||
|
|
||||||
public boolean evaluate(Zork zork) {
|
public boolean evaluate(Zork zork) {
|
||||||
return command.equals(zork.userInput);
|
return command.equals(zork.userInput);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
/* Generic condition*/
|
/* Generic condition*/
|
||||||
abstract class ZorkCondition {
|
public abstract class ZorkCondition {
|
||||||
String object;
|
public String object;
|
||||||
|
|
||||||
public abstract boolean evaluate(Zork zork);
|
public abstract boolean evaluate(Zork zork);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
/* Has conditions*/
|
/* Has conditions*/
|
||||||
class ZorkConditionHas extends ZorkCondition {
|
public class ZorkConditionHas extends ZorkCondition {
|
||||||
String has;
|
public String has;
|
||||||
String owner;
|
public String owner;
|
||||||
|
|
||||||
public boolean evaluate(Zork zork) {
|
public boolean evaluate(Zork zork) {
|
||||||
/*Inventory is a special case as it isn't the name of any object in the game, check for it specifically*/
|
/*Inventory is a special case as it isn't the name of any object in the game, check for it specifically*/
|
||||||
if (owner.equals("inventory")) {
|
if (owner.equals("inventory")) {
|
||||||
return zork.Inventory.get(object) != null && has.equals("yes") || zork.Inventory.get(object) == null && has.equals("no");
|
return zork.game.Inventory.get(object) != null && has.equals("yes") || zork.game.Inventory.get(object) == null && has.equals("no");
|
||||||
} else {
|
} else {
|
||||||
/* is it a room?*/
|
/* is it a room?*/
|
||||||
ZorkRoom roomObject = zork.Rooms.get(owner);
|
ZorkRoom roomObject = zork.game.Rooms.get(owner);
|
||||||
if (roomObject != null) {
|
if (roomObject != null) {
|
||||||
return (roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no");
|
return (roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no");
|
||||||
}
|
}
|
||||||
/* is it a container?*/
|
/* is it a container?*/
|
||||||
else {
|
else {
|
||||||
ZorkContainer containerObject = zork.Containers.get(owner);
|
ZorkContainer containerObject = zork.game.Containers.get(owner);
|
||||||
if (containerObject != null) {
|
if (containerObject != null) {
|
||||||
return (containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no");
|
return (containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no");
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
/* Status conditions*/
|
/* Status conditions*/
|
||||||
class ZorkConditionStatus extends ZorkCondition {
|
public class ZorkConditionStatus extends ZorkCondition {
|
||||||
String status;
|
public String status;
|
||||||
|
|
||||||
public boolean evaluate(Zork zork) {
|
public boolean evaluate(Zork zork) {
|
||||||
ZorkObject tested = zork.Objects.get(object);
|
ZorkObject tested = zork.game.Objects.get(object);
|
||||||
return tested != null && tested.status.equals(status);
|
return tested != null && tested.status.equals(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,12 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/* Container*/
|
/* Container*/
|
||||||
class ZorkContainer extends ZorkObject {
|
public class ZorkContainer extends ZorkObject {
|
||||||
public String name;
|
public String name;
|
||||||
public HashMap<String, String> item = new HashMap<>();
|
public HashMap<String, String> item = new HashMap<>();
|
||||||
public String description;
|
public String description;
|
||||||
public ArrayList<String> accept = new ArrayList<>();
|
public ArrayList<String> accept = new ArrayList<>();
|
||||||
boolean isOpen;
|
public boolean isOpen;
|
||||||
|
|
||||||
public ZorkContainer() {
|
public ZorkContainer() {
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/* Creature*/
|
/* Creature*/
|
||||||
class ZorkCreature extends ZorkObject {
|
public class ZorkCreature extends ZorkObject {
|
||||||
public String name;
|
public String name;
|
||||||
public String description;
|
public String description;
|
||||||
public HashMap<String, String> vulnerability = new HashMap<>();
|
public HashMap<String, String> vulnerability = new HashMap<>();
|
||||||
|
|
|
@ -3,7 +3,7 @@ package com.github.dtschust.zork;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/* Item*/
|
/* Item*/
|
||||||
class ZorkItem extends ZorkObject {
|
public class ZorkItem extends ZorkObject {
|
||||||
public String name;
|
public String name;
|
||||||
public String description;
|
public String description;
|
||||||
public String writing;
|
public String writing;
|
||||||
|
|
|
@ -3,7 +3,7 @@ package com.github.dtschust.zork;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/* Generic object, everything inherits from this*/
|
/* Generic object, everything inherits from this*/
|
||||||
class ZorkObject {
|
public class ZorkObject {
|
||||||
public String status;
|
public String status;
|
||||||
public ArrayList<ZorkTrigger> trigger = new ArrayList<>();
|
public ArrayList<ZorkTrigger> trigger = new ArrayList<>();
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ package com.github.dtschust.zork;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/* Room*/
|
/* Room*/
|
||||||
class ZorkRoom extends ZorkObject {
|
public class ZorkRoom extends ZorkObject {
|
||||||
public String name;
|
public String name;
|
||||||
public String type = "regular";
|
public String type = "regular";
|
||||||
public String description;
|
public String description;
|
||||||
|
|
|
@ -3,12 +3,12 @@ package com.github.dtschust.zork;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/*Trigger*/
|
/*Trigger*/
|
||||||
class ZorkTrigger {
|
public class ZorkTrigger {
|
||||||
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
|
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
|
||||||
public ArrayList<String> print = new ArrayList<>();
|
public ArrayList<String> print = new ArrayList<>();
|
||||||
public ArrayList<String> action = new ArrayList<>();
|
public ArrayList<String> action = new ArrayList<>();
|
||||||
String type = "single"; /*By default, single*/
|
public String type = "single"; /*By default, single*/
|
||||||
boolean hasCommand = false;
|
public boolean hasCommand = false;
|
||||||
|
|
||||||
public boolean evaluate(Zork zork) {
|
public boolean evaluate(Zork zork) {
|
||||||
for (ZorkCondition condition : conditions) {
|
for (ZorkCondition condition : conditions) {
|
||||||
|
|
18
src/main/java/com/github/dtschust/zork/parser/ZorkGame.java
Normal file
18
src/main/java/com/github/dtschust/zork/parser/ZorkGame.java
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package com.github.dtschust.zork.parser;
|
||||||
|
|
||||||
|
import com.github.dtschust.zork.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class ZorkGame {
|
||||||
|
public HashMap<String, ZorkRoom> Rooms = new HashMap<>();
|
||||||
|
public HashMap<String, ZorkItem> Items = new HashMap<>();
|
||||||
|
public HashMap<String, ZorkContainer> Containers = new HashMap<>();
|
||||||
|
public HashMap<String, ZorkObject> Objects = new HashMap<>();
|
||||||
|
public HashMap<String, ZorkCreature> Creatures = new HashMap<>();
|
||||||
|
public HashMap<String, String> Inventory = new HashMap<>();
|
||||||
|
public HashMap<String, String> ObjectLookup = new HashMap<>();
|
||||||
|
public String currentRoom;
|
||||||
|
public File file;
|
||||||
|
}
|
497
src/main/java/com/github/dtschust/zork/parser/ZorkReader.java
Normal file
497
src/main/java/com/github/dtschust/zork/parser/ZorkReader.java
Normal file
|
@ -0,0 +1,497 @@
|
||||||
|
package com.github.dtschust.zork.parser;
|
||||||
|
|
||||||
|
import com.github.dtschust.zork.*;
|
||||||
|
import org.w3c.dom.CharacterData;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class ZorkReader {
|
||||||
|
|
||||||
|
private final String filename;
|
||||||
|
|
||||||
|
public ZorkReader(String filename) {
|
||||||
|
this.filename = filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ZorkGame build(){
|
||||||
|
int j, k, l;
|
||||||
|
|
||||||
|
ZorkGame data = new ZorkGame();
|
||||||
|
|
||||||
|
File file = new File(filename);
|
||||||
|
if (!file.canRead()) {
|
||||||
|
System.out.println("Error opening file. Exiting...");
|
||||||
|
throw new RuntimeException();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
/* Open the xml file*/
|
||||||
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
|
Document doc = builder.parse(file);
|
||||||
|
|
||||||
|
Element rootElement = doc.getDocumentElement();
|
||||||
|
|
||||||
|
/* Every single first generation child is a room, container, creature, or item. So load them in*/
|
||||||
|
NodeList nodes = rootElement.getChildNodes();
|
||||||
|
for (k = 0; k < nodes.getLength(); k++) {
|
||||||
|
Node node = nodes.item(k);
|
||||||
|
Element element;
|
||||||
|
if (node instanceof Element) {
|
||||||
|
element = (Element) node;
|
||||||
|
String tagType = element.getTagName();
|
||||||
|
|
||||||
|
/* If it's a room ... */
|
||||||
|
if (tagType.equals("room")) {
|
||||||
|
ZorkRoom tempRoom = new ZorkRoom();
|
||||||
|
|
||||||
|
/*Get all possible Room attributes*/
|
||||||
|
|
||||||
|
NodeList name = element.getElementsByTagName("name");
|
||||||
|
tempRoom.name = getString((Element) name.item(0));
|
||||||
|
NodeList type = element.getElementsByTagName("type");
|
||||||
|
if (type.getLength() > 0) {
|
||||||
|
tempRoom.type = getString((Element) type.item(0));
|
||||||
|
} else {
|
||||||
|
tempRoom.type = "regular";
|
||||||
|
}
|
||||||
|
NodeList status = element.getElementsByTagName("status");
|
||||||
|
if (status.getLength() > 0) {
|
||||||
|
tempRoom.status = getString((Element) type.item(0));
|
||||||
|
} else {
|
||||||
|
tempRoom.status = "";
|
||||||
|
}
|
||||||
|
NodeList description = element.getElementsByTagName("description");
|
||||||
|
tempRoom.description = getString((Element) description.item(0));
|
||||||
|
|
||||||
|
NodeList items = element.getElementsByTagName("item");
|
||||||
|
for (j = 0; j < items.getLength(); j++) {
|
||||||
|
Element item = (Element) items.item(j);
|
||||||
|
String itemName = getString(item);
|
||||||
|
tempRoom.item.put(itemName, itemName);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList creatures = element.getElementsByTagName("creature");
|
||||||
|
for (j = 0; j < creatures.getLength(); j++) {
|
||||||
|
Element creature = (Element) creatures.item(j);
|
||||||
|
String creatureName = getString(creature);
|
||||||
|
tempRoom.creature.put(creatureName, creatureName);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList triggers = element.getElementsByTagName("trigger");
|
||||||
|
for (j = 0; j < triggers.getLength(); j++) {
|
||||||
|
ZorkTrigger tempTrigger = new ZorkTrigger();
|
||||||
|
Element trigger = (Element) triggers.item(j);
|
||||||
|
NodeList commands = trigger.getElementsByTagName("command");
|
||||||
|
for (l = 0; l < commands.getLength(); l++) {
|
||||||
|
Element command = (Element) commands.item(l);
|
||||||
|
ZorkCommand tempCommand = new ZorkCommand();
|
||||||
|
tempCommand.command = getString(command);
|
||||||
|
tempTrigger.conditions.add(tempCommand);
|
||||||
|
tempTrigger.hasCommand = true;
|
||||||
|
}
|
||||||
|
NodeList conditions = trigger.getElementsByTagName("condition");
|
||||||
|
for (l = 0; l < conditions.getLength(); l++) {
|
||||||
|
Element condition = (Element) conditions.item(l);
|
||||||
|
NodeList object = condition.getElementsByTagName("object");
|
||||||
|
NodeList has = condition.getElementsByTagName("has");
|
||||||
|
if (has.getLength() > 0) {
|
||||||
|
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
||||||
|
tempConditionHas.has = getString((Element) has.item(0));
|
||||||
|
tempConditionHas.object = getString((Element) object.item(0));
|
||||||
|
NodeList owner = condition.getElementsByTagName("owner");
|
||||||
|
tempConditionHas.owner = getString((Element) owner.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionHas);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
||||||
|
tempConditionStatus.object = getString((Element) object.item(0));
|
||||||
|
NodeList sstatus = condition.getElementsByTagName("status");
|
||||||
|
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
NodeList ttype = element.getElementsByTagName("type");
|
||||||
|
if (ttype.getLength() > 0) {
|
||||||
|
tempTrigger.type = getString((Element) ttype.item(0));
|
||||||
|
} else {
|
||||||
|
tempTrigger.type = "single";
|
||||||
|
}
|
||||||
|
NodeList prints = trigger.getElementsByTagName("print");
|
||||||
|
for (l = 0; l < prints.getLength(); l++) {
|
||||||
|
Element print = (Element) prints.item(l);
|
||||||
|
tempTrigger.print.add(getString(print));
|
||||||
|
}
|
||||||
|
NodeList actions = trigger.getElementsByTagName("action");
|
||||||
|
for (l = 0; l < actions.getLength(); l++) {
|
||||||
|
Element action = (Element) actions.item(l);
|
||||||
|
tempTrigger.action.add(getString(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
tempRoom.trigger.add(tempTrigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList containers = element.getElementsByTagName("container");
|
||||||
|
for (j = 0; j < containers.getLength(); j++) {
|
||||||
|
Element container = (Element) containers.item(j);
|
||||||
|
String containerName = getString(container);
|
||||||
|
tempRoom.container.put(containerName, containerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList borders = element.getElementsByTagName("border");
|
||||||
|
for (j = 0; j < borders.getLength(); j++) {
|
||||||
|
Element border = (Element) borders.item(j);
|
||||||
|
String borderdirection = getString((Element) border.getElementsByTagName("direction").item(0));
|
||||||
|
String bordername = getString((Element) border.getElementsByTagName("name").item(0));
|
||||||
|
tempRoom.border.put(borderdirection, bordername);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/
|
||||||
|
data.Rooms.put(tempRoom.name, tempRoom);
|
||||||
|
data.Objects.put(tempRoom.name, tempRoom);
|
||||||
|
data.ObjectLookup.put(tempRoom.name, "room");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If it's an item... */
|
||||||
|
else if (tagType.equals("item")) {
|
||||||
|
ZorkItem tempItem = new ZorkItem();
|
||||||
|
|
||||||
|
/* Get all possible item attributes*/
|
||||||
|
NodeList name = element.getElementsByTagName("name");
|
||||||
|
if (name.getLength() > 0)
|
||||||
|
tempItem.name = getString((Element) name.item(0));
|
||||||
|
|
||||||
|
NodeList status = element.getElementsByTagName("status");
|
||||||
|
if (status.getLength() > 0) {
|
||||||
|
tempItem.status = getString((Element) status.item(0));
|
||||||
|
} else {
|
||||||
|
tempItem.status = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList description = element.getElementsByTagName("description");
|
||||||
|
if (description.getLength() > 0)
|
||||||
|
tempItem.description = getString((Element) description.item(0));
|
||||||
|
|
||||||
|
NodeList writing = element.getElementsByTagName("writing");
|
||||||
|
if (writing.getLength() > 0)
|
||||||
|
tempItem.writing = getString((Element) writing.item(0));
|
||||||
|
|
||||||
|
NodeList turnon = element.getElementsByTagName("turnon");
|
||||||
|
if (turnon.getLength() > 0) {
|
||||||
|
NodeList prints = element.getElementsByTagName("print");
|
||||||
|
for (j = 0; j < prints.getLength(); j++) {
|
||||||
|
tempItem.turnOnPrint.add(getString((Element) prints.item(j)));
|
||||||
|
}
|
||||||
|
NodeList actions = element.getElementsByTagName("action");
|
||||||
|
for (j = 0; j < actions.getLength(); j++) {
|
||||||
|
tempItem.turnOnAction.add(getString((Element) actions.item(j)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList triggers = element.getElementsByTagName("trigger");
|
||||||
|
for (j = 0; j < triggers.getLength(); j++) {
|
||||||
|
ZorkTrigger tempTrigger = new ZorkTrigger();
|
||||||
|
Element trigger = (Element) triggers.item(j);
|
||||||
|
NodeList commands = trigger.getElementsByTagName("command");
|
||||||
|
for (l = 0; l < commands.getLength(); l++) {
|
||||||
|
Element command = (Element) commands.item(l);
|
||||||
|
ZorkCommand tempCommand = new ZorkCommand();
|
||||||
|
tempCommand.command = getString(command);
|
||||||
|
tempTrigger.conditions.add(tempCommand);
|
||||||
|
tempTrigger.hasCommand = true;
|
||||||
|
}
|
||||||
|
NodeList conditions = trigger.getElementsByTagName("condition");
|
||||||
|
for (l = 0; l < conditions.getLength(); l++) {
|
||||||
|
Element condition = (Element) conditions.item(l);
|
||||||
|
NodeList object = condition.getElementsByTagName("object");
|
||||||
|
NodeList has = condition.getElementsByTagName("has");
|
||||||
|
if (has.getLength() > 0) {
|
||||||
|
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
||||||
|
tempConditionHas.has = getString((Element) has.item(0));
|
||||||
|
tempConditionHas.object = getString((Element) object.item(0));
|
||||||
|
NodeList owner = condition.getElementsByTagName("owner");
|
||||||
|
tempConditionHas.owner = getString((Element) owner.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionHas);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
||||||
|
tempConditionStatus.object = getString((Element) object.item(0));
|
||||||
|
NodeList sstatus = condition.getElementsByTagName("status");
|
||||||
|
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
NodeList ttype = element.getElementsByTagName("type");
|
||||||
|
if (ttype.getLength() > 0) {
|
||||||
|
tempTrigger.type = getString((Element) ttype.item(0));
|
||||||
|
} else {
|
||||||
|
tempTrigger.type = "single";
|
||||||
|
}
|
||||||
|
NodeList prints = trigger.getElementsByTagName("print");
|
||||||
|
for (l = 0; l < prints.getLength(); l++) {
|
||||||
|
Element print = (Element) prints.item(l);
|
||||||
|
tempTrigger.print.add(getString(print));
|
||||||
|
}
|
||||||
|
NodeList actions = trigger.getElementsByTagName("action");
|
||||||
|
for (l = 0; l < actions.getLength(); l++) {
|
||||||
|
Element action = (Element) actions.item(l);
|
||||||
|
tempTrigger.action.add(getString(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
tempItem.trigger.add(tempTrigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
|
||||||
|
data.Items.put(tempItem.name, tempItem);
|
||||||
|
data.Objects.put(tempItem.name, tempItem);
|
||||||
|
data.ObjectLookup.put(tempItem.name, "item");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If it's a container... */
|
||||||
|
else if (tagType.equals("container")) {
|
||||||
|
ZorkContainer tempCont = new ZorkContainer();
|
||||||
|
|
||||||
|
/*Get all possible container attributes*/
|
||||||
|
|
||||||
|
NodeList name = element.getElementsByTagName("name");
|
||||||
|
if (name.getLength() > 0)
|
||||||
|
tempCont.name = getString((Element) name.item(0));
|
||||||
|
|
||||||
|
NodeList status = element.getElementsByTagName("status");
|
||||||
|
if (status.getLength() > 0)
|
||||||
|
tempCont.status = getString((Element) status.item(0));
|
||||||
|
|
||||||
|
/*Initially assume a closed container*/
|
||||||
|
tempCont.isOpen = false;
|
||||||
|
NodeList description = element.getElementsByTagName("description");
|
||||||
|
if (description.getLength() > 0)
|
||||||
|
tempCont.description = getString((Element) description.item(0));
|
||||||
|
|
||||||
|
NodeList accepts = element.getElementsByTagName("accept");
|
||||||
|
for (j = 0; j < accepts.getLength(); j++) {
|
||||||
|
/* If container has an accepts attribute, then it is always open*/
|
||||||
|
tempCont.isOpen = true;
|
||||||
|
tempCont.accept.add(getString((Element) accepts.item(j)));
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList citems = element.getElementsByTagName("item");
|
||||||
|
for (j = 0; j < citems.getLength(); j++) {
|
||||||
|
Element item = (Element) citems.item(j);
|
||||||
|
String itemName = getString(item);
|
||||||
|
tempCont.item.put(itemName, itemName);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList triggers = element.getElementsByTagName("trigger");
|
||||||
|
for (j = 0; j < triggers.getLength(); j++) {
|
||||||
|
ZorkTrigger tempTrigger = new ZorkTrigger();
|
||||||
|
Element trigger = (Element) triggers.item(j);
|
||||||
|
NodeList commands = trigger.getElementsByTagName("command");
|
||||||
|
for (l = 0; l < commands.getLength(); l++) {
|
||||||
|
Element command = (Element) commands.item(l);
|
||||||
|
ZorkCommand tempCommand = new ZorkCommand();
|
||||||
|
tempCommand.command = getString(command);
|
||||||
|
tempTrigger.conditions.add(tempCommand);
|
||||||
|
tempTrigger.hasCommand = true;
|
||||||
|
}
|
||||||
|
NodeList conditions = trigger.getElementsByTagName("condition");
|
||||||
|
for (l = 0; l < conditions.getLength(); l++) {
|
||||||
|
Element condition = (Element) conditions.item(l);
|
||||||
|
NodeList object = condition.getElementsByTagName("object");
|
||||||
|
NodeList has = condition.getElementsByTagName("has");
|
||||||
|
if (has.getLength() > 0) {
|
||||||
|
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
||||||
|
tempConditionHas.has = getString((Element) has.item(0));
|
||||||
|
tempConditionHas.object = getString((Element) object.item(0));
|
||||||
|
NodeList owner = condition.getElementsByTagName("owner");
|
||||||
|
tempConditionHas.owner = getString((Element) owner.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionHas);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
||||||
|
tempConditionStatus.object = getString((Element) object.item(0));
|
||||||
|
NodeList sstatus = condition.getElementsByTagName("status");
|
||||||
|
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
NodeList ttype = element.getElementsByTagName("type");
|
||||||
|
if (ttype.getLength() > 0) {
|
||||||
|
tempTrigger.type = getString((Element) ttype.item(0));
|
||||||
|
} else {
|
||||||
|
tempTrigger.type = "single";
|
||||||
|
}
|
||||||
|
NodeList prints = trigger.getElementsByTagName("print");
|
||||||
|
for (l = 0; l < prints.getLength(); l++) {
|
||||||
|
Element print = (Element) prints.item(l);
|
||||||
|
tempTrigger.print.add(getString(print));
|
||||||
|
}
|
||||||
|
NodeList actions = trigger.getElementsByTagName("action");
|
||||||
|
for (l = 0; l < actions.getLength(); l++) {
|
||||||
|
Element action = (Element) actions.item(l);
|
||||||
|
tempTrigger.action.add(getString(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
tempCont.trigger.add(tempTrigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||||
|
data.Containers.put(tempCont.name, tempCont);
|
||||||
|
data.Objects.put(tempCont.name, tempCont);
|
||||||
|
data.ObjectLookup.put(tempCont.name, "container");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* And finally, if it's a creature...*/
|
||||||
|
else if (tagType.equals("creature")) {
|
||||||
|
ZorkCreature tempCreature = new ZorkCreature();
|
||||||
|
|
||||||
|
/* Get all possible creature attributes*/
|
||||||
|
|
||||||
|
NodeList name = element.getElementsByTagName("name");
|
||||||
|
if (name.getLength() > 0)
|
||||||
|
tempCreature.name = getString((Element) name.item(0));
|
||||||
|
|
||||||
|
NodeList status = element.getElementsByTagName("status");
|
||||||
|
if (status.getLength() > 0)
|
||||||
|
tempCreature.status = getString((Element) status.item(0));
|
||||||
|
|
||||||
|
NodeList description = element.getElementsByTagName("description");
|
||||||
|
if (description.getLength() > 0)
|
||||||
|
tempCreature.description = getString((Element) description.item(0));
|
||||||
|
|
||||||
|
NodeList vulns = element.getElementsByTagName("vulnerability");
|
||||||
|
for (j = 0; j < vulns.getLength(); j++) {
|
||||||
|
String vulnString = getString((Element) vulns.item(j));
|
||||||
|
tempCreature.vulnerability.put(vulnString, vulnString);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList attacks = element.getElementsByTagName("attack");
|
||||||
|
for (j = 0; j < attacks.getLength(); j++) {
|
||||||
|
Element attack = (Element) attacks.item(j);
|
||||||
|
NodeList conditions = attack.getElementsByTagName("condition");
|
||||||
|
for (l = 0; l < conditions.getLength(); l++) {
|
||||||
|
Element condition = (Element) conditions.item(l);
|
||||||
|
NodeList object = condition.getElementsByTagName("object");
|
||||||
|
NodeList has = condition.getElementsByTagName("has");
|
||||||
|
if (has.getLength() > 0) {
|
||||||
|
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
||||||
|
tempConditionHas.has = getString((Element) has.item(0));
|
||||||
|
tempConditionHas.object = getString((Element) object.item(0));
|
||||||
|
NodeList owner = condition.getElementsByTagName("owner");
|
||||||
|
tempConditionHas.owner = getString((Element) owner.item(0));
|
||||||
|
tempCreature.conditions.add(tempConditionHas);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
||||||
|
tempConditionStatus.object = getString((Element) object.item(0));
|
||||||
|
NodeList sstatus = condition.getElementsByTagName("status");
|
||||||
|
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
||||||
|
tempCreature.conditions.add(tempConditionStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
NodeList prints = attack.getElementsByTagName("print");
|
||||||
|
for (l = 0; l < prints.getLength(); l++) {
|
||||||
|
Element print = (Element) prints.item(l);
|
||||||
|
tempCreature.print.add(getString(print));
|
||||||
|
}
|
||||||
|
NodeList actions = attack.getElementsByTagName("action");
|
||||||
|
for (l = 0; l < actions.getLength(); l++) {
|
||||||
|
Element action = (Element) actions.item(l);
|
||||||
|
tempCreature.action.add(getString(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList triggers = element.getElementsByTagName("trigger");
|
||||||
|
for (j = 0; j < triggers.getLength(); j++) {
|
||||||
|
ZorkTrigger tempTrigger = new ZorkTrigger();
|
||||||
|
Element trigger = (Element) triggers.item(j);
|
||||||
|
NodeList commands = trigger.getElementsByTagName("command");
|
||||||
|
for (l = 0; l < commands.getLength(); l++) {
|
||||||
|
Element command = (Element) commands.item(l);
|
||||||
|
ZorkCommand tempCommand = new ZorkCommand();
|
||||||
|
tempCommand.command = getString(command);
|
||||||
|
tempTrigger.conditions.add(tempCommand);
|
||||||
|
tempTrigger.hasCommand = true;
|
||||||
|
}
|
||||||
|
NodeList conditions = trigger.getElementsByTagName("condition");
|
||||||
|
for (l = 0; l < conditions.getLength(); l++) {
|
||||||
|
Element condition = (Element) conditions.item(l);
|
||||||
|
NodeList object = condition.getElementsByTagName("object");
|
||||||
|
NodeList has = condition.getElementsByTagName("has");
|
||||||
|
if (has.getLength() > 0) {
|
||||||
|
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
|
||||||
|
tempConditionHas.has = getString((Element) has.item(0));
|
||||||
|
tempConditionHas.object = getString((Element) object.item(0));
|
||||||
|
NodeList owner = condition.getElementsByTagName("owner");
|
||||||
|
tempConditionHas.owner = getString((Element) owner.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionHas);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
|
||||||
|
tempConditionStatus.object = getString((Element) object.item(0));
|
||||||
|
NodeList sstatus = condition.getElementsByTagName("status");
|
||||||
|
tempConditionStatus.status = getString((Element) sstatus.item(0));
|
||||||
|
tempTrigger.conditions.add(tempConditionStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
NodeList ttype = element.getElementsByTagName("type");
|
||||||
|
if (ttype.getLength() > 0) {
|
||||||
|
tempTrigger.type = getString((Element) ttype.item(0));
|
||||||
|
} else {
|
||||||
|
tempTrigger.type = "single";
|
||||||
|
}
|
||||||
|
NodeList prints = trigger.getElementsByTagName("print");
|
||||||
|
for (l = 0; l < prints.getLength(); l++) {
|
||||||
|
Element print = (Element) prints.item(l);
|
||||||
|
tempTrigger.print.add(getString(print));
|
||||||
|
}
|
||||||
|
NodeList actions = trigger.getElementsByTagName("action");
|
||||||
|
for (l = 0; l < actions.getLength(); l++) {
|
||||||
|
Element action = (Element) actions.item(l);
|
||||||
|
tempTrigger.action.add(getString(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
tempCreature.trigger.add(tempTrigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||||
|
data.Creatures.put(tempCreature.name, tempCreature);
|
||||||
|
data.Objects.put(tempCreature.name, tempCreature);
|
||||||
|
data.ObjectLookup.put(tempCreature.name, "creature");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Invalid XML file, exiting");
|
||||||
|
System.exit(-1);
|
||||||
|
//e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Get a string from an element (XML parsing stuff)*/
|
||||||
|
public static String getString(Element e) {
|
||||||
|
Node child = e.getFirstChild();
|
||||||
|
if (child instanceof CharacterData) {
|
||||||
|
CharacterData cd = (CharacterData) child;
|
||||||
|
return cd.getData();
|
||||||
|
}
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in a new issue