package example7_2.game; import example7_2.game.characters.Flower; import java.util.List; /** * Moves toward the flower with the highest point value * @author macavaneys * */ public class GreedyMoveBehavior implements MoveBehavior { /** * The character associated with this behavior */ private GameCharacter gc; /** * Creates a new GreedyMoveBehavior for the specified character * @param character */ public GreedyMoveBehavior(GameCharacter character) { this.gc = character; } /** * Finds and moves to the flower with the most points */ @Override public void planMove(List list) { // Find the flower with the most points. GameCharacter targetCharacter = null; int targetCharacterScore = Integer.MIN_VALUE; for (GameCharacter character : list) { if (character instanceof Flower && character.getScore() > targetCharacterScore) { targetCharacter = character; targetCharacterScore = character.getScore(); } } 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 are no flowers on the board? Stay put then! gc.setTargetPosition(gc.getCurrentRow(), gc.getCurrentColumn()); } Game.validateMove(gc); } }