Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import edu.kzoo.util.RandNumGenerator;
- /**
- * Grid-Based Marine Biology Simulation Program:<br>
- *
- * A <code>FastFish</code> object represents a fish in the Marine Biology
- * Simulation. FastFish move forward two times if the two cells
- * are empty, one cell if only one cell is empty, and change direction
- * if it can't move forward. It can only swim two cells away if the
- * intermediate cell is empty. Each fish has a unique ID, which remains constant
- * throughout its life. A FastFish also maintains information about its
- * location and direction in the environment.
- *
- * @author Andrew Wright
- * @author with assistance from Marrissa
- * @version 14 May 2019
- **/
- public class FastFish extends Fish
- {
- // Class Variable: Shared among ALL fish
- private static int nextAvailableID = 1; // next avail unique identifier
- // Instance Variables: Encapsulated data for EACH fish
- private int myId; // unique ID for this fish
- private Direction myDir; // fish's direction
- private Color myColor; // fish's color
- // constructors and related helper methods
- /** Constructs a FastFish at the specified location in a given environment.
- * The Fish is assigned a random direction and random color.
- * (Precondition: parameters are non-null; <code>loc</code> is valid
- * for <code>env</code>.)
- * @param env environment (grid) in which fish will live
- * @param loc location of the new fish in <code>env</code>
- **/
- public FastFish(Grid env, Location loc)
- {
- this(env, loc, env.randomDirection(),Color.RED);
- }
- /** Constructs a fish at the specified location and direction in a
- * given environment. The Fish is assigned a random color.
- * (Precondition: parameters are non-null; <code>loc</code> is valid
- * for <code>env</code>.)
- * @param env environment (grid) in which fish will live
- * @param loc location of the new fish in <code>env</code>
- * @param dir direction the new fish is facing
- **/
- public FastFish(Grid env, Location loc, Direction dir)
- {
- this(env, loc, dir, Color.RED);
- }
- /** Constructs a fish of the specified color at the specified location
- * and direction.
- * (Precondition: parameters are non-null; <code>loc</code> is valid
- * for <code>env</code>.)
- * @param env environment (grid) in which fish will live
- * @param loc location of the new fish in <code>env</code>
- * @param dir direction the new fish is facing
- * @param col color of the new fish
- **/
- public FastFish(Grid env, Location loc, Direction dir, Color col)
- {
- super(env, loc, dir, col); // puts object at location myLoc in environment
- this.myId = nextAvailableID;
- this.nextAvailableID++;
- this.myDir = dir;
- this.myColor = col;
- }
- // accessor methods
- /** Generates a new FastFish. This method is called upon in the act method of the Fish class.
- * @param loc location of the new fish
- *
- */
- protected void generateChild(Location loc)
- {
- FastFish child = new FastFish(grid(), loc, grid().randomDirection(), this.color());
- Debug.println(" New Fish created: " + child.toString());
- }
- // modifier method
- // internal helper methods
- /** Finds this fish's next location.
- * A fish may move to any empty adjacent locations or a location two steps ahead
- * except the one behind it (fish do not move backwards). If this fish cannot
- * move, <code>nextLocation</code> returns its current location.
- * @return the next location for this fish
- **/
- protected Location nextLocation()
- {
- Location loc = this.location();
- // FastFish can move in any of the four directions
- Direction dir = this.grid().randomDirection();
- Location locFront = this.grid().getNeighbor(loc, dir);
- Location locTwoFront = this.grid().getNeighbor(locFront, dir);
- if (this.grid().isEmpty(locFront) && this.grid().isEmpty(locTwoFront))
- return locTwoFront;
- else if (this.grid().isEmpty(locFront))
- return locFront;
- else
- return loc;
- }
- /** Moves this fish in its environment and reverses it if the initial
- /* location is the new location.
- /**/
- protected void move()
- {
- // Find a location to move to.
- Debug.print("Fish " + toString() + " attempting to move. ");
- Location nextLoc = nextLocation();
- Location oldLoc = location();
- Direction newDir = grid().getDirection(oldLoc, nextLoc);
- Direction fourDir = grid().randomDirection();
- // If the next location is different, move there.
- if ( ! nextLoc.equals(location()) )
- {
- // Move to new location.
- changeLocation(nextLoc);
- // Update direction in case fish had to turn to move.
- changeDirection(newDir);
- Debug.println(" Moves to " + location() + direction());
- }
- else
- changeDirection(this.direction().reverse());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement