Advertisement
Guest User

FastFish

a guest
May 21st, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. import edu.kzoo.util.RandNumGenerator;
  2.  
  3. /**
  4.  *  Grid-Based Marine Biology Simulation Program:<br>
  5.  *
  6.  *  A <code>FastFish</code> object represents a fish in the Marine Biology
  7.  *  Simulation. FastFish move forward two times if the two cells
  8.  *  are empty, one cell if only one cell is empty, and change direction
  9.  *  if it can't move forward. It can only swim two cells away if the
  10.  *  intermediate cell is empty. Each fish has a unique ID, which remains constant
  11.  *  throughout its life.  A FastFish also maintains information about its
  12.  *  location and direction in the environment.
  13.  *
  14.  *  @author Andrew Wright
  15.  *  @author with assistance from Marrissa
  16.  *  @version 14 May 2019
  17.  **/
  18.  
  19. public class FastFish extends Fish
  20. {
  21.     // Class Variable: Shared among ALL fish
  22.     private static int nextAvailableID = 1;   // next avail unique identifier
  23.  
  24.     // Instance Variables: Encapsulated data for EACH fish
  25.     private int myId;                  // unique ID for this fish
  26.     private Direction myDir;           // fish's direction
  27.     private Color myColor;             // fish's color
  28.  
  29.  
  30.   // constructors and related helper methods
  31.  
  32.     /** Constructs a FastFish at the specified location in a given environment.
  33.      *  The Fish is assigned a random direction and random color.
  34.      *  (Precondition: parameters are non-null; <code>loc</code> is valid
  35.      *  for <code>env</code>.)
  36.      *  @param env    environment (grid) in which fish will live
  37.      *  @param loc    location of the new fish in <code>env</code>
  38.      **/
  39.     public FastFish(Grid env, Location loc)
  40.     {
  41.         this(env, loc, env.randomDirection(),Color.RED);
  42.     }
  43.  
  44.     /** Constructs a fish at the specified location and direction in a
  45.      *  given environment.  The Fish is assigned a random color.
  46.      *  (Precondition: parameters are non-null; <code>loc</code> is valid
  47.      *  for <code>env</code>.)
  48.      *  @param env    environment (grid) in which fish will live
  49.      *  @param loc    location of the new fish in <code>env</code>
  50.      *  @param dir    direction the new fish is facing
  51.      **/
  52.     public FastFish(Grid env, Location loc, Direction dir)
  53.     {
  54.         this(env, loc, dir, Color.RED);
  55.     }
  56.  
  57.     /** Constructs a fish of the specified color at the specified location
  58.      *  and direction.
  59.      *  (Precondition: parameters are non-null; <code>loc</code> is valid
  60.      *  for <code>env</code>.)
  61.      *  @param env    environment (grid) in which fish will live
  62.      *  @param loc    location of the new fish in <code>env</code>
  63.      *  @param dir    direction the new fish is facing
  64.      *  @param col    color of the new fish
  65.      **/
  66.     public FastFish(Grid env, Location loc, Direction dir, Color col)
  67.     {
  68.         super(env, loc, dir, col);    // puts object at location myLoc in environment
  69.         this.myId = nextAvailableID;
  70.         this.nextAvailableID++;
  71.         this.myDir = dir;
  72.         this.myColor = col;
  73.     }
  74.  
  75.   // accessor methods
  76.     /** Generates a new FastFish. This method is called upon in the act method of the Fish class.
  77.      * @param loc location of the new fish
  78.      *
  79.      */
  80.       protected void generateChild(Location loc)
  81.     {
  82.         FastFish child = new FastFish(grid(), loc, grid().randomDirection(), this.color());
  83.         Debug.println(" New Fish created: " + child.toString());
  84.     }
  85.  
  86.  
  87.   // modifier method
  88.  
  89.   // internal helper methods
  90.     /** Finds this fish's next location.
  91.      *  A fish may move to any empty adjacent locations or a location two steps ahead
  92.      *  except the one behind it (fish do not move backwards).  If this fish cannot
  93.      *  move, <code>nextLocation</code> returns its current location.
  94.      *  @return the next location for this fish
  95.      **/
  96.     protected Location nextLocation()
  97.     {
  98.         Location loc = this.location();
  99.         // FastFish can move in any of the four directions
  100.         Direction dir = this.grid().randomDirection();
  101.         Location locFront = this.grid().getNeighbor(loc, dir);
  102.         Location locTwoFront = this.grid().getNeighbor(locFront, dir);
  103.         if (this.grid().isEmpty(locFront) && this.grid().isEmpty(locTwoFront))
  104.         return locTwoFront;
  105.         else if (this.grid().isEmpty(locFront))
  106.         return locFront;
  107.         else
  108.         return loc;
  109.     }
  110.  
  111.     /** Moves this fish in its environment and reverses it if the initial
  112.     /* location is the new location.
  113.     /**/
  114.     protected void move()
  115.     {
  116.         // Find a location to move to.
  117.         Debug.print("Fish " + toString() + " attempting to move.  ");
  118.         Location nextLoc = nextLocation();
  119.         Location oldLoc = location();
  120.         Direction newDir = grid().getDirection(oldLoc, nextLoc);
  121.         Direction fourDir = grid().randomDirection();
  122.  
  123.         // If the next location is different, move there.
  124.         if ( ! nextLoc.equals(location()) )
  125.       {
  126.             // Move to new location.
  127.             changeLocation(nextLoc);
  128.    
  129.             // Update direction in case fish had to turn to move.
  130.             changeDirection(newDir);
  131.             Debug.println("  Moves to " + location() + direction());
  132.         }
  133.         else
  134.             changeDirection(this.direction().reverse());
  135.     }
  136.  
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement