import java.awt.Color; public class Townies extends Player { // OVERVIEW: A Townie is a subtype of Player. It randomly moves // around the grid and interacts with the crawler. private boolean /*@non_null@*/inTurn; protected String /*@non_null@*/state; // Currently in the middle of a turn (used for coloring) public Townies() { inTurn = false; state = "Townies"; } public Color getColor() // Effects: returns the color associated with this { if (inTurn) { return Color.pink; } else { return Color.pink; } } public void executeTurn() throws RuntimeException // Note: requires isInitialized is inherited from SimObject // EFFECTS: Picks a random direction and tries to move that way. // If the move is successful, return true. If the move fails // because the spot being attempted to move into is already // occupied then return false. { inTurn = true; Direction dir = Direction.randomDirection(); int newy = getY() + dir.northerlyDirection(); int newx = getX() + dir.easterlyDirection(); if (getGrid().validLocation(newx, newy)) { synchronized (getGrid()){ if (getGrid().isSquareEmpty(newx, newy)) { //@nowarn Pre // Here, we pretend there is a lot of compution to do by sleeping, // to make race conditions more likely. delay(500); setLocation(newx, newy); } } } inTurn = false; } } //class modeled loosely after Philosopher.java