actions print on stream

This commit is contained in:
RaffaeleMorganti 2022-11-22 17:25:40 +01:00
parent 6341503a70
commit d2ce23fead
18 changed files with 93 additions and 38 deletions

View file

@ -8,25 +8,26 @@ package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ParserIOC; import com.github.dtschust.zork.parser.ParserIOC;
import com.github.dtschust.zork.repl.ActionDispatcher; import com.github.dtschust.zork.repl.ActionDispatcher;
import java.io.PrintStream;
import java.util.Scanner; import java.util.Scanner;
/* And away we go*/ /* And away we go*/
public class Zork { public class Zork {
final ZorkGame game; 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) { public Zork(final String filename) {
game = ParserIOC.xmlParser().parse(filename); game = ParserIOC.xmlParser().parse(filename);
game.changeRoom("Entrance"); ActionDispatcher d = new ActionDispatcher(game, output);
/* Print out the first entrance description, starting the game!*/
System.out.println(game.getCurrentRoom().getDescription());
ActionDispatcher d = new ActionDispatcher(game); /* starting the game!*/
d.dispatch("Start at Entrance");
/* There is no stopping in Zork, until we're done!!*/ /* There is no stopping in Zork, until we're done!!*/
while (game.isRunning()) { while (game.isRunning()) {
String userInput = source.nextLine(); String userInput = input.nextLine();
/*Now that we have the user command, check the input*/ /*Now that we have the user command, check the input*/
if (!game.evaluateTriggers(userInput)) { if (!game.evaluateTriggers(userInput)) {

View file

@ -2,6 +2,7 @@ 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 {
@ -15,5 +16,5 @@ public interface Action {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
boolean run(final ZorkGame game, final List<String> arguments); boolean run(final ZorkGame game, final List<String> arguments, PrintStream stream);
} }

View file

@ -3,6 +3,7 @@ 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;
@ -19,15 +20,18 @@ public class ActionDispatcher {
new OpenAction(), new OpenAction(),
new PutAction(), new PutAction(),
new ReadAction(), new ReadAction(),
new StartGameAction(),
new TakeAction(), new TakeAction(),
new TurnOnAction(), new TurnOnAction(),
new UpdateAction() new UpdateAction()
); );
private final ZorkGame game; private final ZorkGame game;
private final PrintStream stream;
public ActionDispatcher(ZorkGame game) { public ActionDispatcher(ZorkGame game, PrintStream stream) {
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) {
@ -44,8 +48,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)) { if (action.isEmpty() || !action.get().run(game, arguments, stream)) {
System.out.println("Error"); stream.println("Error");
} }
} }

View file

@ -6,6 +6,7 @@ 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;
@ -24,7 +25,7 @@ public class AddAction implements Action {
} }
@Override @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 object = arguments.get(1);
final String destination = arguments.get(3); final String destination = arguments.get(3);

View file

@ -4,6 +4,7 @@ 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;
@ -23,14 +24,14 @@ public class AttackAction implements Action {
} }
@Override @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 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)) {
System.out.println("You assault the " + tempString + " with the " + weapon + "."); stream.println("You assault the " + tempString + " with the " + weapon + ".");
tempCreature.printAndExecuteActions(game); tempCreature.printAndExecuteActions(game);
return true; return true;
} }

View file

@ -7,6 +7,7 @@ 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;
@ -37,7 +38,7 @@ public class DeleteAction implements Action {
} }
@Override @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 object = arguments.get(1);
switch (game.getTypeFromLookup(object)) { switch (game.getTypeFromLookup(object)) {

View file

@ -4,6 +4,7 @@ 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;
@ -20,7 +21,7 @@ public class DropItemAction implements Action {
} }
@Override @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 what = arguments.get(1);
if (game.inventory.contains(what)) { if (game.inventory.contains(what)) {
@ -28,7 +29,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);
System.out.println(what + " dropped."); stream.println(what + " dropped.");
} else { } else {
return false; return false;
} }

View file

