import java.awt.Color; public class CarMover extends MobileSimObject{ // OVERVIEW: Moves the cars in a lane private boolean inTurn; private int dir; // Currently in the middle of a turn (used for coloring) public CarMover(int dir) { inTurn = false; this.dir = dir; } public void executeTurn() throws RuntimeException // Note: requires isInitialized is inherited from SimObject // EFFECTS: Calculates the position of cars in a lane based on which // lane the cars are traveling in. Some lanes move left to right // and some move right to left. // If the car's space is occupied by the frog, the frog will die. { int oldy = getY(); int newx = getX(); if(dir > 0) { if(newx == 19) { grid.removeObjectAt(getX(), getY()); newx = 0; } else newx += dir; } else { if(newx == 0) { grid.removeObjectAt(getX(), getY()); newx = 19; } else newx += dir; } synchronized(getGrid()){ if(grid.getObjectAt(newx, oldy) != null){ System.out.println("You're NASTAY!!!!"); System.exit(1); } setLocation(newx, oldy); grid.repaint(); } } public Color getColor() { if (getY() ==1 ) { return Color.white; } else if (getY()==2){ return Color.cyan; }else if (getY()==3){ return Color.white; }else if (getY()==4){ return Color.cyan; }else { return Color.white; } } //@nowarn Post // ESC/Java doesn't know Color constants are not null }