// CSE 142, Spring 2010, Marty Stepp // A Snake is a critter that "slithers" in a pattern, going EAST once, then // SOUTH once, then WEST twice, then SOUTH once, then EAST 3 times, then SOUTH // once, then WEST 4 times, ... and so on. public class Snake extends Critter { private int horizontal; // number of horizontal moves to be made on this pass private int stepsTaken; // number of horizontal steps taken within this pass so far public Snake() { stepsTaken = 0; horizontal = 1; } public String toString() { return "S"; } public Direction getMove() { stepsTaken++; if (stepsTaken > horizontal) { // the snake has reached the end of his horizontal pass; // we'll go south this time and start over on a longer pass next time stepsTaken = 0; horizontal++; return Direction.SOUTH; } else if (horizontal % 2 == 1) { return Direction.EAST; } else { // horizontal % 2 == 0 return Direction.WEST; } } }