import java.awt.Color; public class Police extends Player { // OVERVIEW: The Police are of type Player and move around the grid. // If the crawler runs into the police, he is ticketed or arrested. private boolean /*@non_null@*/inTurn; // Currently in the middle of a turn (used for coloring) protected String/*@non_null@*/ state; public Police() { //Constructor inTurn = false; state = "Police"; } public Color getColor() //Effects: returns the color associated with this { if (inTurn) { return Color.blue; } else { return Color.white; } }//@nowarn NonNullResult // ESC/Java doesn't know Color constants are not null 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, set new location. If the move fails // because the spot being attempted to move into is already // occupied then try another direction. { 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 conditions more likely. delay(500); setLocation(newx, newy); } } } inTurn = false; } } //modeled loosely after Philosopher.java