import info.gridworld.actor.Bug; import info.gridworld.grid.Location; /** * A WBug traces out a single W pattern of a given size. */ public class WBug extends Bug { private int segmentLength; // the number of flowers in each segment private int steps; // the number of steps in the current side private int segment; // which segment of the W the WBug is on public WBug(int length) { setDirection(Location.SOUTHEAST); steps = 0; segment = 1; segmentLength = length; } public void act() { if (segment <= 4 && steps < segmentLength) { if (canMove()) { move(); steps++; } } else if (segment == 1) { setDirection(Location.NORTHEAST); steps = 0; segment++; } else if (segment == 2) { setDirection(Location.SOUTHEAST); steps = 0; segment++; } else if (segment == 3) { setDirection(Location.NORTHEAST); steps = 0; segment++; } } }