output stream now in zorkgame
This commit is contained in:
parent
d2ce23fead
commit
5786e58842
20 changed files with 48 additions and 57 deletions
|
@ -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");
|
||||
|
|
|
@ -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<String> inventory = new HashSet<>();
|
||||
private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>();
|
||||
private final ZorkMap<ZorkItem> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream);
|
||||
boolean run(final ZorkGame game, final List<String> arguments);
|
||||
}
|
||||
|
|
|
@ -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<Action> findAction(final List<String> arguments) {
|
||||
|
@ -48,8 +45,8 @@ public class ActionDispatcher {
|
|||
final List<String> arguments = Arrays.asList(input.split(" "));
|
||||
final Optional<Action> 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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> arguments) {
|
||||
final String object = arguments.get(1);
|
||||
final String destination = arguments.get(3);
|
||||
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> arguments) {
|
||||
final String object = arguments.get(1);
|
||||
|
||||
switch (game.getTypeFromLookup(object)) {
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
stream.println("Victory!");
|
||||
public boolean run(ZorkGame game, List<String> arguments) {
|
||||
game.stream.println("Victory!");
|
||||
game.setGameOver();
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
|
|
|
@ -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<String> arguments, PrintStream stream) {
|
||||
public boolean run(ZorkGame game, List<String> arguments) {
|
||||
final String object = arguments.get(1);
|
||||
final String newStatus = arguments.get(3);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Reference in a new issue