/** BugVariations @author John Doe Period 3 */ import info.gridworld.actor.ActorWorld; import info.gridworld.grid.Location; import info.gridworld.actor.Actor; import info.gridworld.grid.UnboundedGrid; import java.awt.Color; import info.gridworld.actor.Bug; public class BugVariations { public static void main(String[] args) { ActorWorld world = new ActorWorld(); // set grid to UnboundedGrid to make it easier to see all bugs UnboundedGrid grid = new UnboundedGrid(); world.setGrid(grid); // testing BoxBug: BoxBug box = new BoxBug(4); box.setColor(Color.ORANGE); world.add(new Location(20, 20), box); // testing CircleBug: CircleBug circ = new CircleBug(4); circ.setColor(Color.BLACK); world.add(new Location(23, 3), circ); // testing SpiralBug: SpiralBug spiral = new SpiralBug(4); spiral.setColor(Color.MAGENTA); world.add(new Location(10, 20), spiral); // testing ZBug: ZBug zzz = new ZBug(4); zzz.setColor(Color.GREEN); world.add(new Location(7, 3), zzz); // testing WBug: WBug wyo = new WBug(4); wyo.setColor(Color.YELLOW); world.add(new Location(1, 1), wyo); world.show(); }// end of main method }// end of BugVariations "client" class ///////////////////////////////////////////////////////////////////////////// class BoxBug extends Bug { private int steps; private int sideLength; public BoxBug(int length) { steps = 0; sideLength = length; } public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; } } }// end of BoxBug class ///////////////////////////////////////////////////////////////////////////// class CircleBug extends Bug { private int steps; private int sideLength; public CircleBug(int n) { sideLength = n; } public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); steps = 0; } } }// end of CircleBug class ///////////////////////////////////////////////////////////////////////////// class SpiralBug extends Bug { private int sideLength; private int steps; public SpiralBug(int n) { sideLength = n; steps = 0; } public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; //Each time a SpiralBug turns, increase the sideLength by one sideLength++; } } }// end of SpiralBug class ///////////////////////////////////////////////////////////////////////////// class ZBug 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 Z the ZBug is on public ZBug(int length) { setDirection(Location.EAST); // or setDirection(90); steps = 0; segment = 1; segmentLength = length; } public void act() { if (segment <= 3 && steps < segmentLength) { if (canMove()) { move(); steps++; } } else if (segment == 1) { setDirection(Location.SOUTHWEST); steps = 0; segment++; } else if (segment == 2) { setDirection(Location.EAST); steps = 0; segment++; } } }// end of ZBug class ///////////////////////////////////////////////////////////////////////////// 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++; } } }// end of WBug class