@ -3,6 +3,7 @@ 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;
/** /**
@ -20,8 +21,8 @@ public class GameOverAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments) { public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) {
System.out.println("Victory!"); stream.println("Victory!");
game.setGameOver(); game.setGameOver();
return true; return true;
} }

View file

@ -3,6 +3,7 @@ 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 {
@ -12,12 +13,12 @@ public class InventoryAction implements Action {
} }
@Override @Override
public boolean run(ZorkGame game, List<String> arguments) { public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) {
if (game.inventory.isEmpty()) { if (game.inventory.isEmpty()) {
System.out.println("Inventory: empty"); stream.println("Inventory: empty");
} else { } else {
final String output = "Inventory: " + String.join(", ", game.inventory); final String output = "Inventory: " + String.join(", ", game.inventory);
System.out.println(output); stream.println(output);
} }
return true; return true;
} }

View file

@ -4,6 +4,7 @@ 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;
@ -25,7 +26,7 @@ public class MoveAction implements Action {
} }
@Override @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 // 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"));
@ -33,9 +34,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())) {
System.out.println(game.getCurrentRoom().getDescription()); stream.println(game.getCurrentRoom().getDescription());
} else { } else {
System.out.println("Can't go that way."); stream.println("Can't go that way.");
} }
return true; return true;
} }

View file

@ -4,6 +4,7 @@ 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;
@ -15,12 +16,12 @@ public class OpenAction implements Action {
} }
@Override @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 what = arguments.get(1);
if (what.equals("exit")) { if (what.equals("exit")) {
if (game.getCurrentRoom().isExit()) { if (game.getCurrentRoom().isExit()) {
System.out.println("Game Over"); stream.println("Game Over");
game.setGameOver(); game.setGameOver();
} else { } else {
return false; return false;
@ -30,7 +31,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();
System.out.println(tempContainer.getContents()); stream.println(tempContainer.getContents());
} else { } else {
return false; return false;
} }

View file

@ -4,6 +4,7 @@ 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;
@ -20,7 +21,7 @@ public class PutAction implements Action {
} }
@Override @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 what = arguments.get(1);
final String destination = arguments.get(3); final String destination = arguments.get(3);
@ -29,7 +30,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);
System.out.println("Item " + what + " added to " + destination + "."); stream.println("Item " + what + " added to " + destination + ".");
return true; return true;
} }
} }

View file

@ -4,6 +4,7 @@ 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;
@ -20,12 +21,12 @@ public class ReadAction implements Action {
} }
@Override @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 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);
System.out.println(tempItem.getWriting()); stream.println(tempItem.getWriting());
return true; return true;
} }
return false; return false;

View file

@ -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;
}
}

View file

@ -5,6 +5,7 @@ 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;
@ -22,7 +23,7 @@ public class TakeAction implements Action {
} }
@Override @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 tempString = arguments.get(1);
if ((game.getCurrentRoom()).getItem().contains(tempString)) { if ((game.getCurrentRoom()).getItem().contains(tempString)) {
@ -30,7 +31,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);
System.out.println("Item " + tempString + " added to inventory."); 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! */
@ -40,7 +41,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);
System.out.println("Item " + tempString + " added to inventory."); stream.println("Item " + tempString + " added to inventory.");
return true; return true;
} }
} }

View file

@ -4,6 +4,7 @@ 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;
@ -23,12 +24,12 @@ public class TurnOnAction implements Action {
} }
@Override @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); 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);
System.out.println("You activate the " + what + "."); 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,6 +5,7 @@ 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;
/** /**
@ -22,7 +23,7 @@ public class UpdateAction implements Action {
} }
@Override @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 object = arguments.get(1);
final String newStatus = arguments.get(3); final String newStatus = arguments.get(3);

View file

@ -14,7 +14,7 @@ public interface HasPrintsAndActions {
for (final String print : getPrints()) { for (final String print : getPrints()) {
System.out.println(print); System.out.println(print);
} }
final ActionDispatcher effectsDispatcher = new ActionDispatcher(game); final ActionDispatcher effectsDispatcher = new ActionDispatcher(game, System.out);
for (final String action : getActions()) { for (final String action : getActions()) {
effectsDispatcher.dispatch(action); effectsDispatcher.dispatch(action);
} }