FastFish Exercise Set 1 Question 6 // Additional method findMoveLocs in Fish class /** Finds locations to which this fish might move. * A fish may move to any empty adjacent locations except the one * behind it (fish do not move backwards). * @return a list of locations to which this fish might move **/ protected ArrayList findMoveLocs() { // Get list of neighboring empty locations. ArrayList emptyNbrs = emptyNeighbors(); Debug.println("Has adjacent empty positions: " + emptyNbrs.toString()); // Remove the location behind, since fish do not move backwards. Direction oppositeDir = direction().reverse(); Location locationBehind = environment().getNeighbor(location(), oppositeDir); emptyNbrs.remove(locationBehind); // If no valid empty neighboring locations, then we're done. return emptyNbrs; } // Modified version of nextLocation method in Fish class /** Finds this fish's next location. * Finds the possible move locations for this fish and then randomly * chooses one. If this fish cannot move, nextLocation * returns its current location. * @return the next location for this fish **/ protected Location nextLocation() { // Get list of possible move locations. ArrayList moveLocations = findMoveLocs(); Debug.print("Possible new locations are: " + moveLocations.toString()); // If there are no possible moves, then we're done. if ( moveLocations.size() == 0 ) return location(); // Randomly choose one neighboring empty location and return it. Random randNumGen = RandNumGenerator.getInstance(); int randNum = randNumGen.nextInt(moveLocations.size()); return (Location) moveLocations.get(randNum); }