This repository has been archived on 2022-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
sdm03/src/main/java/com/github/dtschust/zork/repl/actions/MoveAction.java

44 lines
1.4 KiB
Java
Raw Normal View History

package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action;
2022-11-22 10:27:56 +00:00
import com.github.dtschust.zork.types.ZorkDirection;
2022-11-22 16:25:40 +00:00
import java.io.PrintStream;
import java.util.List;
2022-11-22 10:27:56 +00:00
import java.util.Optional;
/**
* If it's not a "Special Action", just treat it normally
* Execute a user action or an action command from some <action> element that is not one of the "Special Commands"
* Movement
*/
public class MoveAction implements Action {
@Override
public boolean matchesInput(List<String> arguments) {
2022-11-22 10:27:56 +00:00
return ZorkDirection.fromShort(arguments.get(0)).isPresent();
}
@Override
public int getMaximumArgCount() {
return 1;
}
@Override
2022-11-22 16:25:40 +00:00
public boolean run(ZorkGame game, List<String> arguments, PrintStream stream) {
2022-11-22 10:27:56 +00:00
// we are guaranteed to have a valid short direction name by matchesInput
final ZorkDirection direction = ZorkDirection.fromShort(arguments.get(0)).orElseThrow(() ->
new IllegalStateException("unreachable"));
final Optional<String> roomName = game.getCurrentRoom().getBorderingRoom(direction);
if (roomName.isPresent() && game.changeRoom(roomName.get())) {
2022-11-22 16:25:40 +00:00
stream.println(game.getCurrentRoom().getDescription());
} else {
2022-11-22 16:25:40 +00:00
stream.println("Can't go that way.");
}
2022-11-22 15:46:23 +00:00
return true;
}
}