From aafbf94434fd3785e7cf5d79595d9acd51753639 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 22 Nov 2022 15:20:35 +0100 Subject: [PATCH] Minor cleanup --- .../java/com/github/dtschust/zork/Zork.java | 9 +++----- .../com/github/dtschust/zork/ZorkGame.java | 8 +++---- .../com/github/dtschust/zork/ZorkTrigger.java | 12 +++++++++++ .../dtschust/zork/objects/ZorkContainer.java | 17 --------------- .../dtschust/zork/objects/ZorkObject.java | 7 +------ .../dtschust/zork/objects/ZorkRoom.java | 8 +++---- .../dtschust/zork/types/Triggerable.java | 21 ------------------- .../github/dtschust/zork/types/ZorkMap.java | 4 ++-- 8 files changed, 24 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/com/github/dtschust/zork/types/Triggerable.java diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index b3767d0..0f176d6 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -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(""); } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkGame.java b/src/main/java/com/github/dtschust/zork/ZorkGame.java index 30e8b97..02d2dc7 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkGame.java +++ b/src/main/java/com/github/dtschust/zork/ZorkGame.java @@ -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 inventory = new HashSet<>(); private final ZorkMap 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; } diff --git a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java index 77cb8ed..7c71867 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java +++ b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java @@ -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 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(); } diff --git a/src/main/java/com/github/dtschust/zork/objects/ZorkContainer.java b/src/main/java/com/github/dtschust/zork/objects/ZorkContainer.java index 9891476..1a4ba40 100644 --- a/src/main/java/com/github/dtschust/zork/objects/ZorkContainer.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkContainer.java @@ -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 items; - private final List 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 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 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; - } } diff --git a/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java b/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java index b8aa90e..dfc2cc7 100644 --- a/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java @@ -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 trigger; @@ -45,10 +44,6 @@ public abstract class ZorkObject implements Triggerable { return description; } - public List getTrigger() { - return trigger; - } - public boolean evaluateTriggers(final ZorkGame game, final String input) { boolean skip = false; final Iterator iterator = trigger.iterator(); 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 4129417..3c17659 100644 --- a/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java @@ -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; } diff --git a/src/main/java/com/github/dtschust/zork/types/Triggerable.java b/src/main/java/com/github/dtschust/zork/types/Triggerable.java deleted file mode 100644 index 3ad66e5..0000000 --- a/src/main/java/com/github/dtschust/zork/types/Triggerable.java +++ /dev/null @@ -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 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); - } -} diff --git a/src/main/java/com/github/dtschust/zork/types/ZorkMap.java b/src/main/java/com/github/dtschust/zork/types/ZorkMap.java index 63c0acb..81e8bc4 100644 --- a/src/main/java/com/github/dtschust/zork/types/ZorkMap.java +++ b/src/main/java/com/github/dtschust/zork/types/ZorkMap.java @@ -7,8 +7,8 @@ import java.util.Iterator; public class ZorkMap extends HashMap implements Iterable { - public T put(T object) { - return put(object.getName(), object); + public void put(T object) { + put(object.getName(), object); } @Override