|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectfishPond.Model
public class Model
Model for the Fish Pond Simulation. The model consists of a List of Fish, a List of Plants, and a two dimensional array of boolean values representing the pond (each element in the array is either ROCK, or WATER.)
Each time the simulation is re-started a new Model object is created.
STUDENTS MAY NOT ADD ANY FIELDS. ALSO, STUDENTS MAY NOT ADD ANY PUBLIC METHODS. (PRIVATE METHODS OF YOUR OWN ARE OKAY.)
Field Summary | |
---|---|
static boolean |
ROCK
Value stored in landscape array to represent rock |
static boolean |
WATER
Value stored in landscape array to represent water |
Constructor Summary | |
---|---|
Model(int numRows,
int numCols,
int numRocks,
int numFish,
int numPlants)
THIS METHOD HAS BEEN WRITTEN FOR YOU! |
|
Model(Model other)
Copy Constructor. |
Method Summary | |
---|---|
void |
addFish(Fish f)
Adds the fish f to the fish list, if possible. |
void |
addPlant(Plant p)
Attempts to add the plant p to plant list, if possible. |
static void |
fishEatPlant(Fish f,
Plant p)
Fish f eats a portion of plant p. |
void |
fishExplosions()
THIS METHOD HAS BEEN WRITTEN FOR YOU! |
int |
getCols()
returns number of columns in landscape array |
java.util.ArrayList<Fish> |
getFish()
Returns a COPY of the fish list. |
java.util.ArrayList<Plant> |
getPlants()
Returns a COPY of the plants list. |
int |
getRows()
returns number of rows in landscape array |
boolean |
getShape(int row,
int col)
Returns the specified entry of the landscape array (either WATER or ROCK). |
void |
growPlants()
Iterates through the plants list, growing each plant by invoking it's "grow" method. |
boolean |
isSpaceAvailable(int r,
int c)
|
void |
moveFish()
Note: This method assumes that each live fish that is not surrounded by rocks is already facing a direction where there is no rock! (Typically the call to this method should immediately follow a call to "turnFish", which ensures that these conditions are satisfied.) |
void |
plantExplosions()
THIS METHOD HAS BEEN WRITTEN FOR YOU. |
void |
removeDeadFish()
Iterates through the list of Fish. |
void |
removeDeadPlants()
Iterates through the list of Plants. |
void |
shrinkFish()
Iterates through fish list. |
void |
turnFish()
Iterate through list of Fish. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final boolean WATER
public static final boolean ROCK
Constructor Detail |
---|
public Model(int numRows, int numCols, int numRocks, int numFish, int numPlants)
If numRows is smaller than MIN_POND_ROWS, or if numCols is smaller than MIN_POND_COLS, then this method will throw an IllegalPondSizeException.
The fields "rows" and "cols" are initilized with the values of parameters numRows and numCols.
The field "landscape" is initialized as a 2-dimensional array of booleans. The size is determined by rows and cols. Every entry in the landscape array is filled with WATER. The border around the perimeter of the landscape array (top, bottom, left, right) is then overwritten with ROCK.
Random rocks are placed in the pond until the number of rocks (in addition to those in the border) reaches numRocks.
The "plants" ArrayList is instantiated. Randomly placed Plant objects are put into the List. Their positions are chosen so that they are never above rocks or in the same position as another plant. Plants are generated in this way until the list reaches size numPlants.
The "fish" ArrayList is instantiated. Now randomly placed Fish objects are put into the List. Their directions are also randomly selected. The positions are chosen so that they are never above rocks, plants, or other fish. Fish are generated in this way until the list reaches size numFish.
numRows
- number of rows for pondnumCols
- number of columns for pondnumRocks
- number of rocks to be drawn in addition to rocks around border
of pondnumFish
- number of fish to start withnumPlants
- number of plants to start withpublic Model(Model other)
Since landscape is immutable, it is be copied with just a reference copy. Fish and Plants are mutable, so they must be copied with a DEEP copy! (WARNING: Each fish and each plant must be copied.)
Method Detail |
---|
public void plantExplosions()
When a plant gets bigger than Plant.MAX_PLANT_SIZE, it will explode into 2 to 9 smaller plants, whose sizes add up to the size of the original plant. The smaller plants will be placed in the 9 regions of the landscape array that surround the original plant. If there are rocks, fish, or other plants already occupying these adjacent regions, then fewer than 9 plants are created. If there are no available regions nearby, the plant will not explode.
public void fishExplosions()
When a fish gets bigger than Fish.MAX_FISH_SIZE, it will explode into 4 to 8 smaller fish, whose sizes add up to the size of the original fish. The smaller fish will be placed in the eight regions of the landscape array surrounding the original fish. The little fish will be begin moving in directions that point away from the original location. (Note that no little fish is placed into the original location of the landscape array where the exploding fish was -- just in the surrounding squares.) If there are rocks, fish, or plants already occupying these adjacent squares, then fewer than eight little fish are created. If there are not at least four available surrounding squares, then the fish will not explode.
public boolean isSpaceAvailable(int r, int c)
public static void fishEatPlant(Fish f, Plant p)
public int getRows()
public int getCols()
public void shrinkFish()
public void growPlants()
public void removeDeadFish()
public void removeDeadPlants()
public void turnFish()
1. If this fishIsSurroundedByRocks, DO NOTHING, and move on to the next fish. (This fish will not turn.)
2. If this fish's direction is not equal to one of the codes UP, DOWN, LEFT, or RIGHT, then throw an IllegalFishDirectionException, passing this fish's direction to the constructor.
3. Check whether or not this fish is about to hit a rock if it moves in it's current direction. If it is about to hit a rock, call the fish's setRandomDirection method. Repeat this step until the fish is no longer about to hit a rock. Do not make any EXTRA calls to setRandomDirection or you will fail our tests!
public void moveFish()
This method iterates through the list of fish. For each fish that isAlive do the following:
1. Check to see if this fishIsSurroundedByRocks. If so, DO NOTHING and move along to the next fish in the list. (This fish does not move, does not eat, does not fight.)
2. Move this fish by calling it's "move" method.
3. Check if there is a plant that isAlive and is located in the same position as this fish. If so, have the fish eat part of the plant by calling fishEatPlant.
4. Check if there is another fish (distinct from this fish) that is in the same location as this fish. If so, have the two fish fight each other by calling the fight method. IMPORTANT -- the fight method is not symmetrical. You must use THIS fish as the current object, and pass the OTHER fish as the parameter (otherwise you will not pass our tests.)
public void addPlant(Plant p)
First checks if the landscape in the plant's location is equal to ROCK. If it is, then does not add the plant to the list. Instead throws an IllegalPlantPositionException, passing IllegalPlantPositionException.PLANT_OVER_ROCK to the constructor.
Now checks for another plant (distinct from the parameter) that is in the same location as the parameter. If one is found, then does not add the plant to the list. Instead throws an IllegalPlantPositionException, passing IllegalPlantPositionException.TWO_PLANTS_IN_ONE_PLACE to the constructor.
Otherwise, adds the plant to the list "plants".
public void addFish(Fish f)
First checks if the landscape in the fish's location is equal to ROCK. If it is, then the fish is not added to the list. Instead, throws an IllegalFishPositionException, passing IllegalFishPositionException.FISH_OVER_ROCK to the constructor.
Next checks for another fish (distinct from the parameter) that is in the same location as the parameter. If one is found, then the fish is not added to the list. Instead throws an IllegalFishPositionException, passing IllegalFishPositionException.TWO_FISH_IN_ONE_PLACE to the constructor.
Otherwise, adds the parameter to the fish list.
public java.util.ArrayList<Fish> getFish()
public java.util.ArrayList<Plant> getPlants()
public boolean getShape(int row, int col)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |