From 5786e58842dbacbbaea57a726fc4e0caf2ca75ee Mon Sep 17 00:00:00 2001 From: RaffaeleMorganti Date: Tue, 22 Nov 2022 18:03:09 +0100 Subject: [PATCH] output stream now in zorkgame --- src/main/java/com/github/dtschust/zork/Zork.java | 4 ++-- src/main/java/com/github/dtschust/zork/ZorkGame.java | 8 ++++++++ .../java/com/github/dtschust/zork/parser/ZorkParser.java | 5 +++-- src/main/java/com/github/dtschust/zork/repl/Action.java | 3 +-- .../com/github/dtschust/zork/repl/ActionDispatcher.java | 9 +++------ .../com/github/dtschust/zork/repl/actions/AddAction.java | 3 +-- .../github/dtschust/zork/repl/actions/AttackAction.java | 5 ++--- .../github/dtschust/zork/repl/actions/DeleteAction.java | 3 +-- .../dtschust/zork/repl/actions/DropItemAction.java | 5 ++--- .../dtschust/zork/repl/actions/GameOverAction.java | 5 ++--- .../dtschust/zork/repl/actions/InventoryAction.java | 7 +++---- .../github/dtschust/zork/repl/actions/MoveAction.java | 7 +++---- .../github/dtschust/zork/repl/actions/OpenAction.java | 7 +++---- .../com/github/dtschust/zork/repl/actions/PutAction.java | 5 ++--- .../github/dtschust/zork/repl/actions/ReadAction.java | 5 ++--- .../dtschust/zork/repl/actions/StartGameAction.java | 5 ++--- .../github/dtschust/zork/repl/actions/TakeAction.java | 7 +++---- .../github/dtschust/zork/repl/actions/TurnOnAction.java | 5 ++--- .../github/dtschust/zork/repl/actions/UpdateAction.java | 3 +-- .../github/dtschust/zork/types/HasPrintsAndActions.java | 4 ++-- 20 files changed, 48 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index 04fd829..16bc715 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -18,9 +18,9 @@ public class Zork { final PrintStream output = System.out; public Zork(final String filename) { - game = ParserIOC.xmlParser().parse(filename); + game = ParserIOC.xmlParser().parse(filename, output); - ActionDispatcher d = new ActionDispatcher(game, output); + ActionDispatcher d = new ActionDispatcher(game); /* starting the game!*/ d.dispatch("Start at Entrance"); diff --git a/src/main/java/com/github/dtschust/zork/ZorkGame.java b/src/main/java/com/github/dtschust/zork/ZorkGame.java index 02d2dc7..82b28d1 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkGame.java +++ b/src/main/java/com/github/dtschust/zork/ZorkGame.java @@ -3,6 +3,7 @@ package com.github.dtschust.zork; import com.github.dtschust.zork.objects.*; import com.github.dtschust.zork.types.ZorkMap; +import java.io.PrintStream; import java.util.HashMap; import java.util.HashSet; import java.util.Set; @@ -11,6 +12,8 @@ import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; public class ZorkGame { + public final PrintStream stream; + public final Set inventory = new HashSet<>(); private final ZorkMap rooms = new ZorkMap<>(); private final ZorkMap items = new ZorkMap<>(); @@ -20,6 +23,10 @@ public class ZorkGame { private boolean running = false; private String currentRoom; + public ZorkGame(PrintStream stream) { + this.stream = stream; + } + public ZorkRoom getCurrentRoom() { return rooms.get(currentRoom); } @@ -111,4 +118,5 @@ public class ZorkGame { public boolean evaluateTriggers(final String currentCommand) { return evaluateTriggers(this, currentCommand); } + } diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java b/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java index 42ecc1a..84959c6 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkParser.java @@ -3,6 +3,7 @@ package com.github.dtschust.zork.parser; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.objects.*; +import java.io.PrintStream; import java.util.Map; import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; @@ -24,8 +25,8 @@ public abstract class ZorkParser { protected abstract Property getRootProperty(final String filename); - public ZorkGame parse(final String filename) { - ZorkGame data = new ZorkGame(); + public ZorkGame parse(final String filename, final PrintStream stream) { + ZorkGame data = new ZorkGame(stream); final Property rootElement = getRootProperty(filename); diff --git a/src/main/java/com/github/dtschust/zork/repl/Action.java b/src/main/java/com/github/dtschust/zork/repl/Action.java index 9d5d0e9..4da1d62 100644 --- a/src/main/java/com/github/dtschust/zork/repl/Action.java +++ b/src/main/java/com/github/dtschust/zork/repl/Action.java @@ -2,7 +2,6 @@ package com.github.dtschust.zork.repl; import com.github.dtschust.zork.ZorkGame; -import java.io.PrintStream; import java.util.List; public interface Action { @@ -16,5 +15,5 @@ public interface Action { return Integer.MAX_VALUE; } - boolean run(final ZorkGame game, final List arguments, PrintStream stream); + boolean run(final ZorkGame game, final List arguments); } diff --git a/src/main/java/com/github/dtschust/zork/repl/ActionDispatcher.java b/src/main/java/com/github/dtschust/zork/repl/ActionDispatcher.java index 37326e9..4c3ea76 100644 --- a/src/main/java/com/github/dtschust/zork/repl/ActionDispatcher.java +++ b/src/main/java/com/github/dtschust/zork/repl/ActionDispatcher.java @@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.actions.*; -import java.io.PrintStream; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -27,11 +26,9 @@ public class ActionDispatcher { ); private final ZorkGame game; - private final PrintStream stream; - public ActionDispatcher(ZorkGame game, PrintStream stream) { + public ActionDispatcher(ZorkGame game) { this.game = game; - this.stream = stream; } private Optional findAction(final List arguments) { @@ -48,8 +45,8 @@ public class ActionDispatcher { final List arguments = Arrays.asList(input.split(" ")); final Optional action = findAction(arguments); - if (action.isEmpty() || !action.get().run(game, arguments, stream)) { - stream.println("Error"); + if (action.isEmpty() || !action.get().run(game, arguments)) { + game.stream.println("Error"); } } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java index f47ca08..24ccc30 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/AddAction.java @@ -6,7 +6,6 @@ import com.github.dtschust.zork.types.HasSetOfCollectable; import com.github.dtschust.zork.objects.ZorkObjectTypes; import com.github.dtschust.zork.objects.ZorkObject; -import java.io.PrintStream; import java.util.List; @@ -25,7 +24,7 @@ public class AddAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String object = arguments.get(1); final String destination = arguments.get(3); diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java index ad696f2..fdb68b8 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/AttackAction.java @@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.objects.ZorkCreature; -import java.io.PrintStream; import java.util.List; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CREATURE; @@ -24,14 +23,14 @@ public class AttackAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String tempString = arguments.get(1); final String weapon = arguments.get(3); if (game.getCurrentRoom().getCreature().contains(tempString)) { ZorkCreature tempCreature = (ZorkCreature) game.get(CREATURE, tempString); if (tempCreature != null && game.inventory.contains(weapon) && tempCreature.isAttackSuccessful(game, weapon)) { - stream.println("You assault the " + tempString + " with the " + weapon + "."); + game.stream.println("You assault the " + tempString + " with the " + weapon + "."); tempCreature.printAndExecuteActions(game); return true; } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java index ae2cb11..904f6e2 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java @@ -7,7 +7,6 @@ import com.github.dtschust.zork.objects.ZorkObjectTypes; import com.github.dtschust.zork.objects.ZorkObject; import com.github.dtschust.zork.objects.ZorkRoom; -import java.io.PrintStream; import java.util.List; import java.util.Set; @@ -38,7 +37,7 @@ public class DeleteAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String object = arguments.get(1); switch (game.getTypeFromLookup(object)) { diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java index dcea0e8..c9875da 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/DropItemAction.java @@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.objects.ZorkRoom; -import java.io.PrintStream; import java.util.List; import static com.github.dtschust.zork.objects.ZorkObjectTypes.ROOM; @@ -21,7 +20,7 @@ public class DropItemAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String what = arguments.get(1); if (game.inventory.contains(what)) { @@ -29,7 +28,7 @@ public class DropItemAction implements Action { tempRoom.getItem().add(what); game.put(ROOM, tempRoom); game.inventory.remove(what); - stream.println(what + " dropped."); + game.stream.println(what + " dropped."); } else { return false; } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/GameOverAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/GameOverAction.java index a5c600f..789956d 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/GameOverAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/GameOverAction.java @@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import java.io.PrintStream; import java.util.List; /** @@ -21,8 +20,8 @@ public class GameOverAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { - stream.println("Victory!"); + public boolean run(ZorkGame game, List arguments) { + game.stream.println("Victory!"); game.setGameOver(); return true; } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/InventoryAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/InventoryAction.java index d6241d4..fffc996 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/InventoryAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/InventoryAction.java @@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import java.io.PrintStream; import java.util.List; public class InventoryAction implements Action { @@ -13,12 +12,12 @@ public class InventoryAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { if (game.inventory.isEmpty()) { - stream.println("Inventory: empty"); + game.stream.println("Inventory: empty"); } else { final String output = "Inventory: " + String.join(", ", game.inventory); - stream.println(output); + game.stream.println(output); } return true; } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/MoveAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/MoveAction.java index f3161ba..4ef8dcf 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/MoveAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/MoveAction.java @@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.types.ZorkDirection; -import java.io.PrintStream; import java.util.List; import java.util.Optional; @@ -26,7 +25,7 @@ public class MoveAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { // we are guaranteed to have a valid short direction name by matchesInput final ZorkDirection direction = ZorkDirection.fromShort(arguments.get(0)).orElseThrow(() -> new IllegalStateException("unreachable")); @@ -34,9 +33,9 @@ public class MoveAction implements Action { final Optional roomName = game.getCurrentRoom().getBorderingRoom(direction); if (roomName.isPresent() && game.changeRoom(roomName.get())) { - stream.println(game.getCurrentRoom().getDescription()); + game.stream.println(game.getCurrentRoom().getDescription()); } else { - stream.println("Can't go that way."); + game.stream.println("Can't go that way."); } return true; } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java index aa81216..551a952 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/OpenAction.java @@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.objects.ZorkContainer; -import java.io.PrintStream; import java.util.List; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; @@ -16,12 +15,12 @@ public class OpenAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String what = arguments.get(1); if (what.equals("exit")) { if (game.getCurrentRoom().isExit()) { - stream.println("Game Over"); + game.stream.println("Game Over"); game.setGameOver(); } else { return false; @@ -31,7 +30,7 @@ public class OpenAction implements Action { if (game.getCurrentRoom().getContainer().contains(what)) { tempContainer = (ZorkContainer) game.get(CONTAINER, what); tempContainer.open(); - stream.println(tempContainer.getContents()); + game.stream.println(tempContainer.getContents()); } else { return false; } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java index a9f2583..de7c80b 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java @@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.objects.ZorkContainer; -import java.io.PrintStream; import java.util.List; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; @@ -21,7 +20,7 @@ public class PutAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String what = arguments.get(1); final String destination = arguments.get(3); @@ -30,7 +29,7 @@ public class PutAction implements Action { if (tempContainer.isOpen() && game.inventory.contains(what)) { tempContainer.addItem(what); game.inventory.remove(what); - stream.println("Item " + what + " added to " + destination + "."); + game.stream.println("Item " + what + " added to " + destination + "."); return true; } } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java index bffc95b..99736a0 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java @@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.objects.ZorkItem; -import java.io.PrintStream; import java.util.List; import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; @@ -21,12 +20,12 @@ public class ReadAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String what = arguments.get(1); if (game.inventory.contains(what)) { ZorkItem tempItem = (ZorkItem) game.get(ITEM, what); - stream.println(tempItem.getWriting()); + game.stream.println(tempItem.getWriting()); return true; } return false; 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 299e1b2..ed5f8ee 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 @@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import java.io.PrintStream; import java.util.List; @@ -22,12 +21,12 @@ public class StartGameAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String room = arguments.get(2); if(!game.isRunning()){ if(game.changeRoom(room)){ - stream.println(game.getCurrentRoom().getDescription()); + game.stream.println(game.getCurrentRoom().getDescription()); return true; } } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java index 7cd4091..016fb7b 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java @@ -5,7 +5,6 @@ import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.objects.ZorkContainer; import com.github.dtschust.zork.objects.ZorkRoom; -import java.io.PrintStream; import java.util.List; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; @@ -23,7 +22,7 @@ public class TakeAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String tempString = arguments.get(1); if ((game.getCurrentRoom()).getItem().contains(tempString)) { @@ -31,7 +30,7 @@ public class TakeAction implements Action { ZorkRoom tempRoom = (game.getCurrentRoom()); tempRoom.getItem().remove(tempString); game.put(ROOM, tempRoom); - stream.println("Item " + tempString + " added to inventory."); + game.stream.println("Item " + tempString + " added to inventory."); return true; } else { /* Search all containers in the current room for the item! */ @@ -41,7 +40,7 @@ public class TakeAction implements Action { game.inventory.add(tempString); tempContainer.removeItem(tempString); game.put(CONTAINER, tempContainer); - stream.println("Item " + tempString + " added to inventory."); + game.stream.println("Item " + tempString + " added to inventory."); return true; } } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java index bfd0010..14986c9 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/TurnOnAction.java @@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.objects.ZorkItem; -import java.io.PrintStream; import java.util.List; import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; @@ -24,12 +23,12 @@ public class TurnOnAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String what = arguments.get(2); if (game.inventory.contains(what)) { ZorkItem tempItem = (ZorkItem) game.get(ITEM, what); - stream.println("You activate the " + what + "."); + game.stream.println("You activate the " + what + "."); if (tempItem != null) { tempItem.printAndExecuteActions(game); return true; diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java index 99a6222..baec12c 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/UpdateAction.java @@ -5,7 +5,6 @@ import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.types.ZorkMap; import com.github.dtschust.zork.objects.ZorkObject; -import java.io.PrintStream; import java.util.List; /** @@ -23,7 +22,7 @@ public class UpdateAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments, PrintStream stream) { + public boolean run(ZorkGame game, List arguments) { final String object = arguments.get(1); final String newStatus = arguments.get(3); diff --git a/src/main/java/com/github/dtschust/zork/types/HasPrintsAndActions.java b/src/main/java/com/github/dtschust/zork/types/HasPrintsAndActions.java index e20de25..923a8b3 100644 --- a/src/main/java/com/github/dtschust/zork/types/HasPrintsAndActions.java +++ b/src/main/java/com/github/dtschust/zork/types/HasPrintsAndActions.java @@ -12,9 +12,9 @@ public interface HasPrintsAndActions { default void printAndExecuteActions(final ZorkGame game) { for (final String print : getPrints()) { - System.out.println(print); + game.stream.println(print); } - final ActionDispatcher effectsDispatcher = new ActionDispatcher(game, System.out); + final ActionDispatcher effectsDispatcher = new ActionDispatcher(game); for (final String action : getActions()) { effectsDispatcher.dispatch(action); }