Minor cleanup

This commit is contained in:
Claudio Maggioni 2022-11-22 15:20:35 +01:00
parent 69072097bb
commit aafbf94434
8 changed files with 24 additions and 62 deletions

View file

@ -7,16 +7,13 @@ package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ParserIOC;
import com.github.dtschust.zork.repl.ActionDispatcher;
import com.github.dtschust.zork.objects.ZorkContainer;
import java.util.Scanner;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.*;
/* And away we go*/
public class Zork {
ZorkGame game;
Scanner source = new Scanner(System.in);
final ZorkGame game;
final Scanner source = new Scanner(System.in);
public Zork(final String filename) {
game = ParserIOC.xmlParser().parse(filename);
@ -36,7 +33,7 @@ public class Zork {
/* 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!*/
// check the triggers again (various states have changed, gnomes need to be found!)
game.evaluateTriggers("");
}
}

View file

@ -1,7 +1,6 @@
package com.github.dtschust.zork;
import com.github.dtschust.zork.objects.*;
import com.github.dtschust.zork.types.Triggerable;
import com.github.dtschust.zork.types.ZorkMap;
import java.util.HashMap;
@ -10,7 +9,7 @@ import java.util.Set;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
public class ZorkGame implements Triggerable {
public class ZorkGame {
public final Set<String> inventory = new HashSet<>();
private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>();
@ -102,10 +101,9 @@ public class ZorkGame implements Triggerable {
}
}
@Override
public boolean evaluateTriggers(final ZorkGame game, final String currentCommand) {
private boolean evaluateTriggers(final ZorkGame game, final String currentCommand) {
final boolean currentRoom = getCurrentRoom().evaluateTriggers(game, currentCommand);
final boolean itemsInInventory = Triggerable.evaluateTriggerCollection(inventory, ITEM, game, currentCommand);
final boolean itemsInInventory = ZorkTrigger.evaluateTriggerCollection(inventory, ITEM, game, currentCommand);
return currentRoom || itemsInInventory;
}

View file

@ -1,7 +1,9 @@
package com.github.dtschust.zork;
import com.github.dtschust.zork.objects.ZorkObjectTypes;
import com.github.dtschust.zork.types.HasPrintsAndActions;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -27,6 +29,16 @@ public class ZorkTrigger implements HasPrintsAndActions {
this.type = type;
}
public 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);
}
public boolean hasCommand() {
return !this.commands.isEmpty();
}

View file

@ -1,9 +1,7 @@
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.*;
@ -12,7 +10,6 @@ import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
/* Container*/
public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
private final Set<String> items;
private final List<String> accepts;
private boolean open;
public ZorkContainer(final String name,
@ -25,7 +22,6 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
// If a container has an accepts attribute, then it is always open
this.open = !accepts.isEmpty();
this.items = new HashSet<>(items);
this.accepts = new ArrayList<>(accepts);
}
public String getContents() {
@ -48,10 +44,6 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
return this.items.contains(item);
}
public Iterable<String> items() {
return Collections.unmodifiableSet(items);
}
public boolean isOpen() {
return open;
}
@ -67,13 +59,4 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
throw new IllegalStateException("Unexpected value: " + type);
}
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

@ -3,12 +3,11 @@ 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.*;
/* Generic object, everything inherits from this*/
public abstract class ZorkObject implements Triggerable {
public abstract class ZorkObject {
private final String name;
private final String description;
private final List<ZorkTrigger> trigger;
@ -45,10 +44,6 @@ public abstract class ZorkObject implements Triggerable {
return description;
}
public List<ZorkTrigger> getTrigger() {
return trigger;
}
public boolean evaluateTriggers(final ZorkGame game, final String input) {
boolean skip = false;
final Iterator<ZorkTrigger> iterator = trigger.iterator();

View file

@ -4,7 +4,6 @@ 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.*;
@ -66,11 +65,10 @@ 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);
final boolean items = ZorkTrigger.evaluateTriggerCollection(item, ITEM, game, input);
final boolean creatures = ZorkTrigger.evaluateTriggerCollection(creature, CREATURE, game, input);
final boolean containers = ZorkTrigger.evaluateTriggerCollection(container, CONTAINER, game, input);
return super.evaluateTriggers(game, input) || items || creatures || containers;
}

View file

@ -1,21 +0,0 @@
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

@ -7,8 +7,8 @@ import java.util.Iterator;
public class ZorkMap<T extends ZorkObject> extends HashMap<String, T> implements Iterable<T> {
public T put(T object) {
return put(object.getName(), object);
public void put(T object) {
put(object.getName(), object);
}
@Override