Trigger evaluation moved from Zork class

This commit is contained in:
Claudio Maggioni 2022-11-22 15:12:55 +01:00
parent d0d2db70a4
commit 69072097bb
34 changed files with 228 additions and 201 deletions

View file

@ -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<ZorkTrigger> 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;
}
}

View file

@ -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*/

View file

@ -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 {

View file

@ -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<String> inventory = new HashSet<>();
private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>();
private final ZorkMap<ZorkItem> items = new ZorkMap<>();
private final ZorkMap<ZorkContainer> containers = new ZorkMap<>();
private final ZorkMap<ZorkCreature> creatures = new ZorkMap<>();
private final HashMap<String, ZorkGameStatusType> objectLookup = new HashMap<>();
private final HashMap<String, ZorkObjectTypes> 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 <T extends ZorkObject> ZorkMap<T> values(Class<T> cast, ZorkGameStatusType type) {
public <T extends ZorkObject> ZorkMap<T> values(Class<T> cast, ZorkObjectTypes type) {
return (ZorkMap<T>) 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<? extends ZorkObject> getMapFromType(ZorkGameStatusType type) {
private ZorkMap<? extends ZorkObject> 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);
}
}

View file

@ -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<String> print;
public final List<String> action;
public class ZorkTrigger implements HasPrintsAndActions {
private final List<String> print;
private final List<String> action;
/* By default, "single" */
public final String type;
private final ZorkTriggerType type;
private final List<ZorkCondition> conditions;
private final List<ZorkCommand> commands;
public ZorkTrigger(final String type,
public ZorkTrigger(final ZorkTriggerType type,
final List<ZorkCondition> conditions,
final List<ZorkCommand> commands,
final List<String> 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;
public boolean isTriggered(final ZorkGame game, final String currentCommand) {
return commands.stream().allMatch(c -> c.matchesInput(currentCommand)) &&
conditions.stream().allMatch(c -> c.evaluate(game));
}
return conditions.stream().allMatch(c -> c.evaluate(game));
@Override
public List<String> getPrints() {
return Collections.unmodifiableList(print);
}
@Override
public List<String> getActions() {
return Collections.unmodifiableList(action);
}
public ZorkTriggerType getType() {
return type;
}
}

View file

@ -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<ZorkTriggerType> fromName(final String name) {
return EnumSet.allOf(ZorkTriggerType.class).stream().filter(e -> e.name.equals(name)).findAny();
}
}

View file

@ -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<String> getSetFromType(ZorkGameStatusType type) {
public Set<String> 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<String> 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;
}
}

View file

@ -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.*;

View file

@ -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;

View file

@ -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<ZorkTrigger> trigger;
@ -48,4 +48,20 @@ public abstract class ZorkObject {
public List<ZorkTrigger> getTrigger() {
return trigger;
}
public boolean evaluateTriggers(final ZorkGame game, final String input) {
boolean skip = false;
final Iterator<ZorkTrigger> 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;
}
}

View file

@ -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<ZorkObjectTypes> fromPropertyName(final String propertyName) {
return EnumSet.allOf(ZorkObjectTypes.class).stream().filter(e -> e.propertyName.equals(propertyName)).findFirst();
}
}

View file

@ -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<String> getSetFromType(ZorkGameStatusType type) {
public Set<String> 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<String> getContainer() {
return container;
}

View file

@ -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

View file

@ -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<ZorkGameStatusType, PropertyParseStrategy<? extends ZorkObject>> strategies;
private final Map<ZorkObjectTypes, PropertyParseStrategy<? extends ZorkObject>> strategies;
public ZorkParser(final PropertyParseStrategy<ZorkCreature> creatureStrategy,
final PropertyParseStrategy<ZorkContainer> 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);

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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<ZorkCommand> commands = source.subPropertiesByName("command").stream()
.map(Property::value)

View file

@ -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);

View file

@ -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?

View file

@ -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<String> set = ((HasSetOfCollectable) tempObject).getSetFromType(element);
if (set.contains(object)) {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -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<String> getSetFromType(ZorkGameStatusType type) {
default Set<String> getSetFromType(ZorkObjectTypes type) {
throw new UnsupportedOperationException();
}
}

View file

@ -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<String> 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);
}
}

View file

@ -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<ZorkGameStatusType> fromPropertyName(final String propertyName) {
return EnumSet.allOf(ZorkGameStatusType.class).stream().filter(e -> e.propertyName.equals(propertyName)).findFirst();
}
}

View file

@ -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;