// CSE 142 Summer 2008, Helene Martin // // Snakes slither: E S W W S E E E S W W W W S E E E E E ... import java.awt.*; import java.util.*; public class Snake extends Critter { private int cycleLength; // how many steps in current horizontal cycle private int cycleStep; // how many of the cycle's steps already taken private Random r; // for fighting public Snake() { cycleLength = 1; cycleStep = 0; r = new Random(); } public Direction getMove() { cycleStep++; // println statements are useful for debugging: // System.out.println("cycleStep: " + cycleStep + " cycleLength: " + cycleLength); if(cycleStep > cycleLength) { // Cycle was just completed cycleLength++; cycleStep = 0; return Direction.SOUTH; } else if(cycleLength % 2 == 1) { return Direction.EAST; } else { return Direction.WEST; } } public Attack fight(String opponent) { int attack = r.nextInt(2); if(attack == 0) { return Attack.POUNCE; } else { return Attack.ROAR; } } public String toString() { return "S"; } public Color getColor() { // R: 0 - 255, G: 0 - 255, B: 0 - 255 return new Color(20, 50, 128); } // we can keep the default eat behavior! }