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

43 lines
1.3 KiB
Java

package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.types.ZorkDirection;
import java.util.List;
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) {
return ZorkDirection.fromShort(arguments.get(0)).isPresent();
}
@Override
public int getMaximumArgCount() {
return 1;
}
@Override
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"));
final Optional<String> roomName = game.getCurrentRoom().getBorderingRoom(direction);
if (roomName.isPresent() && game.changeRoom(roomName.get())) {
game.stream.println(game.getCurrentRoom().getDescription());
} else {
game.stream.println("Can't go that way.");
}
return true;
}
}