// Helene Martin, CSE 142 // Snake critters move in a lengthening horizontal "slither" pattern: // E, S, WW, S, EEE, S, WWWW, S, EEEEE, S, ... public class Snake extends Critter { private int horizontalSize; // size of current horizontal move private int currentSteps; // steps taken in current direction public Snake() { horizontalSize = 1; } // Development strategy: // - we started by writing out several moves a Snake would make // - we identified properties of the pattern we wanted to create // - we chose the state we would use to create the movement // - we wrote the method // Scratch work: // E S W W S E E E S W W W W S E E E E E // ^ // 1 1 2 2 2 3 3 3 3 4 5 // E W E W E // 0 1 0 1 2 0 1 2 3 0 1 2 3 4 5 // always starts at E // number of E or W movement increments after each S // - E and W alternate // - always go E an odd number of times, W an even number of times // always go S just once public Direction getMove() { if (horizontalSize == currentSteps) { currentSteps = 0; horizontalSize++; return Direction.SOUTH; } else { // we haven't reached S in the pattern (currentSteps < horizontalSize) currentSteps++; if (horizontalSize % 2 == 1) { return Direction.EAST; } else { return Direction.WEST; } } } public String toString() { return "S"; } }