diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index 58f7bb5..b3767d0 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -7,13 +7,11 @@ package com.github.dtschust.zork; import com.github.dtschust.zork.parser.ParserIOC; import com.github.dtschust.zork.repl.ActionDispatcher; -import com.github.dtschust.zork.types.ZorkContainer; -import com.github.dtschust.zork.types.ZorkObject; +import com.github.dtschust.zork.objects.ZorkContainer; -import java.util.Iterator; import java.util.Scanner; -import static com.github.dtschust.zork.types.ZorkGameStatusType.*; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; /* And away we go*/ public class Zork { @@ -34,12 +32,12 @@ public class Zork { String userInput = source.nextLine(); /*Now that we have the user command, check the input*/ - if (!executeTriggers(userInput)) { + if (!game.evaluateTriggers(userInput)) { /* If we haven't skipped, perform the user action*/ d.dispatch(userInput); /* Clear the user input, and check the triggers again (various states have changed, gnomes need to be found!*/ - executeTriggers(""); + game.evaluateTriggers(""); } } @@ -55,95 +53,4 @@ public class Zork { } new Zork(args[0]); } - - /* Check triggers */ - public boolean executeTriggers(String input) { - - /*Variable initialization*/ - boolean skip; - - /*Check Room triggers*/ - skip = doTriggersRoom(input); - /* Check items in the containers in the room */ - skip = skip || doTriggersItemsInContainersInRoom(input); - /* Check all containers in the room*/ - skip = skip || doTriggersContainersInRoom(input); - /* Check all creatures in the room */ - skip = skip || doTriggersCreaturesInRoom(input); - /* Check items in inventory */ - skip = skip || doTriggersItemsInInventory(input); - /* Check items in room */ - skip = skip || doTriggersItemsInRoom(input); - - return skip; - } - - private boolean doTriggersContainersInRoom(String input) { - boolean skip = false; - for (String key : game.getCurrentRoom().getContainer()) { - skip = skip || doZorkTriggers(game.get(CONTAINER, key), input); - } - return skip; - } - - private boolean doTriggersItemsInContainersInRoom(String input) { - boolean skip = false; - for (String key : game.getCurrentRoom().getContainer()) { - ZorkContainer tempContainer = (ZorkContainer) game.get(CONTAINER, key); - for (String key2 : tempContainer.items()) { - skip = skip || doZorkTriggers(game.get(ITEM, key2), input); - } - } - return skip; - } - - private boolean doTriggersItemsInRoom(String input) { - boolean skip = false; - for (String key : game.getCurrentRoom().getItem()) { - skip = skip || doZorkTriggers(game.get(ITEM, key), input); - } - return skip; - } - - private boolean doTriggersItemsInInventory(String input) { - boolean skip = false; - for (String key : game.inventory) { - skip = skip || doZorkTriggers(game.get(ITEM, key), input); - } - return skip; - } - - private boolean doTriggersCreaturesInRoom(String input) { - boolean skip = false; - for (String key : game.getCurrentRoom().getCreature()) { - skip = skip || doZorkTriggers(game.get(CREATURE, key), input); - } - return skip; - } - - private boolean doTriggersRoom(String input) { - return doZorkTriggers(game.getCurrentRoom(), input); - } - - private boolean doZorkTriggers(ZorkObject zorkObject, String input) { - boolean skip = false; - Iterator iterator = zorkObject.getTrigger().iterator(); - while (iterator.hasNext()) { - ZorkTrigger tempTrigger = iterator.next(); - if (tempTrigger.evaluate(game, input)) { - for (String print : tempTrigger.print) { - System.out.println(print); - } - final ActionDispatcher d = new ActionDispatcher(game); - for (String action : tempTrigger.action) { - d.dispatch(action); - } - skip = skip || tempTrigger.hasCommand(); - if (tempTrigger.type.equals("single")) { - iterator.remove(); - } - } - } - return skip; - } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java index b389779..e8aaec4 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java +++ b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java @@ -1,10 +1,10 @@ package com.github.dtschust.zork; -import com.github.dtschust.zork.types.ZorkContainer; -import com.github.dtschust.zork.types.ZorkRoom; +import com.github.dtschust.zork.objects.ZorkContainer; +import com.github.dtschust.zork.objects.ZorkRoom; -import static com.github.dtschust.zork.types.ZorkGameStatusType.CONTAINER; -import static com.github.dtschust.zork.types.ZorkGameStatusType.ROOM; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.ROOM; /* Has conditions*/ diff --git a/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java b/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java index b6c32cd..17240c7 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java +++ b/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java @@ -1,6 +1,6 @@ package com.github.dtschust.zork; -import com.github.dtschust.zork.types.ZorkObject; +import com.github.dtschust.zork.objects.ZorkObject; /* Status conditions*/ public class ZorkConditionStatus extends ZorkCondition { diff --git a/src/main/java/com/github/dtschust/zork/ZorkGame.java b/src/main/java/com/github/dtschust/zork/ZorkGame.java index 428c2b3..30e8b97 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkGame.java +++ b/src/main/java/com/github/dtschust/zork/ZorkGame.java @@ -1,19 +1,23 @@ package com.github.dtschust.zork; -import com.github.dtschust.zork.types.*; +import com.github.dtschust.zork.objects.*; +import com.github.dtschust.zork.types.Triggerable; +import com.github.dtschust.zork.types.ZorkMap; import java.util.HashMap; import java.util.HashSet; import java.util.Set; -public class ZorkGame { +import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; + +public class ZorkGame implements Triggerable { public final Set inventory = new HashSet<>(); private final ZorkMap rooms = new ZorkMap<>(); private final ZorkMap items = new ZorkMap<>(); private final ZorkMap containers = new ZorkMap<>(); private final ZorkMap creatures = new ZorkMap<>(); - private final HashMap objectLookup = new HashMap<>(); + private final HashMap objectLookup = new HashMap<>(); private boolean running = false; private String currentRoom; @@ -38,11 +42,11 @@ public class ZorkGame { running = false; } - public ZorkGameStatusType getTypeFromLookup(String object) { + public ZorkObjectTypes getTypeFromLookup(String object) { return objectLookup.get(object); } - public void addObjectThroughLookup(ZorkGameStatusType type, ZorkObject object) { + public void addObjectThroughLookup(ZorkObjectTypes type, ZorkObject object) { putInMapGivenType(type, object); objectLookup.put(object.getName(), type); } @@ -51,20 +55,20 @@ public class ZorkGame { return values(cast, objectLookup.get(name)); } - public ZorkMap values(Class cast, ZorkGameStatusType type) { + public ZorkMap values(Class cast, ZorkObjectTypes type) { return (ZorkMap) getMapFromType(type); } - public ZorkObject get(ZorkGameStatusType type, String key) { + public ZorkObject get(ZorkObjectTypes type, String key) { return getMapFromType(type).get(key); } - public void put(ZorkGameStatusType type, ZorkObject object) { + public void put(ZorkObjectTypes type, ZorkObject object) { putInMapGivenType(type, object); } - private ZorkMap getMapFromType(ZorkGameStatusType type) { + private ZorkMap getMapFromType(ZorkObjectTypes type) { switch (type) { case ROOM: return rooms; @@ -79,7 +83,7 @@ public class ZorkGame { } } - private void putInMapGivenType(ZorkGameStatusType type, ZorkObject object) { + private void putInMapGivenType(ZorkObjectTypes type, ZorkObject object) { switch (type) { case ROOM: rooms.put((ZorkRoom) object); @@ -97,4 +101,16 @@ public class ZorkGame { throw new IllegalStateException("Unexpected value: " + type); } } + + @Override + public boolean evaluateTriggers(final ZorkGame game, final String currentCommand) { + final boolean currentRoom = getCurrentRoom().evaluateTriggers(game, currentCommand); + final boolean itemsInInventory = Triggerable.evaluateTriggerCollection(inventory, ITEM, game, currentCommand); + + return currentRoom || itemsInInventory; + } + + public boolean evaluateTriggers(final String currentCommand) { + return evaluateTriggers(this, currentCommand); + } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java index a9b89fb..77cb8ed 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java +++ b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java @@ -1,17 +1,21 @@ package com.github.dtschust.zork; +import com.github.dtschust.zork.types.HasPrintsAndActions; + +import java.util.Collections; import java.util.List; -/*Trigger*/ -public class ZorkTrigger { - public final List print; - public final List action; +public class ZorkTrigger implements HasPrintsAndActions { + + private final List print; + private final List action; + /* By default, "single" */ - public final String type; + private final ZorkTriggerType type; private final List conditions; private final List commands; - public ZorkTrigger(final String type, + public ZorkTrigger(final ZorkTriggerType type, final List conditions, final List commands, final List print, @@ -27,10 +31,22 @@ public class ZorkTrigger { return !this.commands.isEmpty(); } - public boolean evaluate(ZorkGame game, String input) { - if (!commands.stream().allMatch(c -> c.matchesInput(input))) { - return false; - } - return conditions.stream().allMatch(c -> c.evaluate(game)); + public boolean isTriggered(final ZorkGame game, final String currentCommand) { + return commands.stream().allMatch(c -> c.matchesInput(currentCommand)) && + conditions.stream().allMatch(c -> c.evaluate(game)); + } + + @Override + public List getPrints() { + return Collections.unmodifiableList(print); + } + + @Override + public List getActions() { + return Collections.unmodifiableList(action); + } + + public ZorkTriggerType getType() { + return type; } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkTriggerType.java b/src/main/java/com/github/dtschust/zork/ZorkTriggerType.java new file mode 100644 index 0000000..e575643 --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/ZorkTriggerType.java @@ -0,0 +1,19 @@ +package com.github.dtschust.zork; + +import java.util.EnumSet; +import java.util.Optional; + +public enum ZorkTriggerType { + SINGLE("single"), + PERMANENT("permanent"); + + private final String name; + + ZorkTriggerType(final String name) { + this.name = name; + } + + public static Optional fromName(final String name) { + return EnumSet.allOf(ZorkTriggerType.class).stream().filter(e -> e.name.equals(name)).findAny(); + } +} diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkContainer.java b/src/main/java/com/github/dtschust/zork/objects/ZorkContainer.java similarity index 75% rename from src/main/java/com/github/dtschust/zork/types/ZorkContainer.java rename to src/main/java/com/github/dtschust/zork/objects/ZorkContainer.java index b85ee62..9891476 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkContainer.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkContainer.java @@ -1,10 +1,13 @@ -package com.github.dtschust.zork.types; +package com.github.dtschust.zork.objects; +import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.types.HasSetOfCollectable; +import com.github.dtschust.zork.types.Triggerable; import java.util.*; -import static com.github.dtschust.zork.types.ZorkGameStatusType.ITEM; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; /* Container*/ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable { @@ -58,7 +61,7 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable { } @Override - public Set getSetFromType(ZorkGameStatusType type) { + public Set getSetFromType(ZorkObjectTypes type) { if (type.equals(ITEM)) return items; throw new IllegalStateException("Unexpected value: " + type); @@ -67,4 +70,10 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable { public List getAccepts() { return Collections.unmodifiableList(accepts); } + + @Override + public boolean evaluateTriggers(ZorkGame game, String input) { + final boolean itemsEvaluation = Triggerable.evaluateTriggerCollection(items, ITEM, game, input); + return super.evaluateTriggers(game, input) || itemsEvaluation; + } } diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkCreature.java b/src/main/java/com/github/dtschust/zork/objects/ZorkCreature.java similarity index 95% rename from src/main/java/com/github/dtschust/zork/types/ZorkCreature.java rename to src/main/java/com/github/dtschust/zork/objects/ZorkCreature.java index 90fdfba..b69709b 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkCreature.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkCreature.java @@ -1,8 +1,9 @@ -package com.github.dtschust.zork.types; +package com.github.dtschust.zork.objects; import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.types.HasPrintsAndActions; import java.util.*; diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkItem.java b/src/main/java/com/github/dtschust/zork/objects/ZorkItem.java similarity index 92% rename from src/main/java/com/github/dtschust/zork/types/ZorkItem.java rename to src/main/java/com/github/dtschust/zork/objects/ZorkItem.java index ad450ad..c3ac3b6 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkItem.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkItem.java @@ -1,6 +1,7 @@ -package com.github.dtschust.zork.types; +package com.github.dtschust.zork.objects; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.types.HasPrintsAndActions; import java.util.ArrayList; import java.util.Collection; diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkObject.java b/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java similarity index 57% rename from src/main/java/com/github/dtschust/zork/types/ZorkObject.java rename to src/main/java/com/github/dtschust/zork/objects/ZorkObject.java index cc0107d..b8aa90e 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkObject.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java @@ -1,14 +1,14 @@ -package com.github.dtschust.zork.types; +package com.github.dtschust.zork.objects; +import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.ZorkTriggerType; +import com.github.dtschust.zork.types.Triggerable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; /* Generic object, everything inherits from this*/ -public abstract class ZorkObject { +public abstract class ZorkObject implements Triggerable { private final String name; private final String description; private final List trigger; @@ -48,4 +48,20 @@ public abstract class ZorkObject { public List getTrigger() { return trigger; } + + public boolean evaluateTriggers(final ZorkGame game, final String input) { + boolean skip = false; + final Iterator iterator = trigger.iterator(); + while (iterator.hasNext()) { + final ZorkTrigger t = iterator.next(); + if (t.isTriggered(game, input)) { + t.printAndExecuteActions(game); + skip = skip || t.hasCommand(); + if (t.getType() == ZorkTriggerType.SINGLE) { + iterator.remove(); + } + } + } + return skip; + } } diff --git a/src/main/java/com/github/dtschust/zork/objects/ZorkObjectTypes.java b/src/main/java/com/github/dtschust/zork/objects/ZorkObjectTypes.java new file mode 100644 index 0000000..612e200 --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkObjectTypes.java @@ -0,0 +1,21 @@ +package com.github.dtschust.zork.objects; + +import java.util.EnumSet; +import java.util.Optional; + +public enum ZorkObjectTypes { + ROOM("room"), + ITEM("item"), + CONTAINER("container"), + CREATURE("creature"); + + private final String propertyName; + + ZorkObjectTypes(final String propertyName) { + this.propertyName = propertyName; + } + + public static Optional fromPropertyName(final String propertyName) { + return EnumSet.allOf(ZorkObjectTypes.class).stream().filter(e -> e.propertyName.equals(propertyName)).findFirst(); + } +} diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkRoom.java b/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java similarity index 70% rename from src/main/java/com/github/dtschust/zork/types/ZorkRoom.java rename to src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java index 540d9b4..4129417 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkRoom.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java @@ -1,10 +1,16 @@ -package com.github.dtschust.zork.types; +package com.github.dtschust.zork.objects; +import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.types.HasSetOfCollectable; +import com.github.dtschust.zork.types.Triggerable; +import com.github.dtschust.zork.types.ZorkDirection; import java.util.*; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; + /* Room*/ public class ZorkRoom extends ZorkObject implements HasSetOfCollectable { private final String type; @@ -31,7 +37,7 @@ public class ZorkRoom extends ZorkObject implements HasSetOfCollectable { } @Override - public Set getSetFromType(ZorkGameStatusType type) { + public Set getSetFromType(ZorkObjectTypes type) { switch (type) { case CONTAINER: return getContainer(); @@ -60,6 +66,14 @@ public class ZorkRoom extends ZorkObject implements HasSetOfCollectable { return Optional.ofNullable(this.border.get(border)); } + @Override + public boolean evaluateTriggers(ZorkGame game, String input) { + final boolean items = Triggerable.evaluateTriggerCollection(item, ITEM, game, input); + final boolean creatures = Triggerable.evaluateTriggerCollection(creature, CREATURE, game, input); + final boolean containers = Triggerable.evaluateTriggerCollection(container, CONTAINER, game, input); + return super.evaluateTriggers(game, input) || items || creatures || containers; + } + public Set getContainer() { return container; } diff --git a/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java b/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java index c414ded..0d09315 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java +++ b/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java @@ -2,10 +2,10 @@ package com.github.dtschust.zork.parser; import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.parser.strategies.*; -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 com.github.dtschust.zork.objects.ZorkContainer; +import com.github.dtschust.zork.objects.ZorkCreature; +import com.github.dtschust.zork.objects.ZorkItem; +import com.github.dtschust.zork.objects.ZorkRoom; /** * Inversion of control for Zork parse strategies diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java b/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java index e8d7ef4..42ecc1a 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java @@ -1,14 +1,14 @@ package com.github.dtschust.zork.parser; import com.github.dtschust.zork.ZorkGame; -import com.github.dtschust.zork.types.*; +import com.github.dtschust.zork.objects.*; import java.util.Map; -import static com.github.dtschust.zork.types.ZorkGameStatusType.*; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; public abstract class ZorkParser { - private final Map> strategies; + private final Map> strategies; public ZorkParser(final PropertyParseStrategy creatureStrategy, final PropertyParseStrategy containerStrategy, @@ -32,7 +32,7 @@ public abstract class ZorkParser { // Every single first generation child is a room, container, creature, or item. So load them in for (final Property element : rootElement.subProperties()) { final String name = element.name(); - final ZorkGameStatusType t = ZorkGameStatusType.fromPropertyName(name) + final ZorkObjectTypes t = ZorkObjectTypes.fromPropertyName(name) .orElseThrow(() -> new IllegalStateException("Unexpected value: " + name)); final ZorkObject built = strategies.get(t).parse(element); data.addObjectThroughLookup(t, built); diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java b/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java index 3252d21..f40e03e 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java @@ -1,10 +1,10 @@ package com.github.dtschust.zork.parser; import com.github.dtschust.zork.parser.dom.DOMElement; -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 com.github.dtschust.zork.objects.ZorkContainer; +import com.github.dtschust.zork.objects.ZorkCreature; +import com.github.dtschust.zork.objects.ZorkItem; +import com.github.dtschust.zork.objects.ZorkRoom; import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilderFactory; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java index 0bd449e..b0bc41e 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java @@ -4,7 +4,7 @@ import com.github.dtschust.zork.ZorkTrigger; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; -import com.github.dtschust.zork.types.ZorkContainer; +import com.github.dtschust.zork.objects.ZorkContainer; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java index d2a9ca7..a2ba01c 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java @@ -5,7 +5,7 @@ import com.github.dtschust.zork.ZorkTrigger; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; -import com.github.dtschust.zork.types.ZorkCreature; +import com.github.dtschust.zork.objects.ZorkCreature; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java index b0d2f58..7dfe825 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java @@ -4,7 +4,7 @@ import com.github.dtschust.zork.ZorkTrigger; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; -import com.github.dtschust.zork.types.ZorkItem; +import com.github.dtschust.zork.objects.ZorkItem; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java index 662ada7..2b564f2 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java @@ -5,7 +5,7 @@ import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; import com.github.dtschust.zork.types.ZorkDirection; -import com.github.dtschust.zork.types.ZorkRoom; +import com.github.dtschust.zork.objects.ZorkRoom; import java.util.List; import java.util.Map; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkTriggerParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkTriggerParseStrategy.java index 53c5caf..2f6cade 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkTriggerParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkTriggerParseStrategy.java @@ -3,6 +3,7 @@ package com.github.dtschust.zork.parser.strategies; import com.github.dtschust.zork.ZorkCommand; import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.ZorkTriggerType; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; @@ -20,7 +21,9 @@ public class ZorkTriggerParseStrategy implements TriggerPropertyParseStrategy { @Override public ZorkTrigger parseTrigger(final Property source, final Property parent) { - final String type = parent.subPropertyValue("type", "single"); + final String typeString = parent.subPropertyValue("type", "single"); + final ZorkTriggerType type = ZorkTriggerType.fromName(typeString).orElseThrow(() -> + new IllegalArgumentException(typeString + " is not a valid trigger type")); final List commands = source.subPropertiesByName("command").stream() .map(Property::value) 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 f2ca77b..2e3a3e7 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 @@ -3,8 +3,8 @@ package com.github.dtschust.zork.repl.actions; 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.ZorkGameStatusType; -import com.github.dtschust.zork.types.ZorkObject; +import com.github.dtschust.zork.objects.ZorkObjectTypes; +import com.github.dtschust.zork.objects.ZorkObject; import java.util.List; @@ -29,8 +29,8 @@ public class AddAction implements Action { final String destination = arguments.get(3); try { - ZorkGameStatusType destType = game.getTypeFromLookup(destination); - ZorkGameStatusType objType = game.getTypeFromLookup(object); + ZorkObjectTypes destType = game.getTypeFromLookup(destination); + ZorkObjectTypes objType = game.getTypeFromLookup(object); ZorkObject tempObject = game.get(destType, destination); ((HasSetOfCollectable) tempObject).getSetFromType(objType).add(object); game.put(destType, tempObject); diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java index 3c942fa..ba0e6c5 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java @@ -2,11 +2,11 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ZorkCreature; +import com.github.dtschust.zork.objects.ZorkCreature; import java.util.List; -import static com.github.dtschust.zork.types.ZorkGameStatusType.CREATURE; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.CREATURE; /** * Attempt an attack, do you feel lucky? 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 3b86517..fe9f95e 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 @@ -3,20 +3,20 @@ package com.github.dtschust.zork.repl.actions; 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.ZorkGameStatusType; -import com.github.dtschust.zork.types.ZorkObject; -import com.github.dtschust.zork.types.ZorkRoom; +import com.github.dtschust.zork.objects.ZorkObjectTypes; +import com.github.dtschust.zork.objects.ZorkObject; +import com.github.dtschust.zork.objects.ZorkRoom; import java.util.List; import java.util.Set; -import static com.github.dtschust.zork.types.ZorkGameStatusType.*; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; /** * 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, ZorkGameStatusType space, ZorkGameStatusType element, String object) { + private static void deleteElementFromSpace(ZorkGame game, ZorkObjectTypes space, ZorkObjectTypes element, String object) { for (ZorkObject tempObject : game.values(ZorkObject.class, space)) { Set set = ((HasSetOfCollectable) tempObject).getSetFromType(element); if (set.contains(object)) { diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java index c96c76a..081c948 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java @@ -2,11 +2,11 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ZorkRoom; +import com.github.dtschust.zork.objects.ZorkRoom; import java.util.List; -import static com.github.dtschust.zork.types.ZorkGameStatusType.ROOM; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.ROOM; public class DropItemAction implements Action { @Override diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java index 8c23d9b..ebf7c36 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java @@ -2,11 +2,11 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ZorkContainer; +import com.github.dtschust.zork.objects.ZorkContainer; import java.util.List; -import static com.github.dtschust.zork.types.ZorkGameStatusType.CONTAINER; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; public class OpenAction implements Action { @Override diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java index f8dcdd1..309646a 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java @@ -2,11 +2,11 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ZorkContainer; +import com.github.dtschust.zork.objects.ZorkContainer; import java.util.List; -import static com.github.dtschust.zork.types.ZorkGameStatusType.CONTAINER; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; public class PutAction implements Action { @Override diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java index 588c965..04982fc 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java @@ -2,11 +2,11 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ZorkItem; +import com.github.dtschust.zork.objects.ZorkItem; import java.util.List; -import static com.github.dtschust.zork.types.ZorkGameStatusType.ITEM; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; public class ReadAction implements Action { @Override 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 ac0ee33..6b09ce4 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 @@ -2,13 +2,13 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ZorkContainer; -import com.github.dtschust.zork.types.ZorkRoom; +import com.github.dtschust.zork.objects.ZorkContainer; +import com.github.dtschust.zork.objects.ZorkRoom; import java.util.List; -import static com.github.dtschust.zork.types.ZorkGameStatusType.CONTAINER; -import static com.github.dtschust.zork.types.ZorkGameStatusType.ROOM; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.ROOM; public class TakeAction implements Action { @Override diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java index 58fbd6f..7869399 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java @@ -2,11 +2,11 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ZorkItem; +import com.github.dtschust.zork.objects.ZorkItem; import java.util.List; -import static com.github.dtschust.zork.types.ZorkGameStatusType.ITEM; +import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; /** * Turn on an item diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java index 79afd77..201fb24 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java @@ -3,7 +3,7 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.types.ZorkMap; -import com.github.dtschust.zork.types.ZorkObject; +import com.github.dtschust.zork.objects.ZorkObject; import java.util.List; diff --git a/src/main/java/com/github/dtschust/zork/types/HasSetOfCollectable.java b/src/main/java/com/github/dtschust/zork/types/HasSetOfCollectable.java index e4b4a08..31e890d 100644 --- a/src/main/java/com/github/dtschust/zork/types/HasSetOfCollectable.java +++ b/src/main/java/com/github/dtschust/zork/types/HasSetOfCollectable.java @@ -1,10 +1,12 @@ package com.github.dtschust.zork.types; +import com.github.dtschust.zork.objects.ZorkObjectTypes; + import java.util.Set; public interface HasSetOfCollectable { - default Set getSetFromType(ZorkGameStatusType type) { + default Set getSetFromType(ZorkObjectTypes type) { throw new UnsupportedOperationException(); } } diff --git a/src/main/java/com/github/dtschust/zork/types/Triggerable.java b/src/main/java/com/github/dtschust/zork/types/Triggerable.java new file mode 100644 index 0000000..3ad66e5 --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/types/Triggerable.java @@ -0,0 +1,21 @@ +package com.github.dtschust.zork.types; + +import com.github.dtschust.zork.ZorkGame; +import com.github.dtschust.zork.objects.ZorkObjectTypes; + +import java.util.Collection; + +public interface Triggerable { + + boolean evaluateTriggers(final ZorkGame game, final String userCommand); + + static boolean evaluateTriggerCollection(final Collection collection, + final ZorkObjectTypes type, + final ZorkGame game, + final String input) { + // non short-circuited to execute all side effects of evaluateTriggers + return collection.stream() + .map(i -> game.get(type, i).evaluateTriggers(game, input)) + .reduce(false, (a, b) -> a || b); + } +} diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkGameStatusType.java b/src/main/java/com/github/dtschust/zork/types/ZorkGameStatusType.java deleted file mode 100644 index d195fc5..0000000 --- a/src/main/java/com/github/dtschust/zork/types/ZorkGameStatusType.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.github.dtschust.zork.types; - -import java.util.EnumSet; -import java.util.Optional; - -public enum ZorkGameStatusType { - ROOM("room"), - ITEM("item"), - CONTAINER("container"), - CREATURE("creature"); - - private final String propertyName; - - ZorkGameStatusType(final String propertyName) { - this.propertyName = propertyName; - } - - public static Optional fromPropertyName(final String propertyName) { - return EnumSet.allOf(ZorkGameStatusType.class).stream().filter(e -> e.propertyName.equals(propertyName)).findFirst(); - } -} diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkMap.java b/src/main/java/com/github/dtschust/zork/types/ZorkMap.java index 421ec73..63c0acb 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkMap.java +++ b/src/main/java/com/github/dtschust/zork/types/ZorkMap.java @@ -1,5 +1,7 @@ package com.github.dtschust.zork.types; +import com.github.dtschust.zork.objects.ZorkObject; + import java.util.HashMap; import java.util.Iterator;