actions print on stream
This commit is contained in:
parent
6341503a70
commit
d2ce23fead
18 changed files with 93 additions and 38 deletions
|
@ -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)) {
|
||||
|
|
|
@ -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<String> arguments);
|
||||
boolean run(final ZorkGame game, final List<String> arguments, PrintStream stream);
|
||||
}
|
||||
|
|
|
@ -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<Action> findAction(final List<String> arguments) {
|
||||
|
@ -44,8 +48,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)) {
|
||||
System.out.println("Error");
|
||||
if (action.isEmpty() || !action.get().run(game, arguments, stream)) {
|
||||
stream.println("Error");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) {
|
||||
final String object = arguments.get(1);
|
||||
final String destination = arguments.get(3);
|
||||
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) {
|
||||
final String object = arguments.get(1);
|
||||
|
||||
switch (game.getTypeFromLookup(object)) {
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
System.out.println("Victory!");
|
||||
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) {
|
||||
stream.println("Victory!");
|
||||
game.setGameOver();
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
|
|
|
@ -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<String> 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<String> 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;
|
||||
}
|
||||
}
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> 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;
|
||||
|
|
|
@ -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<String> arguments) {
|
||||
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) {
|
||||
final String object = arguments.get(1);
|
||||
final String newStatus = arguments.get(3);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Reference in a new issue