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.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]);
}
}

View File

@ -27,7 +27,7 @@ public class ZorkRoom extends ZorkObject implements ObjectCollector {
final Collection<String> 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<ZorkDirection, String> 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);

View File

@ -14,6 +14,6 @@ public interface TriggerPropertyParseStrategy {
* @return a lambda mapping a source, a child element of `parent`, to ZorkTrigger objects
*/
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 java.util.AbstractList;
import java.util.Objects;
import java.util.RandomAccess;
public class DOMElementList extends AbstractList<DOMElement> implements RandomAccess {
@ -14,6 +15,20 @@ public class DOMElementList extends AbstractList<DOMElement> 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));
}

View File

@ -23,17 +23,19 @@ public class ZorkCreatureParseStrategy implements PropertyParseStrategy<ZorkCrea
@Override
public ZorkCreature parse(final Property source) {
final List<? extends Property> attacks = source.subPropertiesByName("attack");
// 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())
.map(conditionStrategy::parse)
.collect(Collectors.toList());
final List<String> prints = source.subPropertiesByName("attack").stream()
final List<String> prints = attacks.stream()
.flatMap(e -> e.subPropertyValues("print").stream())
.collect(Collectors.toList());
final List<String> actions = source.subPropertiesByName("attack").stream()
final List<String> actions = attacks.stream()
.flatMap(e -> e.subPropertyValues("action").stream())
.collect(Collectors.toList());

View File

@ -24,11 +24,9 @@ public class StartGameAction implements Action {
public boolean run(ZorkGame game, List<String> 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;
}

View File

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