// CSE 142, Autumn 2008, Marty Stepp // // DrunkenFratGuys collectively decide on one random location where there will // be a "party" on the critter map, then they all go to that location, moving // north and then east until they reach the party's location. // // Since we want all the frat guys to go to the same party, the party's location // is represented as a set of static fields. Normal fields are replicated into // each object, but static ones Static fields are shared throughout all objects // of a class. This is important because all frat guys want to go to the same // party. import java.util.*; public class DrunkenFratGuy extends Critter { // shared data (all objects share ONE copy of these variables; // not replicated in each object) private static int partyX; private static int partyY; private static boolean hasBeenSet = false; public DrunkenFratGuy() { if (!hasBeenSet) { // choose random party location Random rand = new Random(); partyX = rand.nextInt(60); partyY = rand.nextInt(50); hasBeenSet = true; } } public Direction getMove() { if (getY() != partyY) { // go north or east until reaching party return Direction.NORTH; } else if (getX() != partyX) { return Direction.EAST; } else { // already at party; stay here return Direction.CENTER; } } }