import java.awt.Color; public class Hunter extends MobileSimObject { // OVERVIEW: A Hunter is a user controlled simulator object that moves // where the keyboard directs him to. Direction dir = new Direction (0,0); boolean started = false; Color c = new Color(0,0,0); int score = 0; public Hunter (Color col) { dir = new Direction (0,0); c = col; } public Color getColor() { return c; } //@nowarn NonNullResult // ESC/Java doesn't know Color constants are not null public String getName() { return "Hunter"; } public void executeTurn() throws RuntimeException // Note: requires isInitialized is inherited from SimObject // EFFECTS: Moves in the direction told to by the Keyboard. // If the square being moved into is occupied, checks // to see what kind of object occupies it, and acts // accordingly // Encounters Stalker => Hunter Loses, game over // Encounters Hunter => Does nothing // Encounters Food => Removes food from grid, increses score // If all food is eaten, exits game and prints who // won, and their score. { int newrow = getRow () + dir.northerlyDirection (); int newcol = getColumn () + dir.easterlyDirection (); // Code to deal with running into the various objects (Hunter, Stalker, // Food) and also code to end the game when all the food is eaten. if (started) { if (getGrid().validLocation(newrow,newcol)) { if (getGrid().isSquareEmpty(newrow, newcol)) { } else { if (getGrid().getObjectAt(newrow,newcol).getName().equals("Stalker")) { if (c.equals(new Color(255,0,0))) { System.out.println("Red Loses! Watch out for the Stalkers!"); } else { System.out.println("Magenta Loses! Watch out for the Stalkers!"); } System.exit(0); } else if (getGrid().getObjectAt(newrow,newcol).getName().equals("Hunter")) { } else { getGrid().removeObjectAt(newrow, newcol); System.err.println("You got him!"); score++; if (getGrid().numObjects() == 7) { if (score > 5) { System.err.println(c.toString() + "wins with a score of " + score); } else if (score < 5) { if (c.equals(new Color(255,0,0))) { System.err.println("magenta wins with a score of " + (10 - score)); } else { System.err.println("red wins with a score of " + (10 - score)); } System.exit(0); } else { System.err.println("You tied, try again!"); System.exit(0); } } } } } } synchronized (getGrid()) { if (getGrid ().validLocation (newrow, newcol)) { if (getGrid().isSquareEmpty (newrow, newcol)) { setLocation (newrow, newcol); } } } } }