import java.awt.Color; // An implementation of the simplified Buggle class from Lecture 19. // These buggles have the usual buggle state (position, heading, color, brushDown), // but cannot drop/detect bagels, detect walls, leave a trail, or set cell colors. public class SimpleBuggle { // Instance variables: private Location pos; // position of buggle private Direction dir; // heading of buggle private Color col; // color of buggle private boolean brushDown; // brush state of buggle // Constructor method: public SimpleBuggle () { pos = new Location (0,0); dir = Direction.EAST; col = Color.red; brushDown = true; } // Instance methods: public Location getPosition() { return pos; } public void setPosition (Location loc) { pos = loc; } public Direction getHeading() { return dir; } public void setHeading (Direction d) { dir = d; } public Color getColor() { return col; } public void setColor (Color c) { col = c; } public boolean isBrushDown() { return brushDown; } public void brushDown() { brushDown = true; } public void brushUp() { brushDown = false; } public void left() { dir = dir.left(); } public void right() { dir = dir.right(); } public void forward() { Location delta = dir.toLocation(); pos = new Location(pos.x + delta.x, pos.y + delta.y); } public void forward (int n) { if (n < 0) { backward(-n); } else { while (n > 0) { forward(); n--; } } } public void backward() { Location delta = dir.toLocation(); // In this wall-less implementation, position coordinates can become negative. pos = new Location(pos.x - delta.x, pos.y - delta.y); } public void backward (int n) { if (n < 0) { forward(-n); } else { while (n > 0) { backward(); n--; } } } public String toString () { return "[SimpleBuggle: pos = " + getPosition() + ";\n dir = " + getHeading() + ";\n col = " + getColor() + ";\n brushDown = " + isBrushDown() + "]"; } // This main method exercises all the methods of SimpleBuggle. public static void main (String[] args) { SimpleBuggle becky = new SimpleBuggle(); System.out.println("Initial state:\n" + becky); for (int i = 1; i <= 3; i++) { Location loc = becky.getPosition(); becky.setPosition(new Location(loc.y + 3, 2*loc.x + 4)); Direction d = becky.getHeading(); becky.setHeading(d.left()); Color c = becky.getColor(); becky.setColor(new Color(c.getBlue(), c.getGreen(), c.getRed()/2)); if (becky.isBrushDown()) becky.brushUp(); else becky.brushDown(); System.out.println("State at beginning of step " + i + ":\n" + becky); becky.forward(2*i); becky.left(); becky.backward(i); becky.left(); becky.forward(i); becky.right(); becky.backward(); System.out.println("State at end of step " + i + ":\n" + becky); } } }