/* CSE 142, Autumn 2007, Marty Stepp This class is an example of a Critter. All Cougars know how to do is party. When the simulator starts, the Cougars all agree on the secret location where their party will be. They all agree on a random x/y position for the party. Now, when each Cougar moves, it will seek out that location. It will move right until it reaches that location's x, and then down until it reaches that location's y. But Cougars aren't very smart. They can't all stand on the same square! When they reach the party location, they will all smash into each other and die. */ import java.awt.*; import java.util.*; public class Cougar2 implements Critter { // these fields are 'static', meaning that one copy of these fields is // shared by all Cougars. (Not a new copy of them in each object.) private static int partyX = -1; private static int partyY = -1; public Cougar2() { } public boolean eat() { return false; } public int fight(String opponent) { return SCRATCH; } public Color getColor() { return Color.RED; } // moves toward the randomly chosen, shared "party" location public int getMove(CritterInfo info) { if (partyX == -1) { // party location has not been chosen; choose it now Random randy = new Random(); partyX = randy.nextInt(info.getWidth()); partyY = randy.nextInt(info.getHeight()); System.out.println("Party is at x=" + partyX + ", y=" + partyY); } if (info.getX() != partyX) { return EAST; } else if (info.getY() != partyY) { return SOUTH; } else { // I am at the party return CENTER; } } public String toString() { return "C"; } }