From 37f6bc2ff9801a345c1486c760b9c8213ce45624 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 22 Nov 2022 10:12:04 +0100 Subject: [PATCH] Parser strategies for all entities --- .../java/com/github/dtschust/zork/Zork.java | 8 +- .../dtschust/zork/ZorkConditionHas.java | 3 +- .../com/github/dtschust/zork/ZorkTrigger.java | 12 +- .../github/dtschust/zork/parser/DOMUtils.java | 4 +- .../dtschust/zork/parser/ZorkReader.java | 116 +----------------- .../zork/parser/builders/Parsers.java | 11 +- .../builders/ZorkCreatureParseStrategy.java | 52 ++++++++ .../builders/ZorkItemParseStrategy.java | 45 +++++++ .../builders/ZorkRoomParseStrategy.java | 46 +++++++ .../dtschust/zork/parser/dom/Elements.java | 8 +- .../dtschust/zork/repl/actions/AddAction.java | 2 +- .../zork/repl/actions/DeleteAction.java | 22 ++-- .../zork/repl/actions/TakeAction.java | 3 +- .../dtschust/zork/types/ZorkCreature.java | 5 +- .../github/dtschust/zork/types/ZorkItem.java | 11 +- .../github/dtschust/zork/types/ZorkRoom.java | 31 +++-- 16 files changed, 219 insertions(+), 160 deletions(-) create mode 100644 src/main/java/com/github/dtschust/zork/parser/builders/ZorkCreatureParseStrategy.java create mode 100644 src/main/java/com/github/dtschust/zork/parser/builders/ZorkItemParseStrategy.java create mode 100644 src/main/java/com/github/dtschust/zork/parser/builders/ZorkRoomParseStrategy.java diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index f4ce3ed..18e2d7f 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -17,11 +17,8 @@ import static com.github.dtschust.zork.Zork.Type.*; /* And away we go*/ public class Zork { - public enum Type {ROOM, ITEM, CONTAINER, CREATURE} - ZorkGame game; Scanner source = new Scanner(System.in); - public Zork(String filename) { game = new ZorkReader(filename).build(); @@ -50,7 +47,6 @@ public class Zork { System.exit(0); } - /* I love how basic java main functions are sometimes.*/ public static void main(String[] args) { if (args.length != 1) { @@ -129,7 +125,6 @@ public class Zork { return doZorkTriggers(game.getCurrentRoom(), input); } - private boolean doZorkTriggers(ZorkObject zorkObject, String input) { boolean skip = false; Iterator iterator = zorkObject.trigger.iterator(); @@ -152,4 +147,7 @@ public class Zork { return skip; } + + public enum Type {ROOM, ITEM, CONTAINER, CREATURE} + } diff --git a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java index c9ffdf2..bdeee19 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java +++ b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java @@ -3,7 +3,8 @@ package com.github.dtschust.zork; import com.github.dtschust.zork.types.ZorkContainer; import com.github.dtschust.zork.types.ZorkRoom; -import static com.github.dtschust.zork.Zork.Type.*; +import static com.github.dtschust.zork.Zork.Type.CONTAINER; +import static com.github.dtschust.zork.Zork.Type.ROOM; /* Has conditions*/ diff --git a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java index 436667b..a9b89fb 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java +++ b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java @@ -4,16 +4,12 @@ import java.util.List; /*Trigger*/ public class ZorkTrigger { - private final List conditions; - private final List commands; public final List print; public final List action; /* By default, "single" */ public final String type; - - public boolean hasCommand() { - return !this.commands.isEmpty(); - } + private final List conditions; + private final List commands; public ZorkTrigger(final String type, final List conditions, @@ -27,6 +23,10 @@ public class ZorkTrigger { this.type = type; } + public boolean hasCommand() { + return !this.commands.isEmpty(); + } + public boolean evaluate(ZorkGame game, String input) { if (!commands.stream().allMatch(c -> c.matchesInput(input))) { return false; diff --git a/src/main/java/com/github/dtschust/zork/parser/DOMUtils.java b/src/main/java/com/github/dtschust/zork/parser/DOMUtils.java index 8e52145..9c51543 100644 --- a/src/main/java/com/github/dtschust/zork/parser/DOMUtils.java +++ b/src/main/java/com/github/dtschust/zork/parser/DOMUtils.java @@ -5,7 +5,9 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import java.util.*; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Optional; public final class DOMUtils { private DOMUtils() { diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java b/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java index 803aa9a..4579058 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java @@ -1,25 +1,16 @@ package com.github.dtschust.zork.parser; -import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkGame; -import com.github.dtschust.zork.ZorkTrigger; import com.github.dtschust.zork.parser.builders.Parsers; -import com.github.dtschust.zork.parser.dom.Elements; import com.github.dtschust.zork.types.ZorkContainer; import com.github.dtschust.zork.types.ZorkCreature; import com.github.dtschust.zork.types.ZorkItem; import com.github.dtschust.zork.types.ZorkRoom; import org.w3c.dom.Element; import org.w3c.dom.Node; -import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; import java.nio.channels.NonReadableChannelException; import static com.github.dtschust.zork.Zork.Type.*; @@ -33,49 +24,9 @@ public class ZorkReader { } private static void addCreature(ZorkGame data, Element element) { - final Set vulnerabilities = new HashSet<>(); - final List conditions = new ArrayList<>(); - final List prints = new ArrayList<>(); - final List actions = new ArrayList<>(); + final ZorkCreature tempCreature = Parsers.creature.parse(element); - - NodeList attacks = element.getElementsByTagName("attack"); - for (Element attack : DOMUtils.iterator(attacks)) { - final List conditionsList = Elements.byTagName(attack, "condition").stream() - .map(Parsers.condition::parse) - .collect(Collectors.toList()); - conditions.addAll(conditionsList); - - for (Element print : DOMUtils.iterator(attack.getElementsByTagName("print"))) { - prints.add(DOMUtils.getInnerText(print)); - } - for (Element action : DOMUtils.iterator(attack.getElementsByTagName("action"))) { - actions.add(DOMUtils.getInnerText(action)); - } - } - - for (Element vuln : DOMUtils.iterator(element.getElementsByTagName("vulnerability"))) { - vulnerabilities.add(DOMUtils.getInnerText(vuln)); - } - - /* Get all possible creature attributes */ - ZorkCreature tempCreature = new ZorkCreature( - DOMUtils.getInnerTextByTagName(element, "name", ""), - DOMUtils.getInnerTextByTagName(element, "description", ""), - vulnerabilities, - conditions, - prints, - actions - ); - - final List triggers = Elements.byTagName(element, "trigger").stream() - .map(Parsers.trigger::parse) - .collect(Collectors.toList()); - tempCreature.trigger.addAll(triggers); - - tempCreature.updateStatus(DOMUtils.getInnerTextByTagName(element, "status", "")); - - /* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/ + /* Put each creature in the creatures hashmap, the generic object hashmap, and the object lookup hashmap*/ data.addObjectThroughLookup(CREATURE, tempCreature); } @@ -87,74 +38,15 @@ public class ZorkReader { } private static void addItem(ZorkGame data, Element element) { - final List prints = new ArrayList<>(); - final List actions = new ArrayList<>(); - - NodeList turnOn = element.getElementsByTagName("turnon"); - if (turnOn.getLength() > 0) { - for (Element print : DOMUtils.iterator(element.getElementsByTagName("print"))) { - prints.add(DOMUtils.getInnerText(print)); - } - for (Element action : DOMUtils.iterator(element.getElementsByTagName("action"))) { - actions.add(DOMUtils.getInnerText(action)); - } - } - - /* Get all possible item attributes*/ - ZorkItem tempItem = new ZorkItem( - DOMUtils.getInnerTextByTagName(element, "name", ""), - DOMUtils.getInnerTextByTagName(element, "description", ""), - DOMUtils.getInnerTextByTagName(element, "writing", ""), - prints, - actions - ); - - final List triggers = Elements.byTagName(element, "trigger").stream() - .map(Parsers.trigger::parse) - .collect(Collectors.toList()); - tempItem.trigger.addAll(triggers); + final ZorkItem tempItem = Parsers.item.parse(element); /* Put each item in the items hashmap, the generic objects hashmap, and store its type in object lookup */ data.addObjectThroughLookup(ITEM, tempItem); - tempItem.updateStatus(DOMUtils.getInnerTextByTagName(element, "status", "")); } private static void addRoom(ZorkGame data, Element element) { + final ZorkRoom tempRoom = Parsers.room.parse(element); - /*Get all possible Room attributes*/ - ZorkRoom tempRoom = new ZorkRoom( - DOMUtils.getInnerTextByTagName(element, "name", ""), - DOMUtils.getInnerTextByTagName(element, "description", ""), - DOMUtils.getInnerTextByTagName(element, "type", "regular") - ); - - final List triggers = Elements.byTagName(element, "trigger").stream() - .map(Parsers.trigger::parse) - .collect(Collectors.toList()); - tempRoom.trigger.addAll(triggers); - - tempRoom.updateStatus(DOMUtils.getInnerTextByTagName(element, "status", "")); - - for (Element item : DOMUtils.iterator(element.getElementsByTagName("item"))) { - String itemName = DOMUtils.getInnerText(item); - tempRoom.item.add(itemName); - } - - for (Element creature : DOMUtils.iterator(element.getElementsByTagName("creature"))) { - String creatureName = DOMUtils.getInnerText(creature); - tempRoom.creature.add(creatureName); - } - - for (Element container : DOMUtils.iterator(element.getElementsByTagName("container"))) { - String containerName = DOMUtils.getInnerText(container); - tempRoom.container.add(containerName); - } - - for (Element border : DOMUtils.iterator(element.getElementsByTagName("border"))) { - String borderDirection = DOMUtils.getInnerText((Element) border.getElementsByTagName("direction").item(0)); - String borderName = DOMUtils.getInnerText((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); } diff --git a/src/main/java/com/github/dtschust/zork/parser/builders/Parsers.java b/src/main/java/com/github/dtschust/zork/parser/builders/Parsers.java index d771eee..12e200e 100644 --- a/src/main/java/com/github/dtschust/zork/parser/builders/Parsers.java +++ b/src/main/java/com/github/dtschust/zork/parser/builders/Parsers.java @@ -3,16 +3,21 @@ package com.github.dtschust.zork.parser.builders; import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkTrigger; import com.github.dtschust.zork.types.ZorkContainer; +import com.github.dtschust.zork.types.ZorkCreature; +import com.github.dtschust.zork.types.ZorkItem; +import com.github.dtschust.zork.types.ZorkRoom; /** * Inversion of control for Zork parse strategies */ public final class Parsers { - public static final ZorkParseStrategy condition = new ZorkConditionParseStrategy(); - public static final ZorkParseStrategy trigger = new ZorkTriggerListParseStrategy(condition); + private static final ZorkParseStrategy condition = new ZorkConditionParseStrategy(); + private static final ZorkParseStrategy trigger = new ZorkTriggerListParseStrategy(condition); public static final ZorkParseStrategy container = new ZorkContainerParseStrategy(trigger); + public static final ZorkParseStrategy item = new ZorkItemParseStrategy(trigger); + public static final ZorkParseStrategy room = new ZorkRoomParseStrategy(trigger); + public static final ZorkParseStrategy creature = new ZorkCreatureParseStrategy(condition, trigger); private Parsers() { - } } diff --git a/src/main/java/com/github/dtschust/zork/parser/builders/ZorkCreatureParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/builders/ZorkCreatureParseStrategy.java new file mode 100644 index 0000000..54a8a32 --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/parser/builders/ZorkCreatureParseStrategy.java @@ -0,0 +1,52 @@ +package com.github.dtschust.zork.parser.builders; + +import com.github.dtschust.zork.ZorkCondition; +import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.parser.DOMUtils; +import com.github.dtschust.zork.parser.dom.Elements; +import com.github.dtschust.zork.types.ZorkCreature; +import org.w3c.dom.Element; + +import java.util.List; +import java.util.stream.Collectors; + +public class ZorkCreatureParseStrategy implements ZorkParseStrategy { + + private final ZorkParseStrategy conditionStrategy; + private final ZorkParseStrategy triggerStrategy; + + public ZorkCreatureParseStrategy(final ZorkParseStrategy conditionStrategy, + final ZorkParseStrategy triggerStrategy) { + this.conditionStrategy = conditionStrategy; + this.triggerStrategy = triggerStrategy; + } + + @Override + public ZorkCreature parse(final Element source) { + // Get all possible creature attributes + final List conditions = Elements.byTagName(source, "attack").stream() + .flatMap(e -> Elements.byTagName(e, "condition").stream()) + .map(conditionStrategy::parse) + .collect(Collectors.toList()); + + final List prints = Elements.byTagName(source, "attack").stream() + .flatMap(e -> Elements.innerTextByTagName(e, "print").stream()) + .collect(Collectors.toList()); + + final List actions = Elements.byTagName(source, "attack").stream() + .flatMap(e -> Elements.innerTextByTagName(e, "action").stream()) + .collect(Collectors.toList()); + + final List vulnerabilities = Elements.innerTextByTagName(source, "vulnerability"); + + final List triggers = Elements.byTagName(source, "trigger").stream() + .map(triggerStrategy::parse) + .collect(Collectors.toList()); + + final String name = DOMUtils.getInnerTextByTagName(source, "name", ""); + final String description = DOMUtils.getInnerTextByTagName(source, "description", ""); + final String status = DOMUtils.getInnerTextByTagName(source, "status", ""); + + return new ZorkCreature(name, description, status, triggers, vulnerabilities, conditions, prints, actions); + } +} diff --git a/src/main/java/com/github/dtschust/zork/parser/builders/ZorkItemParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/builders/ZorkItemParseStrategy.java new file mode 100644 index 0000000..9af4bab --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/parser/builders/ZorkItemParseStrategy.java @@ -0,0 +1,45 @@ +package com.github.dtschust.zork.parser.builders; + +import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.parser.DOMUtils; +import com.github.dtschust.zork.parser.dom.Elements; +import com.github.dtschust.zork.types.ZorkItem; +import org.w3c.dom.Element; + +import java.util.List; +import java.util.stream.Collectors; + +public class ZorkItemParseStrategy implements ZorkParseStrategy { + + private final ZorkParseStrategy triggerStrategy; + + public ZorkItemParseStrategy(final ZorkParseStrategy triggerStrategy) { + this.triggerStrategy = triggerStrategy; + } + + @Override + public ZorkItem parse(final Element source) { + final List prints = Elements.byTagName(source, "turnon").stream() + .flatMap(e -> Elements.innerTextByTagName(e, "print").stream()) + .collect(Collectors.toList()); + + final List actions = Elements.byTagName(source, "turnon").stream() + .flatMap(e -> Elements.innerTextByTagName(e, "action").stream()) + .collect(Collectors.toList()); + + final List triggers = Elements.byTagName(source, "trigger").stream() + .map(triggerStrategy::parse) + .collect(Collectors.toList()); + + /* Get all possible item attributes*/ + return new ZorkItem( + DOMUtils.getInnerTextByTagName(source, "name", ""), + DOMUtils.getInnerTextByTagName(source, "description", ""), + DOMUtils.getInnerTextByTagName(source, "status", ""), + DOMUtils.getInnerTextByTagName(source, "writing", ""), + triggers, + prints, + actions + ); + } +} diff --git a/src/main/java/com/github/dtschust/zork/parser/builders/ZorkRoomParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/builders/ZorkRoomParseStrategy.java new file mode 100644 index 0000000..6fe50fe --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/parser/builders/ZorkRoomParseStrategy.java @@ -0,0 +1,46 @@ +package com.github.dtschust.zork.parser.builders; + +import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.parser.DOMUtils; +import com.github.dtschust.zork.parser.dom.Elements; +import com.github.dtschust.zork.types.ZorkRoom; +import org.w3c.dom.Element; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class ZorkRoomParseStrategy implements ZorkParseStrategy { + + private final ZorkParseStrategy triggerStrategy; + + public ZorkRoomParseStrategy(final ZorkParseStrategy triggerStrategy) { + this.triggerStrategy = triggerStrategy; + } + + @Override + public ZorkRoom parse(final Element source) { + // Get all possible Room attributes + final String name = DOMUtils.getInnerTextByTagName(source, "name", ""); + final String description = DOMUtils.getInnerTextByTagName(source, "description", ""); + final String type = DOMUtils.getInnerTextByTagName(source, "type", "regular"); + + final List triggers = Elements.byTagName(source, "trigger").stream() + .map(triggerStrategy::parse) + .collect(Collectors.toList()); + + final String status = DOMUtils.getInnerTextByTagName(source, "status", ""); + + final List items = Elements.innerTextByTagName(source, "item"); + final List creatures = Elements.innerTextByTagName(source, "creature"); + final List containers = Elements.innerTextByTagName(source, "container"); + + final Map borders = Elements.byTagName(source, "border").stream() + .collect(Collectors.toMap( + e -> DOMUtils.getInnerTextByTagName(e, "direction"), + e -> DOMUtils.getInnerTextByTagName(e, "name") + )); + + return new ZorkRoom(name, description, type, status, triggers, borders, containers, items, creatures); + } +} diff --git a/src/main/java/com/github/dtschust/zork/parser/dom/Elements.java b/src/main/java/com/github/dtschust/zork/parser/dom/Elements.java index 5450c46..a233edb 100644 --- a/src/main/java/com/github/dtschust/zork/parser/dom/Elements.java +++ b/src/main/java/com/github/dtschust/zork/parser/dom/Elements.java @@ -13,6 +13,10 @@ import java.util.stream.Collectors; public class Elements extends AbstractList implements RandomAccess { private final NodeList list; + private Elements(final NodeList l) { + list = l; + } + public static Elements byTagName(final Element parent, final String name) { return new Elements(parent.getElementsByTagName(name)); } @@ -23,10 +27,6 @@ public class Elements extends AbstractList implements RandomAccess { .collect(Collectors.toList()); } - private Elements(final NodeList l) { - list = l; - } - @Override public Element get(int index) { final Node e = list.item(index); diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java index 14e20b3..5224c93 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java @@ -1,7 +1,7 @@ package com.github.dtschust.zork.repl.actions; -import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.Zork.Type; +import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.types.HasSetOfCollectable; import com.github.dtschust.zork.types.ZorkObject; diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java index 32c0e4d..500b382 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java @@ -16,6 +16,16 @@ import static com.github.dtschust.zork.Zork.Type.*; * Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky */ public class DeleteAction implements Action { + private static void deleteElementFromSpace(ZorkGame game, Type space, Type element, String object) { + for (ZorkObject tempObject : game.values(ZorkObject.class, space)) { + Set set = ((HasSetOfCollectable) tempObject).getSetFromType(element); + if (set.contains(object)) { + set.remove(object); + game.put(space, tempObject); + } + } + } + @Override public boolean matchesInput(List arguments) { return arguments.get(0).equals("Delete"); @@ -32,7 +42,7 @@ public class DeleteAction implements Action { switch (game.getTypeFromLookup(object)) { case ROOM: - for (ZorkRoom tempRoom :game.values(ZorkRoom.class, ROOM)) { + for (ZorkRoom tempRoom : game.values(ZorkRoom.class, ROOM)) { for (String key : tempRoom.border.keySet()) { if (tempRoom.border.get(key).equals(object)) { tempRoom.border.remove(key); @@ -53,14 +63,4 @@ public class DeleteAction implements Action { break; } } - - private static void deleteElementFromSpace(ZorkGame game, Type space, Type element, String object) { - for (ZorkObject tempObject : game.values(ZorkObject.class, space)) { - Set set = ((HasSetOfCollectable) tempObject).getSetFromType(element); - if (set.contains(object)) { - set.remove(object); - game.put(space, tempObject); - } - } - } } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java index bfac122..53996ff 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java @@ -7,7 +7,8 @@ import com.github.dtschust.zork.types.ZorkRoom; import java.util.List; -import static com.github.dtschust.zork.Zork.Type.*; +import static com.github.dtschust.zork.Zork.Type.CONTAINER; +import static com.github.dtschust.zork.Zork.Type.ROOM; public class TakeAction implements Action { @Override diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkCreature.java b/src/main/java/com/github/dtschust/zork/types/ZorkCreature.java index 7231396..90fdfba 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkCreature.java +++ b/src/main/java/com/github/dtschust/zork/types/ZorkCreature.java @@ -2,6 +2,7 @@ package com.github.dtschust.zork.types; import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkGame; +import com.github.dtschust.zork.ZorkTrigger; import java.util.*; @@ -14,11 +15,13 @@ public class ZorkCreature extends ZorkObject implements HasPrintsAndActions { public ZorkCreature(final String name, final String description, + final String status, + final Collection triggers, final Collection vulnerabilities, final Collection conditions, final Collection prints, final Collection actions) { - super(name, description); + super(name, description, status, triggers); this.vulnerabilities = new HashSet<>(vulnerabilities); this.conditions = new ArrayList<>(conditions); this.print = new ArrayList<>(prints); diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkItem.java b/src/main/java/com/github/dtschust/zork/types/ZorkItem.java index e87452b..9897df5 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkItem.java +++ b/src/main/java/com/github/dtschust/zork/types/ZorkItem.java @@ -1,6 +1,9 @@ package com.github.dtschust.zork.types; +import com.github.dtschust.zork.ZorkTrigger; + import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; @@ -12,10 +15,12 @@ public class ZorkItem extends ZorkObject implements HasPrintsAndActions { public ZorkItem(final String name, final String description, + final String status, final String writing, - final List turnOnPrint, - final List turnOnAction) { - super(name, description); + final Collection triggers, + final Collection turnOnPrint, + final Collection turnOnAction) { + super(name, description, status, triggers); this.writing = writing; this.turnOnPrint = new ArrayList<>(turnOnPrint); this.turnOnAction = new ArrayList<>(turnOnAction); diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkRoom.java b/src/main/java/com/github/dtschust/zork/types/ZorkRoom.java index 8bfffa2..8c465e8 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkRoom.java +++ b/src/main/java/com/github/dtschust/zork/types/ZorkRoom.java @@ -1,25 +1,34 @@ package com.github.dtschust.zork.types; -import com.github.dtschust.zork.Zork; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import com.github.dtschust.zork.Zork; +import com.github.dtschust.zork.ZorkTrigger; + +import java.util.*; /* Room*/ public class ZorkRoom extends ZorkObject implements HasSetOfCollectable { public final String type; - public final Map border = new HashMap<>(); - public final Set container = new HashSet<>(); - public final Set item = new HashSet<>(); - public final Set creature = new HashSet<>(); + public final Map border; + public final Set container; + public final Set item; + public final Set creature; public ZorkRoom(final String name, final String description, - final String type) { - super(name, description); + final String type, + final String status, + final Collection triggers, + final Map borders, + final Collection containers, + final Collection items, + final Collection creatures) { + super(name, description, status, triggers); this.type = type; + this.border = new HashMap<>(borders); + this.container = new HashSet<>(containers); + this.item = new HashSet<>(items); + this.creature = new HashSet<>(creatures); } @Override