import java.awt.*; import java.util.*; // a knight moves randomly in one direction for one step // and then moves 2 steps in a perpendicular direction public class Knight2 extends Critter { private Random rand; private int moves; private Direction firstStep; // the direction of the first step private Direction multiStep; // the direction of the second and third step public Knight2() { rand = new Random(); } public Attack fight(char opponent) { return Attack.SCRATCH; } public Color getColor() { return Color.BLACK; } // see class description for details on how a knight moves public Direction getMove() { moves++; if (moves % 3 == 1) { Direction[] directions = { Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST }; firstStep = getRandomDirection(directions); return firstStep; } else if (moves % 3 == 2) { if (firstStep == Direction.NORTH || firstStep == Direction.SOUTH) { Direction[] directions = { Direction.EAST, Direction.WEST }; multiStep = getRandomDirection(directions); } else { // EAST or WEST Direction[] directions = { Direction.NORTH, Direction.SOUTH }; multiStep = getRandomDirection(directions); } return multiStep; } else { return multiStep; } } public Direction getRandomDirection(Direction[] nums) { return nums[rand.nextInt(nums.length)]; } public char getChar() { return '2'; } }