actions print error once

This commit is contained in:
RaffaeleMorganti 2022-11-22 16:46:23 +01:00
parent aafbf94434
commit 6341503a70
15 changed files with 38 additions and 32 deletions

View file

@ -15,5 +15,5 @@ public interface Action {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
void run(final ZorkGame game, final List<String> arguments); boolean run(final ZorkGame game, final List<String> arguments);
} }

View file

@ -44,10 +44,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()) { if (action.isEmpty() || !action.get().run(game, arguments)) {
System.out.println("Error"); System.out.println("Error");
} else {
action.get().run(game, arguments);
} }
} }

View file

@ -24,7 +24,7 @@ public class AddAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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);
@ -35,7 +35,8 @@ public class AddAction implements Action {
((HasSetOfCollectable) tempObject).getSetFromType(objType).add(object); ((HasSetOfCollectable) tempObject).getSetFromType(objType).add(object);
game.put(destType, tempObject); game.put(destType, tempObject);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error"); return false;
} }
return true;
} }
} }

View file

@ -23,7 +23,7 @@ public class AttackAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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);
@ -32,10 +32,9 @@ public class AttackAction implements Action {
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 + "."); System.out.println("You assault the " + tempString + " with the " + weapon + ".");
tempCreature.printAndExecuteActions(game); tempCreature.printAndExecuteActions(game);
return; return true;
} }
} }
return false;
System.out.println("Error");
} }
} }

View file

@ -37,7 +37,7 @@ public class DeleteAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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)) {
@ -58,5 +58,6 @@ public class DeleteAction implements Action {
deleteElementFromSpace(game, ROOM, CREATURE, object); deleteElementFromSpace(game, ROOM, CREATURE, object);
break; break;
} }
return true;
} }
} }

View file

@ -20,7 +20,7 @@ public class DropItemAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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)) {
@ -30,7 +30,8 @@ public class DropItemAction implements Action {
game.inventory.remove(what); game.inventory.remove(what);
System.out.println(what + " dropped."); System.out.println(what + " dropped.");
} else { } else {
System.out.println("Error"); return false;
} }
return true;
} }
} }

View file

@ -20,8 +20,9 @@ public class GameOverAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { public boolean run(ZorkGame game, List<String> arguments) {
System.out.println("Victory!"); System.out.println("Victory!");
game.setGameOver(); game.setGameOver();
return true;
} }
} }

View file

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

View file

@ -25,7 +25,7 @@ public class MoveAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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"));
@ -37,5 +37,6 @@ public class MoveAction implements Action {
} else { } else {
System.out.println("Can't go that way."); System.out.println("Can't go that way.");
} }
return true;
} }
} }

View file

@ -15,7 +15,7 @@ public class OpenAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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")) {
@ -23,7 +23,7 @@ public class OpenAction implements Action {
System.out.println("Game Over"); System.out.println("Game Over");
game.setGameOver(); game.setGameOver();
} else { } else {
System.out.println("Error"); return false;
} }
} else { } else {
ZorkContainer tempContainer; ZorkContainer tempContainer;
@ -32,8 +32,9 @@ public class OpenAction implements Action {
tempContainer.open(); tempContainer.open();
System.out.println(tempContainer.getContents()); System.out.println(tempContainer.getContents());
} else { } else {
System.out.println("Error"); return false;
} }
} }
return true;
} }
} }

View file

@ -20,7 +20,7 @@ public class PutAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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,9 +30,9 @@ public class PutAction implements Action {
tempContainer.addItem(what); tempContainer.addItem(what);
game.inventory.remove(what); game.inventory.remove(what);
System.out.println("Item " + what + " added to " + destination + "."); System.out.println("Item " + what + " added to " + destination + ".");
return; return true;
} }
} }
System.out.println("Error"); return false;
} }
} }

View file

@ -20,14 +20,14 @@ public class ReadAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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);
System.out.println(tempItem.getWriting()); System.out.println(tempItem.getWriting());
} else { return true;
System.out.println("Error");
} }
return false;
} }
} }

View file

@ -22,7 +22,7 @@ public class TakeAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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,6 +31,7 @@ public class TakeAction implements Action {
tempRoom.getItem().remove(tempString); tempRoom.getItem().remove(tempString);
game.put(ROOM, tempRoom); game.put(ROOM, tempRoom);
System.out.println("Item " + tempString + " added to inventory."); System.out.println("Item " + tempString + " added to inventory.");
return true;
} else { } else {
/* Search all containers in the current room for the item! */ /* Search all containers in the current room for the item! */
for (String key : game.getCurrentRoom().getContainer()) { for (String key : game.getCurrentRoom().getContainer()) {
@ -40,10 +41,10 @@ public class TakeAction implements Action {
tempContainer.removeItem(tempString); tempContainer.removeItem(tempString);
game.put(CONTAINER, tempContainer); game.put(CONTAINER, tempContainer);
System.out.println("Item " + tempString + " added to inventory."); System.out.println("Item " + tempString + " added to inventory.");
return; return true;
} }
} }
System.out.println("Error"); return false;
} }
} }
} }

View file

@ -23,7 +23,7 @@ public class TurnOnAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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)) {
@ -31,9 +31,9 @@ public class TurnOnAction implements Action {
System.out.println("You activate the " + what + "."); System.out.println("You activate the " + what + ".");
if (tempItem != null) { if (tempItem != null) {
tempItem.printAndExecuteActions(game); tempItem.printAndExecuteActions(game);
return; return true;
} }
} }
System.out.println("Error"); return false;
} }
} }

View file

@ -22,7 +22,7 @@ public class UpdateAction implements Action {
} }
@Override @Override
public void run(ZorkGame game, List<String> arguments) { 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);
@ -30,5 +30,6 @@ public class UpdateAction implements Action {
ZorkObject tempObject = collection.get(object); ZorkObject tempObject = collection.get(object);
tempObject.updateStatus(newStatus); tempObject.updateStatus(newStatus);
collection.put(tempObject); collection.put(tempObject);
return true;
} }
} }