import java.awt.*; public class SnakeWorld extends RandomBagelWorld { public void run () { Snaker sandra = new Snaker(); sandra.tick128(); } } class Snaker extends TickBuggle { public void tick () { if (isFacingUpperRightCorner() || isFacingUpperLeftCorner()) { // Do nothing } else if (isFacingEastWall()) { left(); forward(); left(); } else if (isFacingWestWall()) { right(); forward(); right(); } else { forward(); } } public boolean isFacingUpperRightCorner() { return isFacingEastWall() && isWallToLeft(); } public boolean isFacingUpperLeftCorner() { return isFacingWestWall() && isWallToRight(); } public boolean isFacingEastWall() { return isFacingWall() && (getHeading() == Direction.EAST); } public boolean isFacingWestWall() { return isFacingWall() && (getHeading() == Direction.WEST); } public boolean isWallToRight() { // Returns true if there is a wall to this buggle's right, and false otherwise. // The position and heading of the buggle are invariant under this operation. right(); boolean result = isFacingWall(); left(); return result; } public boolean isWallToLeft() { // Returns true if there is a wall to this buggle's left, and false otherwise. // The position and heading of the buggle are invariant under this operation. left(); boolean result = isFacingWall(); right(); return result; } public void tickEfficient () { // Here is an alternative version of tick. It is more efficient than the above // version in the sense that it performs fewer buggle operations. But it is also // more difficult to understand. Unless efficiency is paramount, we recommend // that you write methods in a clearer style that makes liberal use of abstractions. if (isFacingWall()) { if (getHeading() == Direction.EAST) { left(); if (isFacingWall()) { right(); } else { forward(); left(); } } else { // if (getHeading() == Direction.WEST) right(); if (isFacingWall()) { left(); } else { forward(); right(); } } } else { forward(); } } }