package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.Zork.Type; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.types.HasSetOfCollectable; import com.github.dtschust.zork.types.ZorkObject; import com.github.dtschust.zork.types.ZorkRoom; import java.util.List; import java.util.Set; import static com.github.dtschust.zork.Zork.Type.*; /** * Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky */ public class DeleteAction implements Action { private static void deleteElementFromSpace(ZorkGame game, Type space, Type element, String object) { for (ZorkObject tempObject : game.values(ZorkObject.class, space)) { Set set = ((HasSetOfCollectable) tempObject).getSetFromType(element); if (set.contains(object)) { set.remove(object); game.put(space, tempObject); } } } @Override public boolean matchesInput(List arguments) { return arguments.get(0).equals("Delete"); } @Override public int getMinimumArgCount() { return 2; } @Override public void run(ZorkGame game, List arguments) { final String object = arguments.get(1); switch (game.getTypeFromLookup(object)) { case ROOM: for (ZorkRoom tempRoom : game.values(ZorkRoom.class, ROOM)) { for (String key : tempRoom.border.keySet()) { if (tempRoom.border.get(key).equals(object)) { tempRoom.border.remove(key); } } game.put(ROOM, tempRoom); } break; case ITEM: deleteElementFromSpace(game, ROOM, ITEM, object); deleteElementFromSpace(game, CONTAINER, ITEM, object); break; case CONTAINER: deleteElementFromSpace(game, ROOM, CONTAINER, object); break; case CREATURE: deleteElementFromSpace(game, ROOM, CREATURE, object); break; } } }