diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index 16bc715..e3ad245 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -8,17 +8,12 @@ package com.github.dtschust.zork; import com.github.dtschust.zork.parser.ParserIOC; import com.github.dtschust.zork.repl.ActionDispatcher; -import java.io.PrintStream; import java.util.Scanner; /* And away we go*/ public class Zork { - final ZorkGame game; - final Scanner input = new Scanner(System.in); - final PrintStream output = System.out; - - public Zork(final String filename) { - game = ParserIOC.xmlParser().parse(filename, output); + public static void runZork(final String filename) { + ZorkGame game = ParserIOC.xmlParser().parse(filename, System.out); ActionDispatcher d = new ActionDispatcher(game); @@ -27,6 +22,7 @@ public class Zork { /* There is no stopping in Zork, until we're done!!*/ while (game.isRunning()) { + Scanner input = new Scanner(System.in); String userInput = input.nextLine(); /*Now that we have the user command, check the input*/ @@ -44,11 +40,11 @@ public class Zork { } /* I love how basic java main functions are sometimes.*/ - public static void main(String[] args) { + public static void main(final String[] args) { if (args.length != 1) { System.out.println("Usage: java Zork [filename]"); return; } - new Zork(args[0]); + runZork(args[0]); } } diff --git a/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java b/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java index 2d5e858..6b06caf 100644 --- a/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java @@ -27,7 +27,7 @@ public class ZorkRoom extends ZorkObject implements ObjectCollector { final Collection creatures) { super(name, description, status, triggers); this.type = type; - this.border = new HashMap<>(borders); + this.border = new EnumMap<>(borders); this.container = new HashSet<>(containers); this.item = new HashSet<>(items); this.creature = new HashSet<>(creatures); @@ -38,9 +38,9 @@ public class ZorkRoom extends ZorkObject implements ObjectCollector { } public void removeBorderingRoom(String roomName) { - for (final ZorkDirection d : this.border.keySet()) { - if (this.border.get(d).equals(roomName)) { - this.border.remove(d); + for (final Map.Entry d : this.border.entrySet()) { + if (d.getValue().equals(roomName)) { + this.border.remove(d.getKey()); } } } @@ -49,6 +49,7 @@ public class ZorkRoom extends ZorkObject implements ObjectCollector { return Optional.ofNullable(this.border.get(border)); } + @Override public boolean evaluateTriggers(ZorkGame game, String input) { final boolean items = ZorkTrigger.evaluateTriggersFor(item.stream(), game, input); final boolean creatures = ZorkTrigger.evaluateTriggersFor(creature.stream(), game, input); diff --git a/src/main/java/com/github/dtschust/zork/parser/TriggerPropertyParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/TriggerPropertyParseStrategy.java index 0eab3c0..f0e09fa 100644 --- a/src/main/java/com/github/dtschust/zork/parser/TriggerPropertyParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/TriggerPropertyParseStrategy.java @@ -14,6 +14,6 @@ public interface TriggerPropertyParseStrategy { * @return a lambda mapping a source, a child element of `parent`, to ZorkTrigger objects */ default Function parse(final Property parent) { - return (source) -> this.parseTrigger(source, parent); + return source -> this.parseTrigger(source, parent); } } diff --git a/src/main/java/com/github/dtschust/zork/parser/dom/DOMElementList.java b/src/main/java/com/github/dtschust/zork/parser/dom/DOMElementList.java index 57bbc85..819377a 100644 --- a/src/main/java/com/github/dtschust/zork/parser/dom/DOMElementList.java +++ b/src/main/java/com/github/dtschust/zork/parser/dom/DOMElementList.java @@ -5,6 +5,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.util.AbstractList; +import java.util.Objects; import java.util.RandomAccess; public class DOMElementList extends AbstractList implements RandomAccess { @@ -14,6 +15,20 @@ public class DOMElementList extends AbstractList implements RandomAc list = l; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + DOMElementList that = (DOMElementList) o; + return list.equals(that.list); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), list); + } + static DOMElementList byTagName(final Element parent, final String name) { return new DOMElementList(parent.getElementsByTagName(name)); } 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 82d137d..50b72e1 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 @@ -23,17 +23,19 @@ public class ZorkCreatureParseStrategy implements PropertyParseStrategy attacks = source.subPropertiesByName("attack"); + // Get all possible creature attributes - final List conditions = source.subPropertiesByName("attack").stream() + final List conditions = attacks.stream() .flatMap(e -> e.subPropertiesByName("condition").stream()) .map(conditionStrategy::parse) .collect(Collectors.toList()); - final List prints = source.subPropertiesByName("attack").stream() + final List prints = attacks.stream() .flatMap(e -> e.subPropertyValues("print").stream()) .collect(Collectors.toList()); - final List actions = source.subPropertiesByName("attack").stream() + final List actions = attacks.stream() .flatMap(e -> e.subPropertyValues("action").stream()) .collect(Collectors.toList()); diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java index bc9494e..5282d57 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java @@ -24,11 +24,9 @@ public class StartGameAction implements Action { public boolean run(ZorkGame game, List arguments) { final String room = arguments.get(2); - if (!game.isRunning()) { - if (game.changeRoom(room)) { - game.stream.println(game.getCurrentRoom().getDescription()); - return true; - } + if (!game.isRunning() && game.changeRoom(room)) { + game.stream.println(game.getCurrentRoom().getDescription()); + return true; } return false; } diff --git a/src/test/java/com/github/dtschust/zork/ZorkTest.java b/src/test/java/com/github/dtschust/zork/ZorkTest.java index 923f179..6b1d427 100644 --- a/src/test/java/com/github/dtschust/zork/ZorkTest.java +++ b/src/test/java/com/github/dtschust/zork/ZorkTest.java @@ -21,7 +21,7 @@ class ZorkTest { IOWrapper io = new IOWrapper(true); new Thread(() -> { try { - catchSystemExit(() -> new Zork(gameConfig)); + catchSystemExit(() -> Zork.runZork(gameConfig)); } catch (Exception ignored) {} }).start(); while(true){