ZorkConditions are immutable objects
This commit is contained in:
parent
7cc2352b90
commit
8e54d0684b
8 changed files with 85 additions and 68 deletions
|
@ -61,7 +61,6 @@ public class Zork {
|
|||
}
|
||||
|
||||
|
||||
|
||||
/* I love how basic java main functions are sometimes.*/
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 1) {
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
/* Special Command condition */
|
||||
public class ZorkCommand extends ZorkCondition {
|
||||
public class ZorkCommand implements ZorkEvaluatable {
|
||||
public final String command;
|
||||
|
||||
public ZorkCommand(String command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean evaluate(Zork zork) {
|
||||
return command.equals(zork.userInput);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
/* Generic condition*/
|
||||
public abstract class ZorkCondition {
|
||||
public String object;
|
||||
public abstract class ZorkCondition implements ZorkEvaluatable {
|
||||
public final String object;
|
||||
|
||||
public abstract boolean evaluate(Zork zork);
|
||||
protected ZorkCondition(String object) {
|
||||
this.object = object;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,16 @@ package com.github.dtschust.zork;
|
|||
|
||||
/* Has conditions*/
|
||||
public class ZorkConditionHas extends ZorkCondition {
|
||||
public String has;
|
||||
public String owner;
|
||||
private final String has;
|
||||
private final String owner;
|
||||
|
||||
public ZorkConditionHas(String has, String object, String owner) {
|
||||
super(object);
|
||||
this.has = has;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
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*/
|
||||
if (owner.equals("inventory")) {
|
||||
|
|
|
@ -2,8 +2,14 @@ package com.github.dtschust.zork;
|
|||
|
||||
/* Status conditions*/
|
||||
public class ZorkConditionStatus extends ZorkCondition {
|
||||
public String status;
|
||||
private final String status;
|
||||
|
||||
public ZorkConditionStatus(String status, String object) {
|
||||
super(object);
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean evaluate(Zork zork) {
|
||||
ZorkObject tested = zork.game.Objects.get(object);
|
||||
return tested != null && tested.status.equals(status);
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
public interface ZorkEvaluatable {
|
||||
boolean evaluate(Zork zork);
|
||||
}
|
|
@ -4,14 +4,14 @@ import java.util.ArrayList;
|
|||
|
||||
/*Trigger*/
|
||||
public class ZorkTrigger {
|
||||
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
|
||||
public ArrayList<ZorkEvaluatable> conditions = new ArrayList<>();
|
||||
public ArrayList<String> print = new ArrayList<>();
|
||||
public ArrayList<String> action = new ArrayList<>();
|
||||
public String type = "single"; /*By default, single*/
|
||||
public boolean hasCommand = false;
|
||||
|
||||
public boolean evaluate(Zork zork) {
|
||||
for (ZorkCondition condition : conditions) {
|
||||
for (ZorkEvaluatable condition : conditions) {
|
||||
if (!condition.evaluate(zork)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -2,10 +2,7 @@ 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 org.w3c.dom.*;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
@ -19,6 +16,16 @@ public class ZorkReader {
|
|||
this.filename = filename;
|
||||
}
|
||||
|
||||
/* 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 "?";
|
||||
}
|
||||
|
||||
public ZorkGame build() {
|
||||
int j, k, l;
|
||||
|
||||
|
@ -100,18 +107,19 @@ public class ZorkReader {
|
|||
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));
|
||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas(
|
||||
getString((Element) has.item(0)),
|
||||
getString((Element) object.item(0)),
|
||||
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));
|
||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
|
||||
getString((Element) sstatus.item(0)),
|
||||
getString((Element) object.item(0))
|
||||
);
|
||||
tempTrigger.conditions.add(tempConditionStatus);
|
||||
}
|
||||
|
||||
|
@ -211,18 +219,18 @@ public class ZorkReader {
|
|||
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));
|
||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas(
|
||||
getString((Element) has.item(0)),
|
||||
getString((Element) object.item(0)),
|
||||
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));
|
||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
|
||||
getString((Element) sstatus.item(0)),
|
||||
getString((Element) object.item(0))
|
||||
);
|
||||
tempTrigger.conditions.add(tempConditionStatus);
|
||||
}
|
||||
|
||||
|
@ -305,18 +313,19 @@ public class ZorkReader {
|
|||
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));
|
||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas(
|
||||
getString((Element) has.item(0)),
|
||||
getString((Element) object.item(0)),
|
||||
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));
|
||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
|
||||
getString((Element) sstatus.item(0)),
|
||||
getString((Element) object.item(0))
|
||||
);
|
||||
tempTrigger.conditions.add(tempConditionStatus);
|
||||
}
|
||||
|
||||
|
@ -380,18 +389,18 @@ public class ZorkReader {
|
|||
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));
|
||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas(
|
||||
getString((Element) has.item(0)),
|
||||
getString((Element) object.item(0)),
|
||||
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));
|
||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
|
||||
getString((Element) sstatus.item(0)),
|
||||
getString((Element) object.item(0))
|
||||
);
|
||||
tempCreature.conditions.add(tempConditionStatus);
|
||||
}
|
||||
|
||||
|
@ -406,7 +415,6 @@ public class ZorkReader {
|
|||
Element action = (Element) actions.item(l);
|
||||
tempCreature.action.add(getString(action));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
NodeList triggers = element.getElementsByTagName("trigger");
|
||||
|
@ -426,18 +434,18 @@ public class ZorkReader {
|
|||
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));
|
||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas(
|
||||
getString((Element) has.item(0)),
|
||||
getString((Element) object.item(0)),
|
||||
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));
|
||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
|
||||
getString((Element) sstatus.item(0)),
|
||||
getString((Element) object.item(0))
|
||||
);
|
||||
tempTrigger.conditions.add(tempConditionStatus);
|
||||
}
|
||||
|
||||
|
@ -479,15 +487,4 @@ public class ZorkReader {
|
|||
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