package example7_2.game; import java.util.List; import java.util.Random; /** * Moves the character randomly * @author macavaneys * */ public class RandomMoveBehavior implements MoveBehavior { /** * The maximum amount of movement allowed per turn. */ private final int RANDOM_INCREMENT = 3; /** * The character associted with this behavior */ private GameCharacter gc; /** * Creates a new RandomMoveBehavior for the specified character */ public RandomMoveBehavior(GameCharacter character) { this.gc = character; } /** * Moves randomly. */ @Override public void planMove(List list) { int deltaR; // incremental movement value in row direction int deltaC; // same, but in column direction Random r = new Random(); deltaR = r.nextInt(2*RANDOM_INCREMENT+1)-RANDOM_INCREMENT; // generate random values between -INCREMENT and INCREMENT deltaC = r.nextInt(2*RANDOM_INCREMENT+1)-RANDOM_INCREMENT; // to determine how far the Bee will move this turn int rowNext = gc.getCurrentRow() + deltaR; // the new target row and column to move to int colNext = gc.getCurrentColumn() + deltaC; gc.setTargetPosition(rowNext, colNext); // set the new target position; the Game framework will do the actual move Game.validateMove(gc); // This MUST be called for the Game framework to validate the target position } }