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/OpenAction.java
2022-11-22 09:55:26 +01:00

45 lines
1.4 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.ZorkContainer;
import java.util.List;
import static com.github.dtschust.zork.Zork.Type.CONTAINER;
public class OpenAction implements Action {
@Override
public boolean matchesInput(List<String> arguments) {
return arguments.get(0).equals("open");
}
@Override
public void run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1);
if (what.equals("exit")) {
if (game.getCurrentRoom().type.equals("exit")) {
System.out.println("Game Over");
game.setGameOver();
} else {
System.out.println("Error");
}
} else {
ZorkContainer tempContainer;
if (game.getCurrentRoom().container.contains(what)) {
tempContainer = (ZorkContainer) game.get(CONTAINER, what);
tempContainer.open();
if (tempContainer.item.isEmpty()) {
System.out.println(what + " is empty");
} else {
final String output = String.join(", ", tempContainer.item);
System.out.println(what + " contains " + output + ".");
}
} else {
System.out.println("Error");
}
}
}
}