Minor fixes

This commit is contained in:
Claudio Maggioni 2022-11-22 18:44:40 +01:00
parent 6df81726d1
commit e8a108d89f
7 changed files with 35 additions and 23 deletions

View File

@ -8,17 +8,12 @@ package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ParserIOC; import com.github.dtschust.zork.parser.ParserIOC;
import com.github.dtschust.zork.repl.ActionDispatcher; import com.github.dtschust.zork.repl.ActionDispatcher;
import java.io.PrintStream;
import java.util.Scanner; import java.util.Scanner;
/* And away we go*/ /* And away we go*/
public class Zork { public class Zork {
final ZorkGame game; public static void runZork(final String filename) {
final Scanner input = new Scanner(System.in); ZorkGame game = ParserIOC.xmlParser().parse(filename, System.out);
final PrintStream output = System.out;
public Zork(final String filename) {
game = ParserIOC.xmlParser().parse(filename, output);
ActionDispatcher d = new ActionDispatcher(game); ActionDispatcher d = new ActionDispatcher(game);
@ -27,6 +22,7 @@ public class Zork {
/* There is no stopping in Zork, until we're done!!*/ /* There is no stopping in Zork, until we're done!!*/
while (game.isRunning()) { while (game.isRunning()) {
Scanner input = new Scanner(System.in);
String userInput = input.nextLine(); String userInput = input.nextLine();
/*Now that we have the user command, check the input*/ /*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.*/ /* 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) { if (args.length != 1) {
System.out.println("Usage: java Zork [filename]"); System.out.println("Usage: java Zork [filename]");
return; return;
} }
new Zork(args[0]); runZork(args[0]);
} }
} }

View File

@ -27,7 +27,7 @@ public class ZorkRoom extends ZorkObject implements ObjectCollector {
final Collection<String> creatures) { final Collection<String> creatures) {
super(name, description, status, triggers); super(name, description, status, triggers);
this.type = type; this.type = type;
this.border = new HashMap<>(borders); this.border = new EnumMap<>(borders);
this.container = new HashSet<>(containers); this.container = new HashSet<>(containers);
this.item = new HashSet<>(items); this.item = new HashSet<>(items);
this.creature = new HashSet<>(creatures); this.creature = new HashSet<>(creatures);
@ -38,9 +38,9 @@ public class ZorkRoom extends ZorkObject implements ObjectCollector {
} }
public void removeBorderingRoom(String roomName) { public void removeBorderingRoom(String roomName) {
for (final ZorkDirection d : this.border.keySet()) { for (final Map.Entry<ZorkDirection, String> d : this.border.entrySet()) {
if (this.border.get(d).equals(roomName)) { if (d.getValue().equals(roomName)) {
this.border.remove(d); this.border.remove(d.getKey());
} }
} }
} }
@ -49,6 +49,7 @@ public class ZorkRoom extends ZorkObject implements ObjectCollector {
return Optional.ofNullable(this.border.get(border)); return Optional.ofNullable(this.border.get(border));
} }
@Override
public boolean evaluateTriggers(ZorkGame game, String input) { public boolean evaluateTriggers(ZorkGame game, String input) {
final boolean items = ZorkTrigger.evaluateTriggersFor(item.stream(), game, input); final boolean items = ZorkTrigger.evaluateTriggersFor(item.stream(), game, input);
final boolean creatures = ZorkTrigger.evaluateTriggersFor(creature.stream(), game, input); final boolean creatures = ZorkTrigger.evaluateTriggersFor(creature.stream(), game, input);

View File

@ -14,6 +14,6 @@ public interface TriggerPropertyParseStrategy {
* @return a lambda mapping a source, a child element of `parent`, to ZorkTrigger objects * @return a lambda mapping a source, a child element of `parent`, to ZorkTrigger objects
*/ */
default Function<Property, ZorkTrigger> parse(final Property parent) { default Function<Property, ZorkTrigger> parse(final Property parent) {
return (source) -> this.parseTrigger(source, parent); return source -> this.parseTrigger(source, parent);
} }
} }

View File

@ -5,6 +5,7 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.Objects;
import java.util.RandomAccess; import java.util.RandomAccess;
public class DOMElementList extends AbstractList<DOMElement> implements RandomAccess { public class DOMElementList extends AbstractList<DOMElement> implements RandomAccess {
@ -14,6 +15,20 @@ public class DOMElementList extends AbstractList<DOMElement> implements RandomAc
list = l; 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) { static DOMElementList byTagName(final Element parent, final String name) {
return new DOMElementList(parent.getElementsByTagName(name)); return new DOMElementList(parent.getElementsByTagName(name));
} }

View File

@ -23,17 +23,19 @@ public class ZorkCreatureParseStrategy implements PropertyParseStrategy<ZorkCrea
@Override @Override
public ZorkCreature parse(final Property source) { public ZorkCreature parse(final Property source) {
final List<? extends Property> attacks = source.subPropertiesByName("attack");
// Get all possible creature attributes // Get all possible creature attributes
final List<ZorkCondition> conditions = source.subPropertiesByName("attack").stream() final List<ZorkCondition> conditions = attacks.stream()
.flatMap(e -> e.subPropertiesByName("condition").stream()) .flatMap(e -> e.subPropertiesByName("condition").stream())
.map(conditionStrategy::parse) .map(conditionStrategy::parse)
.collect(Collectors.toList()); .collect(Collectors.toList());
final List<String> prints = source.subPropertiesByName("attack").stream() final List<String> prints = attacks.stream()
.flatMap(e -> e.subPropertyValues("print").stream()) .flatMap(e -> e.subPropertyValues("print").stream())
.collect(Collectors.toList()); .collect(Collectors.toList());
final List<String> actions = source.subPropertiesByName("attack").stream() final List<String> actions = attacks.stream()
.flatMap(e -> e.subPropertyValues("action").stream()) .flatMap(e -> e.subPropertyValues("action").stream())
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -24,11 +24,9 @@ public class StartGameAction implements Action {
public boolean run(ZorkGame game, List<String> arguments) { public boolean run(ZorkGame game, List<String> arguments) {
final String room = arguments.get(2); final String room = arguments.get(2);
if (!game.isRunning()) { if (!game.isRunning() && game.changeRoom(room)) {
if (game.changeRoom(room)) { game.stream.println(game.getCurrentRoom().getDescription());
game.stream.println(game.getCurrentRoom().getDescription()); return true;
return true;
}
} }
return false; return false;
} }

View File

@ -21,7 +21,7 @@ class ZorkTest {
IOWrapper io = new IOWrapper(true); IOWrapper io = new IOWrapper(true);
new Thread(() -> { new Thread(() -> {
try { try {
catchSystemExit(() -> new Zork(gameConfig)); catchSystemExit(() -> Zork.runZork(gameConfig));
} catch (Exception ignored) {} } catch (Exception ignored) {}
}).start(); }).start();
while(true){ while(true){