package example7_2.game; import example7_2.game.characters.Flower; import java.util.List; /** * A move behavior that looks for the flower with the highest points/distance ratio and avoids * flowers that can change the move behavior. * @author macavaneys */ public class SuperSmartMoveBehavior implements MoveBehavior { /** * A list of flowers that can change the move behavior */ public final static String[] DANGEROUS_FLOWER_NAMES = new String[] {AdvancedTagBehavior.RANDOM_FLOWER_NAME, AdvancedTagBehavior.GREEDY_FLOWER_NAME, AdvancedTagBehavior.SMART_FLOWER_NAME}; /** * The character associated with this move behavior */ private GameCharacter gc; /** * Initializes SuperSmartMoveBehavior for the specified game character. * @param character The character associated with this move behavior */ public SuperSmartMoveBehavior(GameCharacter character) { this.gc = character; } /** * Moves to the flower with the highest point/distance ratio, avoiding dangerous flowers when in advanced mode. */ @Override public void planMove(List list) { // Find the closest flower with at least 3 points. GameCharacter targetCharacter = null; double targetCharacterPointsPerDistance = Double.MIN_VALUE; for (GameCharacter character : list) { if (character instanceof Flower && !isDangerous(character)) { // Target this flower if it is closer than the current target double distance = gc.distanceTo(character); if (distance > 0) { double pointsPerDistance = character.getScore() / distance; if (pointsPerDistance > targetCharacterPointsPerDistance) { targetCharacter = character; targetCharacterPointsPerDistance = pointsPerDistance; } } } } if (targetCharacter != null) { // Move to this flower gc.setTargetPosition(targetCharacter.getCurrentRow(), targetCharacter.getCurrentColumn()); // set the new target position; the Game framework will do the actual move } else { // targetCharacter == null // There's no acceptable flower, so don't move anywhere. gc.setTargetPosition(gc.getCurrentRow(), gc.getCurrentColumn()); } Game.validateMove(gc); } /** * Returns true or false depending on whether or not the flower is "dangerous", meaning it could change gc's * move behavior, making it less smart. */ private boolean isDangerous(GameCharacter character) { boolean isDangerous = false; if (Game.timeCounter <= Game.ADVANCED_GAME_MODE_TIME) { for (String name : DANGEROUS_FLOWER_NAMES) { if (isDangerous = character.getName().equals(name)) break; } } return isDangerous; } }