// AP(r) Computer Science Grid World Case Study: // The BugRunner class is copyright(c) 2006 College Entrance // Examination Board (www.collegeboard.com). // // This class is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation. // // This class is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. import java.awt.Color; import info.gridworld.actor.Actor; import info.gridworld.grid.Grid; import info.gridworld.grid.Location; public class Shuttle extends Actor { public Shuttle(int n) { sideLength = n; steps = 1; setColor(Color.RED); } public void act() { if (steps < sideLength) { move(); steps++; } else { turn(); steps = 0; } } /** * Moves this bug forward. The bug may replace other * actors or fall outside the grid. Call canMove to check * whether it is safe to move. */ public void move() { Grid gr = getGrid(); Location loc = getLocation(); Location next = loc.getNeighborLocation(getDirection()); if (gr.isValid(next)) moveTo(next); else removeSelfFromGrid(); } public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); } private int steps; private int sideLength; }