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 element that is not one of the "Special Commands" * Movement */ public class MoveAction implements Action { @Override public boolean matchesInput(List arguments) { return ZorkDirection.fromShort(arguments.get(0)).isPresent(); } @Override public int getMaximumArgCount() { return 1; } @Override public boolean run(ZorkGame game, List 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 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; } }