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;
}
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 Optional<Action> action = findAction(arguments);
if (action.isEmpty()) {
if (action.isEmpty() || !action.get().run(game, arguments)) {
System.out.println("Error");
} else {
action.get().run(game, arguments);
}
}

View file

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

View file

@ -23,7 +23,7 @@ public class AttackAction implements Action {
}
@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 weapon = arguments.get(3);
@ -32,10 +32,9 @@ public class AttackAction implements Action {
if (tempCreature != null && game.inventory.contains(weapon) && tempCreature.isAttackSuccessful(game, weapon)) {
System.out.println("You assault the " + tempString + " with the " + weapon + ".");
tempCreature.printAndExecuteActions(game);
return;
return true;
}
}
System.out.println("Error");
return false;
}
}

View file

@ -37,7 +37,7 @@ public class DeleteAction implements Action {
}
@Override
public void run(ZorkGame game, List<String> arguments) {
public boolean run(ZorkGame game, List<String> arguments) {
final String object = arguments.get(1);
switch (game.getTypeFromLookup(object)) {
@ -58,5 +58,6 @@ public class DeleteAction implements Action {
deleteElementFromSpace(game, ROOM, CREATURE, object);
break;
}
return true;
}
}

View file

@ -20,7 +20,7 @@ public class DropItemAction implements Action {
}
@Override
public void run(ZorkGame game, List<String> arguments) {
public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1);
if (game.inventory.contains(what)) {
@ -30,7 +30,8 @@ public class DropItemAction implements Action {
game.inventory.remove(what);
System.out.println(what + " dropped.");
} else {
System.out.println("Error");
}
return false;
}
return true;
}
}

View file

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

View file

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

View file

@ -25,7 +25,7 @@ public class MoveAction implements Action {
}
@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
final ZorkDirection direction = ZorkDirection.fromShort(arguments.get(0)).orElseThrow(() ->
new IllegalStateException("unreachable"));
@ -37,5 +37,6 @@ public class MoveAction implements Action {
} else {
System.out.println("Can't go that way.");
}
return true;
}
}

View file

@ -15,7 +15,7 @@ public class OpenAction implements Action {
}
@Override
public void run(ZorkGame game, List<String> arguments) {
public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1);
if (what.equals("exit")) {
@ -23,7 +23,7 @@ public class OpenAction implements Action {
System.out.println("Game Over");
game.setGameOver();
} else {
System.out.println("Error");
return false;
}
} else {
ZorkContainer tempContainer;
@ -32,8 +32,9 @@ public class OpenAction implements Action {
tempContainer.open();
System.out.println(tempContainer.getContents());
} else {
System.out.println("Error");
return false;
}
}
return true;
}
}

View file

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

View file

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

View file

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