From d2ce23fead3e342148dc12afb24617396af525f1 Mon Sep 17 00:00:00 2001 From: RaffaeleMorganti Date: Tue, 22 Nov 2022 17:25:40 +0100 Subject: [PATCH] actions print on stream --- .../java/com/github/dtschust/zork/Zork.java | 13 +++---- .../com/github/dtschust/zork/repl/Action.java | 3 +- .../dtschust/zork/repl/ActionDispatcher.java | 10 ++++-- .../dtschust/zork/repl/actions/AddAction.java | 3 +- .../zork/repl/actions/AttackAction.java | 5 +-- .../zork/repl/actions/DeleteAction.java | 3 +- .../zork/repl/actions/DropItemAction.java | 5 +-- .../zork/repl/actions/GameOverAction.java | 5 +-- .../zork/repl/actions/InventoryAction.java | 7 ++-- .../zork/repl/actions/MoveAction.java | 7 ++-- .../zork/repl/actions/OpenAction.java | 7 ++-- .../dtschust/zork/repl/actions/PutAction.java | 5 +-- .../zork/repl/actions/ReadAction.java | 5 +-- .../zork/repl/actions/StartGameAction.java | 36 +++++++++++++++++++ .../zork/repl/actions/TakeAction.java | 7 ++-- .../zork/repl/actions/TurnOnAction.java | 5 +-- .../zork/repl/actions/UpdateAction.java | 3 +- .../zork/types/HasPrintsAndActions.java | 2 +- 18 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index 0f176d6..04fd829 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -8,25 +8,26 @@ 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 source = new Scanner(System.in); + final Scanner input = new Scanner(System.in); + final PrintStream output = System.out; public Zork(final String filename) { game = ParserIOC.xmlParser().parse(filename); - game.changeRoom("Entrance"); - /* Print out the first entrance description, starting the game!*/ - System.out.println(game.getCurrentRoom().getDescription()); + ActionDispatcher d = new ActionDispatcher(game, output); - ActionDispatcher d = new ActionDispatcher(game); + /* starting the game!*/ + d.dispatch("Start at Entrance"); /* There is no stopping in Zork, until we're done!!*/ while (game.isRunning()) { - String userInput = source.nextLine(); + String userInput = input.nextLine(); /*Now that we have the user command, check the input*/ if (!game.evaluateTriggers(userInput)) { 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 4da1d62..9d5d0e9 100644 --- a/src/main/java/com/github/dtschust/zork/repl/Action.java +++ b/src/main/java/com/github/dtschust/zork/repl/Action.java @@ -2,6 +2,7 @@ package com.github.dtschust.zork.repl; import com.github.dtschust.zork.ZorkGame; +import java.io.PrintStream; import java.util.List; public interface Action { @@ -15,5 +16,5 @@ public interface Action { return Integer.MAX_VALUE; } - boolean run(final ZorkGame game, final List arguments); + boolean run(final ZorkGame game, final List arguments, PrintStream stream); } 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 3a485a3..37326e9 100644 --- a/src/main/java/com/github/dtschust/zork/repl/ActionDispatcher.java +++ b/src/main/java/com/github/dtschust/zork/repl/ActionDispatcher.java @@ -3,6 +3,7 @@ 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; @@ -19,15 +20,18 @@ public class ActionDispatcher { new OpenAction(), new PutAction(), new ReadAction(), + new StartGameAction(), new TakeAction(), new TurnOnAction(), new UpdateAction() ); private final ZorkGame game; + private final PrintStream stream; - public ActionDispatcher(ZorkGame game) { + public ActionDispatcher(ZorkGame game, PrintStream stream) { this.game = game; + this.stream = stream; } private Optional findAction(final List arguments) { @@ -44,8 +48,8 @@ public class ActionDispatcher { final List arguments = Arrays.asList(input.split(" ")); final Optional action = findAction(arguments); - if (action.isEmpty() || !action.get().run(game, arguments)) { - System.out.println("Error"); + if (action.isEmpty() || !action.get().run(game, arguments, stream)) { + 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 24ccc30..f47ca08 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,6 +6,7 @@ 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; @@ -24,7 +25,7 @@ public class AddAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { 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 1d19454..ad696f2 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,6 +4,7 @@ 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; @@ -23,14 +24,14 @@ public class AttackAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { 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)) { - System.out.println("You assault the " + tempString + " with the " + weapon + "."); + 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 904f6e2..ae2cb11 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,6 +7,7 @@ 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; @@ -37,7 +38,7 @@ public class DeleteAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { 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 4719738..dcea0e8 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,6 +4,7 @@ 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; @@ -20,7 +21,7 @@ public class DropItemAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { final String what = arguments.get(1); if (game.inventory.contains(what)) { @@ -28,7 +29,7 @@ public class DropItemAction implements Action { tempRoom.getItem().add(what); game.put(ROOM, tempRoom); game.inventory.remove(what); - System.out.println(what + " dropped."); + 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 87e1d59..a5c600f 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,6 +3,7 @@ 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; /** @@ -20,8 +21,8 @@ public class GameOverAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { - System.out.println("Victory!"); + public boolean run(ZorkGame game, List arguments, PrintStream stream) { + 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 3521ad3..d6241d4 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,6 +3,7 @@ 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 { @@ -12,12 +13,12 @@ public class InventoryAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { if (game.inventory.isEmpty()) { - System.out.println("Inventory: empty"); + stream.println("Inventory: empty"); } else { final String output = "Inventory: " + String.join(", ", game.inventory); - System.out.println(output); + 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 831ea93..f3161ba 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,6 +4,7 @@ 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; @@ -25,7 +26,7 @@ public class MoveAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { // we are guaranteed to have a valid short direction name by matchesInput final ZorkDirection direction = ZorkDirection.fromShort(arguments.get(0)).orElseThrow(() -> new IllegalStateException("unreachable")); @@ -33,9 +34,9 @@ public class MoveAction implements Action { final Optional roomName = game.getCurrentRoom().getBorderingRoom(direction); if (roomName.isPresent() && game.changeRoom(roomName.get())) { - System.out.println(game.getCurrentRoom().getDescription()); + stream.println(game.getCurrentRoom().getDescription()); } else { - System.out.println("Can't go that way."); + 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 8e5537c..aa81216 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,6 +4,7 @@ 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; @@ -15,12 +16,12 @@ public class OpenAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { final String what = arguments.get(1); if (what.equals("exit")) { if (game.getCurrentRoom().isExit()) { - System.out.println("Game Over"); + stream.println("Game Over"); game.setGameOver(); } else { return false; @@ -30,7 +31,7 @@ public class OpenAction implements Action { if (game.getCurrentRoom().getContainer().contains(what)) { tempContainer = (ZorkContainer) game.get(CONTAINER, what); tempContainer.open(); - System.out.println(tempContainer.getContents()); + 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 025e632..a9f2583 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,6 +4,7 @@ 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; @@ -20,7 +21,7 @@ public class PutAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { final String what = arguments.get(1); final String destination = arguments.get(3); @@ -29,7 +30,7 @@ public class PutAction implements Action { if (tempContainer.isOpen() && game.inventory.contains(what)) { tempContainer.addItem(what); game.inventory.remove(what); - System.out.println("Item " + what + " added to " + destination + "."); + 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 c736c6c..bffc95b 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,6 +4,7 @@ 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; @@ -20,12 +21,12 @@ public class ReadAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { final String what = arguments.get(1); if (game.inventory.contains(what)) { ZorkItem tempItem = (ZorkItem) game.get(ITEM, what); - System.out.println(tempItem.getWriting()); + 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 new file mode 100644 index 0000000..299e1b2 --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java @@ -0,0 +1,36 @@ +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; + + +/** + * Add: figure out what type the destination is, then what type the object is. Then add object to destination if it makes sense + */ +public class StartGameAction implements Action { + @Override + public boolean matchesInput(List arguments) { + return arguments.get(0).equals("Start") && arguments.get(1).equals("at"); + } + + @Override + public int getMinimumArgCount() { + return 3; + } + + @Override + public boolean run(ZorkGame game, List arguments, PrintStream stream) { + final String room = arguments.get(2); + + if(!game.isRunning()){ + if(game.changeRoom(room)){ + stream.println(game.getCurrentRoom().getDescription()); + return true; + } + } + return false; + } +} 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 c1ab8b1..7cd4091 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,6 +5,7 @@ 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; @@ -22,7 +23,7 @@ public class TakeAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { final String tempString = arguments.get(1); if ((game.getCurrentRoom()).getItem().contains(tempString)) { @@ -30,7 +31,7 @@ public class TakeAction implements Action { ZorkRoom tempRoom = (game.getCurrentRoom()); tempRoom.getItem().remove(tempString); game.put(ROOM, tempRoom); - System.out.println("Item " + tempString + " added to inventory."); + stream.println("Item " + tempString + " added to inventory."); return true; } else { /* Search all containers in the current room for the item! */ @@ -40,7 +41,7 @@ public class TakeAction implements Action { game.inventory.add(tempString); tempContainer.removeItem(tempString); game.put(CONTAINER, tempContainer); - System.out.println("Item " + tempString + " added to inventory."); + 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 f5f32da..bfd0010 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,6 +4,7 @@ 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; @@ -23,12 +24,12 @@ public class TurnOnAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { final String what = arguments.get(2); if (game.inventory.contains(what)) { ZorkItem tempItem = (ZorkItem) game.get(ITEM, what); - System.out.println("You activate the " + what + "."); + 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 baec12c..99a6222 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,6 +5,7 @@ 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; /** @@ -22,7 +23,7 @@ public class UpdateAction implements Action { } @Override - public boolean run(ZorkGame game, List arguments) { + public boolean run(ZorkGame game, List arguments, PrintStream stream) { 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 9c4e430..e20de25 100644 --- a/src/main/java/com/github/dtschust/zork/types/HasPrintsAndActions.java +++ b/src/main/java/com/github/dtschust/zork/types/HasPrintsAndActions.java @@ -14,7 +14,7 @@ public interface HasPrintsAndActions { for (final String print : getPrints()) { System.out.println(print); } - final ActionDispatcher effectsDispatcher = new ActionDispatcher(game); + final ActionDispatcher effectsDispatcher = new ActionDispatcher(game, System.out); for (final String action : getActions()) { effectsDispatcher.dispatch(action); }