ZorkReader refactored

This commit is contained in:
RaffaeleMorganti 2022-11-19 10:46:28 +01:00
parent 4a7eca5302
commit e6b96d69d1
11 changed files with 233 additions and 260 deletions

View File

@ -6,6 +6,7 @@
package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ZorkGame;
import com.github.dtschust.zork.types.*;
import com.github.dtschust.zork.parser.ZorkReader;
import java.util.Map;
@ -165,7 +166,6 @@ public class Zork {
private void doActionDelete(String object) {
String objectType;
objectType = game.ObjectLookup.get(object);
game.Objects.remove(object);
if (objectType.equals("room")) {
for (String key : game.Rooms.keySet()) {
ZorkRoom tempRoom = game.Rooms.get(key);
@ -211,28 +211,10 @@ public class Zork {
}
private void doActionUpdate(String object, String newStatus) {
String objectType;
objectType = game.ObjectLookup.get(object);
if (objectType.equals("room")) {
ZorkRoom tempRoom = game.Rooms.get(object);
tempRoom.status = newStatus;
game.Rooms.put(tempRoom);
} else if (objectType.equals("container")) {
ZorkContainer tempContainer = game.Containers.get(object);
tempContainer.status = newStatus;
game.Containers.put(tempContainer);
} else if (objectType.equals("creature")) {
ZorkCreature tempCreature = game.Creatures.get(object);
tempCreature.status = newStatus;
game.Creatures.put(tempCreature);
} else if (objectType.equals("item")) {
ZorkItem tempItem = game.Items.get(object);
tempItem.status = newStatus;
game.Items.put(tempItem);
}
ZorkMap<ZorkObject> collection = (ZorkMap<ZorkObject>) game.getListThroughLookup(object);
ZorkObject tempObject = collection.get(object);
tempObject.status = newStatus;
collection.put(tempObject);
}
private void doActionAttack(String tempString, String weapon) {

View File

@ -1,5 +1,8 @@
package com.github.dtschust.zork;
import com.github.dtschust.zork.types.ZorkContainer;
import com.github.dtschust.zork.types.ZorkRoom;
import java.util.Set;
/* Has conditions*/

View File

@ -1,5 +1,7 @@
package com.github.dtschust.zork;
import com.github.dtschust.zork.types.ZorkObject;
/* Status conditions*/
public class ZorkConditionStatus extends ZorkCondition {
private final String status;
@ -11,7 +13,7 @@ public class ZorkConditionStatus extends ZorkCondition {
@Override
public boolean evaluate(Zork zork) {
ZorkObject tested = zork.game.Objects.get(object);
ZorkObject tested = zork.game.getListThroughLookup(object).get(object);
return tested != null && tested.status.equals(status);
}
}

View File

@ -1,6 +1,6 @@
package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.*;
import com.github.dtschust.zork.types.*;
import java.util.HashMap;
import java.util.HashSet;
@ -10,9 +10,39 @@ public class ZorkGame {
public ZorkMap<ZorkRoom> Rooms = new ZorkMap<>();
public ZorkMap<ZorkItem> Items = new ZorkMap<>();
public ZorkMap<ZorkContainer> Containers = new ZorkMap<>();
public ZorkMap<ZorkObject> Objects = new ZorkMap<>();
public ZorkMap<ZorkCreature> Creatures = new ZorkMap<>();
public Set<String> Inventory = new HashSet<>();
public HashMap<String, String> ObjectLookup = new HashMap<>();
public void addObjectThroughLookup(String name, ZorkObject object){
switch (name) {
case "room":
Rooms.put((ZorkRoom) object);
break;
case "container":
Containers.put((ZorkContainer) object);
break;
case "creature":
Creatures.put((ZorkCreature) object);
break;
case "item":
Items.put((ZorkItem) object);
break;
}
ObjectLookup.put(object.name, name);
}
public ZorkMap<? extends ZorkObject> getListThroughLookup(String name){
switch (ObjectLookup.get(name)) {
case "room":
return Rooms;
case "container":
return Containers;
case "creature":
return Creatures;
case "item":
return Items;
}
return null;
}
}

View File

@ -1,13 +1,16 @@
package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.*;
import com.github.dtschust.zork.types.*;
import org.w3c.dom.CharacterData;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class ZorkReader {
@ -28,7 +31,6 @@ public class ZorkReader {
}
public ZorkGame build() {
int j, k, l;
ZorkGame data = new ZorkGame();
@ -46,209 +48,29 @@ public class ZorkReader {
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);
for (Node node : iterNodes(rootElement.getChildNodes())) {
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.add(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.add(creatureName);
}
readTriggersInObject(element, tempRoom);
NodeList containers = element.getElementsByTagName("container");
for (j = 0; j < containers.getLength(); j++) {
Element container = (Element) containers.item(j);
String containerName = getString(container);
tempRoom.container.add(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);
data.Objects.put(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)));
}
}
readTriggersInObject(element, tempItem);
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
data.Items.put(tempItem);
data.Objects.put(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.add(itemName);
}
readTriggersInObject(element, tempCont);
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
data.Containers.put(tempCont);
data.Objects.put(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.add(vulnString);
}
NodeList attacks = element.getElementsByTagName("attack");
for (j = 0; j < attacks.getLength(); j++) {
Element attack = (Element) attacks.item(j);
readConditionsInTrigger(attack, tempCreature.conditions);
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));
}
}
readTriggersInObject(element, tempCreature);
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
data.Creatures.put(tempCreature);
data.Objects.put(tempCreature);
data.ObjectLookup.put(tempCreature.name, "creature");
switch (tagType) {
/* If it's a room ... */
case "room":
addRoom(data, element);
break;
/* If it's an item... */
case "item":
addItem(data, element);
break;
/* If it's a container... */
case "container":
addContainer(data, element);
break;
/* And finally, if it's a creature...*/
case "creature":
addCreature(data, element);
break;
}
}
@ -262,16 +84,137 @@ public class ZorkReader {
return data;
}
private static void addCreature(ZorkGame data, Element element) {
/* Get all possible creature attributes*/
ZorkCreature tempCreature = (ZorkCreature) readZorkObjectAttributes(new ZorkCreature(), element);
for (Element vuln : iterElements(element.getElementsByTagName("vulnerability"))) {
tempCreature.vulnerability.add(getString(vuln));
}
NodeList attacks = element.getElementsByTagName("attack");
for (Element attack : iterElements(attacks)) {
readConditionsInTrigger(attack, tempCreature.conditions);
for (Element print : iterElements(attack.getElementsByTagName("print"))) {
tempCreature.print.add(getString(print));
}
for (Element action : iterElements(attack.getElementsByTagName("action"))) {
tempCreature.action.add(getString(action));
}
}
readTriggersInObject(element, tempCreature);
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
data.addObjectThroughLookup("creature", tempCreature);
}
private static void addContainer(ZorkGame data, Element element) {
/*Get all possible container attributes*/
ZorkContainer tempCont = (ZorkContainer) readZorkObjectAttributes(new ZorkContainer(), element);
/*Initially assume a closed container*/
tempCont.isOpen = false;
for (Element accept : iterElements(element.getElementsByTagName("accept"))) {
/* If container has an accepts attribute, then it is always open*/
tempCont.isOpen = true;
tempCont.accept.add(getString(accept));
}
for (Element item : iterElements(element.getElementsByTagName("item"))) {
String itemName = getString(item);
tempCont.item.add(itemName);
}
readTriggersInObject(element, tempCont);
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
data.addObjectThroughLookup("container", tempCont);
}
private static void addItem(ZorkGame data, Element element) {
/* Get all possible item attributes*/
ZorkItem tempItem = (ZorkItem) readZorkObjectAttributes(new ZorkItem(), element);
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) {
for (Element print : iterElements(element.getElementsByTagName("print"))) {
tempItem.turnOnPrint.add(getString(print));
}
for (Element action : iterElements(element.getElementsByTagName("action"))) {
tempItem.turnOnAction.add(getString(action));
}
}
readTriggersInObject(element, tempItem);
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
data.addObjectThroughLookup("item", tempItem);
}
private static void addRoom(ZorkGame data, Element element) {
/*Get all possible Room attributes*/
ZorkRoom tempRoom = (ZorkRoom) readZorkObjectAttributes(new ZorkRoom(), element);
NodeList type = element.getElementsByTagName("type");
if (type.getLength() > 0) {
tempRoom.type = getString((Element) type.item(0));
} else {
tempRoom.type = "regular";
}
for (Element item : iterElements(element.getElementsByTagName("item"))) {
String itemName = getString(item);
tempRoom.item.add(itemName);
}
for (Element creature : iterElements(element.getElementsByTagName("creature"))) {
String creatureName = getString(creature);
tempRoom.creature.add(creatureName);
}
readTriggersInObject(element, tempRoom);
for (Element container : iterElements(element.getElementsByTagName("container"))) {
String containerName = getString(container);
tempRoom.container.add(containerName);
}
for (Element border : iterElements(element.getElementsByTagName("border"))) {
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.addObjectThroughLookup("room", tempRoom);
}
private static ZorkObject readZorkObjectAttributes(ZorkObject tempObject, Element element) {
NodeList name = element.getElementsByTagName("name");
tempObject.name = getString((Element) name.item(0));
NodeList status = element.getElementsByTagName("status");
tempObject.status = (status.getLength() > 0) ? getString((Element) status.item(0)) : "";
NodeList description = element.getElementsByTagName("description");
tempObject.description = (description.getLength() > 0) ? getString((Element) description.item(0)) : "";
return tempObject;
}
private static void readTriggersInObject(Element element, ZorkObject tempRoom) {
int j;
int l;
NodeList triggers = element.getElementsByTagName("trigger");
for (j = 0; j < triggers.getLength(); j++) {
for (Element trigger : iterElements(element.getElementsByTagName("trigger"))) {
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);
for (Element command : iterElements(trigger.getElementsByTagName("command"))) {
ZorkCommand tempCommand = new ZorkCommand(getString(command));
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
@ -285,14 +228,10 @@ public class ZorkReader {
} else {
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l = 0; l < prints.getLength(); l++) {
Element print = (Element) prints.item(l);
for (Element print : iterElements(trigger.getElementsByTagName("print"))) {
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l = 0; l < actions.getLength(); l++) {
Element action = (Element) actions.item(l);
for (Element action : iterElements(trigger.getElementsByTagName("action"))) {
tempTrigger.action.add(getString(action));
}
@ -301,10 +240,7 @@ public class ZorkReader {
}
private static void readConditionsInTrigger(Element trigger, List<? super ZorkCondition> conditionsList) {
int l;
NodeList conditions = trigger.getElementsByTagName("condition");
for (l = 0; l < conditions.getLength(); l++) {
Element condition = (Element) conditions.item(l);
for (Element condition : iterElements(trigger.getElementsByTagName("condition"))) {
NodeList object = condition.getElementsByTagName("object");
NodeList has = condition.getElementsByTagName("has");
if (has.getLength() > 0) {
@ -326,4 +262,27 @@ public class ZorkReader {
}
}
private static <T> Iterable<T> iterable(final NodeList nodeList) {
return () -> new Iterator<>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < nodeList.getLength();
}
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
return (T) nodeList.item(index++);
}
};
}
private static Iterable<Node> iterNodes(final NodeList nodeList) {
return iterable(nodeList);
}
private static Iterable<Element> iterElements(final NodeList nodeList) {
return iterable(nodeList);
}
}

View File

@ -1,14 +1,12 @@
package com.github.dtschust.zork;
package com.github.dtschust.zork.types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/* Container*/
public class ZorkContainer extends ZorkObject {
public Set<String> item = new HashSet<String>();
public String description;
public ArrayList<String> accept = new ArrayList<>();
public boolean isOpen;

View File

@ -1,13 +1,14 @@
package com.github.dtschust.zork;
package com.github.dtschust.zork.types;
import com.github.dtschust.zork.Zork;
import com.github.dtschust.zork.ZorkCondition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/* Creature*/
public class ZorkCreature extends ZorkObject {
public String description;
public Set<String> vulnerability = new HashSet<>();
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
public ArrayList<String> print = new ArrayList<>();

View File

@ -1,10 +1,9 @@
package com.github.dtschust.zork;
package com.github.dtschust.zork.types;
import java.util.ArrayList;
/* Item*/
public class ZorkItem extends ZorkObject {
public String description;
public String writing;
public ArrayList<String> turnOnPrint = new ArrayList<>();
public ArrayList<String> turnOnAction = new ArrayList<>();

View File

@ -1,6 +1,4 @@
package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.ZorkObject;
package com.github.dtschust.zork.types;
import java.util.HashMap;

View File

@ -1,12 +1,14 @@
package com.github.dtschust.zork;
package com.github.dtschust.zork.types;
import com.github.dtschust.zork.ZorkTrigger;
import java.util.ArrayList;
/* Generic object, everything inherits from this*/
public class ZorkObject {
public String status;
public abstract class ZorkObject {
public String name;
public String status;
public String description;
public ArrayList<ZorkTrigger> trigger = new ArrayList<>();
public ZorkObject() {

View File

@ -1,4 +1,4 @@
package com.github.dtschust.zork;
package com.github.dtschust.zork.types;
import java.util.HashMap;
import java.util.HashSet;
@ -7,7 +7,6 @@ import java.util.Set;
/* Room*/
public class ZorkRoom extends ZorkObject {
public String type = "regular";
public String description;
public HashMap<String, String> border = new HashMap<>();
public Set<String> container = new HashSet<>();
public Set<String> item = new HashSet<>();