output stream now in zorkgame

This commit is contained in:
RaffaeleMorganti 2022-11-22 18:03:09 +01:00
parent d2ce23fead
commit 5786e58842
20 changed files with 48 additions and 57 deletions

View file

@ -18,9 +18,9 @@ public class Zork {
final PrintStream output = System.out; final PrintStream output = System.out;
public Zork(final String filename) { 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!*/ /* starting the game!*/
d.dispatch("Start at Entrance"); d.dispatch("Start at Entrance");

View file

@ -3,6 +3,7 @@ package com.github.dtschust.zork;
import com.github.dtschust.zork.objects.*; import com.github.dtschust.zork.objects.*;
import com.github.dtschust.zork.types.ZorkMap; import com.github.dtschust.zork.types.ZorkMap;
import java.io.PrintStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -11,6 +12,8 @@ import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
public class ZorkGame { public class ZorkGame {
public final PrintStream stream;
public final Set<String> inventory = new HashSet<>(); public final Set<String> inventory = new HashSet<>();
private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>(); private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>();
private final ZorkMap<ZorkItem> items = new ZorkMap<>(); private final ZorkMap<ZorkItem> items = new ZorkMap<>();
@ -20,6 +23,10 @@ public class ZorkGame {
private boolean running = false; private boolean running = false;
private String currentRoom; private String currentRoom;
public ZorkGame(PrintStream stream) {
this.stream = stream;
}
public ZorkRoom getCurrentRoom() { public ZorkRoom getCurrentRoom() {
return rooms.get(currentRoom); return rooms.get(currentRoom);
} }
@ -111,4 +118,5 @@ public class ZorkGame {
public boolean evaluateTriggers(final String currentCommand) { public boolean evaluateTriggers(final String currentCommand) {
return evaluateTriggers(this, currentCommand); return evaluateTriggers(this, currentCommand);
} }
} }

View file

@ -3,6 +3,7 @@ package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.objects.*; import com.github.dtschust.zork.objects.*;
import java.io.PrintStream;
import java.util.Map; import java.util.Map;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; import static com.github.dtschust.zork.objects.ZorkObjectTypes.*;
@ -24,8 +25,8 @@ public abstract class ZorkParser {
protected abstract Property getRootProperty(final String filename); protected abstract Property getRootProperty(final String filename);
public ZorkGame parse(final String filename) { public ZorkGame parse(final String filename, final PrintStream stream) {
ZorkGame data = new ZorkGame(); ZorkGame data = new ZorkGame(stream);
final Property rootElement = getRootProperty(filename); final Property rootElement = getRootProperty(filename);

View file

@ -2,7 +2,6 @@ package com.github.dtschust.zork.repl;
import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import java.io.PrintStream;
import java.util.List; import java.util.List;
public interface Action { public interface Action {
@ -16,5 +15,5 @@ public interface Action {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
boolean run(final ZorkGame game, final List<String> arguments, PrintStream stream); boolean run(final ZorkGame game, final List<String> arguments);
} }

View file

@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl;
import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.actions.*; import com.github.dtschust.zork.repl.actions.*;
import java.io.PrintStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -27,11 +26,9 @@ public class ActionDispatcher {
); );
private final ZorkGame game; private final ZorkGame game;
private final PrintStream stream;
public ActionDispatcher(ZorkGame game, PrintStream stream) { public ActionDispatcher(ZorkGame game) {
this.game = game; this.game = game;
this.stream = stream;
} }
private Optional<Action> findAction(final List<String> arguments) { private Optional<Action> findAction(final List<String> arguments) {
@ -48,8 +45,8 @@ public class ActionDispatcher {
final List<String> arguments = Arrays.asList(input.split(" ")); final List<String> arguments = Arrays.asList(input.split(" "));
final Optional<Action> action = findAction(arguments); final Optional<Action> action = findAction(arguments);
if (action.isEmpty() || !action.get().run(game, arguments, stream)) { if (action.isEmpty() || !action.get().run(game, arguments)) {
stream.println("Error"); game.stream.println("Error");
} }
} }

View file

@ -6,7 +6,6 @@ import com.github.dtschust.zork.types.HasSetOfCollectable;
import com.github.dtschust.zork.objects.ZorkObjectTypes; import com.github.dtschust.zork.objects.ZorkObjectTypes;
import com.github.dtschust.zork.objects.ZorkObject; import com.github.dtschust.zork.objects.ZorkObject;
import java.io.PrintStream;
import java.util.List; import java.util.List;
@ -25,7 +24,7 @@ public class AddAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String object = arguments.get(1); final String object = arguments.get(1);
final String destination = arguments.get(3); final String destination = arguments.get(3);

View file

@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkCreature; import com.github.dtschust.zork.objects.ZorkCreature;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.CREATURE; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CREATURE;
@ -24,14 +23,14 @@ public class AttackAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String tempString = arguments.get(1); final String tempString = arguments.get(1);
final String weapon = arguments.get(3); final String weapon = arguments.get(3);
if (game.getCurrentRoom().getCreature().contains(tempString)) { if (game.getCurrentRoom().getCreature().contains(tempString)) {
ZorkCreature tempCreature = (ZorkCreature) game.get(CREATURE, tempString); ZorkCreature tempCreature = (ZorkCreature) game.get(CREATURE, tempString);
if (tempCreature != null && game.inventory.contains(weapon) && tempCreature.isAttackSuccessful(game, weapon)) { 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); tempCreature.printAndExecuteActions(game);
return true; return true;
} }

View file

@ -7,7 +7,6 @@ import com.github.dtschust.zork.objects.ZorkObjectTypes;
import com.github.dtschust.zork.objects.ZorkObject; import com.github.dtschust.zork.objects.ZorkObject;
import com.github.dtschust.zork.objects.ZorkRoom; import com.github.dtschust.zork.objects.ZorkRoom;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -38,7 +37,7 @@ public class DeleteAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String object = arguments.get(1); final String object = arguments.get(1);
switch (game.getTypeFromLookup(object)) { switch (game.getTypeFromLookup(object)) {

View file

@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkRoom; import com.github.dtschust.zork.objects.ZorkRoom;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.ROOM; import static com.github.dtschust.zork.objects.ZorkObjectTypes.ROOM;
@ -21,7 +20,7 @@ public class DropItemAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1); final String what = arguments.get(1);
if (game.inventory.contains(what)) { if (game.inventory.contains(what)) {
@ -29,7 +28,7 @@ public class DropItemAction implements Action {
tempRoom.getItem().add(what); tempRoom.getItem().add(what);
game.put(ROOM, tempRoom); game.put(ROOM, tempRoom);
game.inventory.remove(what); game.inventory.remove(what);
stream.println(what + " dropped."); game.stream.println(what + " dropped.");
} else { } else {
return false; return false;
} }

View file

@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import java.io.PrintStream;
import java.util.List; import java.util.List;
/** /**
@ -21,8 +20,8 @@ public class GameOverAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
stream.println("Victory!"); game.stream.println("Victory!");
game.setGameOver(); game.setGameOver();
return true; return true;
} }

View file

@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import java.io.PrintStream;
import java.util.List; import java.util.List;
public class InventoryAction implements Action { public class InventoryAction implements Action {
@ -13,12 +12,12 @@ public class InventoryAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
if (game.inventory.isEmpty()) { if (game.inventory.isEmpty()) {
stream.println("Inventory: empty"); game.stream.println("Inventory: empty");
} else { } else {
final String output = "Inventory: " + String.join(", ", game.inventory); final String output = "Inventory: " + String.join(", ", game.inventory);
stream.println(output); game.stream.println(output);
} }
return true; return true;
} }

View file

@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.types.ZorkDirection; import com.github.dtschust.zork.types.ZorkDirection;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -26,7 +25,7 @@ public class MoveAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
// we are guaranteed to have a valid short direction name by matchesInput // we are guaranteed to have a valid short direction name by matchesInput
final ZorkDirection direction = ZorkDirection.fromShort(arguments.get(0)).orElseThrow(() -> final ZorkDirection direction = ZorkDirection.fromShort(arguments.get(0)).orElseThrow(() ->
new IllegalStateException("unreachable")); new IllegalStateException("unreachable"));
@ -34,9 +33,9 @@ public class MoveAction implements Action {
final Optional<String> roomName = game.getCurrentRoom().getBorderingRoom(direction); final Optional<String> roomName = game.getCurrentRoom().getBorderingRoom(direction);
if (roomName.isPresent() && game.changeRoom(roomName.get())) { if (roomName.isPresent() && game.changeRoom(roomName.get())) {
stream.println(game.getCurrentRoom().getDescription()); game.stream.println(game.getCurrentRoom().getDescription());
} else { } else {
stream.println("Can't go that way."); game.stream.println("Can't go that way.");
} }
return true; return true;
} }

View file

@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkContainer; import com.github.dtschust.zork.objects.ZorkContainer;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER;
@ -16,12 +15,12 @@ public class OpenAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1); final String what = arguments.get(1);
if (what.equals("exit")) { if (what.equals("exit")) {
if (game.getCurrentRoom().isExit()) { if (game.getCurrentRoom().isExit()) {
stream.println("Game Over"); game.stream.println("Game Over");
game.setGameOver(); game.setGameOver();
} else { } else {
return false; return false;
@ -31,7 +30,7 @@ public class OpenAction implements Action {
if (game.getCurrentRoom().getContainer().contains(what)) { if (game.getCurrentRoom().getContainer().contains(what)) {
tempContainer = (ZorkContainer) game.get(CONTAINER, what); tempContainer = (ZorkContainer) game.get(CONTAINER, what);
tempContainer.open(); tempContainer.open();
stream.println(tempContainer.getContents()); game.stream.println(tempContainer.getContents());
} else { } else {
return false; return false;
} }

View file

@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkContainer; import com.github.dtschust.zork.objects.ZorkContainer;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER;
@ -21,7 +20,7 @@ public class PutAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1); final String what = arguments.get(1);
final String destination = arguments.get(3); final String destination = arguments.get(3);
@ -30,7 +29,7 @@ public class PutAction implements Action {
if (tempContainer.isOpen() && game.inventory.contains(what)) { if (tempContainer.isOpen() && game.inventory.contains(what)) {
tempContainer.addItem(what); tempContainer.addItem(what);
game.inventory.remove(what); game.inventory.remove(what);
stream.println("Item " + what + " added to " + destination + "."); game.stream.println("Item " + what + " added to " + destination + ".");
return true; return true;
} }
} }

View file

@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkItem; import com.github.dtschust.zork.objects.ZorkItem;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
@ -21,12 +20,12 @@ public class ReadAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1); final String what = arguments.get(1);
if (game.inventory.contains(what)) { if (game.inventory.contains(what)) {
ZorkItem tempItem = (ZorkItem) game.get(ITEM, what); ZorkItem tempItem = (ZorkItem) game.get(ITEM, what);
stream.println(tempItem.getWriting()); game.stream.println(tempItem.getWriting());
return true; return true;
} }
return false; return false;

View file

@ -3,7 +3,6 @@ package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import java.io.PrintStream;
import java.util.List; import java.util.List;
@ -22,12 +21,12 @@ public class StartGameAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String room = arguments.get(2); final String room = arguments.get(2);
if(!game.isRunning()){ if(!game.isRunning()){
if(game.changeRoom(room)){ if(game.changeRoom(room)){
stream.println(game.getCurrentRoom().getDescription()); game.stream.println(game.getCurrentRoom().getDescription());
return true; return true;
} }
} }

View file

@ -5,7 +5,6 @@ import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkContainer; import com.github.dtschust.zork.objects.ZorkContainer;
import com.github.dtschust.zork.objects.ZorkRoom; import com.github.dtschust.zork.objects.ZorkRoom;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER; import static com.github.dtschust.zork.objects.ZorkObjectTypes.CONTAINER;
@ -23,7 +22,7 @@ public class TakeAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String tempString = arguments.get(1); final String tempString = arguments.get(1);
if ((game.getCurrentRoom()).getItem().contains(tempString)) { if ((game.getCurrentRoom()).getItem().contains(tempString)) {
@ -31,7 +30,7 @@ public class TakeAction implements Action {
ZorkRoom tempRoom = (game.getCurrentRoom()); ZorkRoom tempRoom = (game.getCurrentRoom());
tempRoom.getItem().remove(tempString); tempRoom.getItem().remove(tempString);
game.put(ROOM, tempRoom); game.put(ROOM, tempRoom);
stream.println("Item " + tempString + " added to inventory."); game.stream.println("Item " + tempString + " added to inventory.");
return true; return true;
} else { } else {
/* Search all containers in the current room for the item! */ /* Search all containers in the current room for the item! */
@ -41,7 +40,7 @@ public class TakeAction implements Action {
game.inventory.add(tempString); game.inventory.add(tempString);
tempContainer.removeItem(tempString); tempContainer.removeItem(tempString);
game.put(CONTAINER, tempContainer); game.put(CONTAINER, tempContainer);
stream.println("Item " + tempString + " added to inventory."); game.stream.println("Item " + tempString + " added to inventory.");
return true; return true;
} }
} }

View file

@ -4,7 +4,6 @@ import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkItem; import com.github.dtschust.zork.objects.ZorkItem;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
@ -24,12 +23,12 @@ public class TurnOnAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(2); final String what = arguments.get(2);
if (game.inventory.contains(what)) { if (game.inventory.contains(what)) {
ZorkItem tempItem = (ZorkItem) game.get(ITEM, what); ZorkItem tempItem = (ZorkItem) game.get(ITEM, what);
stream.println("You activate the " + what + "."); game.stream.println("You activate the " + what + ".");
if (tempItem != null) { if (tempItem != null) {
tempItem.printAndExecuteActions(game); tempItem.printAndExecuteActions(game);
return true; return true;

View file

@ -5,7 +5,6 @@ import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.types.ZorkMap; import com.github.dtschust.zork.types.ZorkMap;
import com.github.dtschust.zork.objects.ZorkObject; import com.github.dtschust.zork.objects.ZorkObject;
import java.io.PrintStream;
import java.util.List; import java.util.List;
/** /**
@ -23,7 +22,7 @@ public class UpdateAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) { public boolean run(ZorkGame game, List<String> arguments) {
final String object = arguments.get(1); final String object = arguments.get(1);
final String newStatus = arguments.get(3); final String newStatus = arguments.get(3);

View file

@ -12,9 +12,9 @@ public interface HasPrintsAndActions {
default void printAndExecuteActions(final ZorkGame game) { default void printAndExecuteActions(final ZorkGame game) {
for (final String print : getPrints()) { 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()) { for (final String action : getActions()) {
effectsDispatcher.dispatch(action); effectsDispatcher.dispatch(action);
} }