MINNOW PROJECT ADDITIONS
Breeding and Dying | MrMerlisFish | RightTurnFish | LeftTurnFish | HungryFish | SmellyFish |
EXTENSION 1: Breeding and Dying (called in the act method)
YOUR ACT METHOD Determine if you should breed. NOW DETERMINE IF YOU SHOULD DIE. |
BREEDING
DYING
EXTENSION 2: New Types of Fish
MrMerlisFish (subclass of Minnow)
RightTurnFish (subclass of Minnow)
LeftTurnFish (subclass of Minnow)
EXTENSION 3: Advanced Fish (EXTRA CREDIT!)
I WILL NOT PROVIDE YOU WITH HELP UNTIL YOU FIRST WRITE OUT AN OUTLINE (using pseudocode or entirely plain English) that describes the process you will use to accomplish this task. |
HungryFish (subclass of Minnow)
A HungryFish works as follows:
The fish starts out with a "reserve", set it to a 5. This is an instance variable of the HungryFish class. The goal of this fish is to eat any fish that is adjacent to it. When it eats, its reserve increases by 1. For every step that it does not eat, its reserve should decrease by 1. If the reserve reaches 0, the HungryFish dies.
This fish CAN go on locations fish currently exist (this is absolutely necessary to work correctly.) In doing so, remove the fish that is adjacent to the HungryFish, change the location to be in that spot, and increment the reserve.
Use the following method: /** * This method does the following: * If all adjacent locations are EMPTY, it should return its current location. * This will indicate to the calling method that there were no fish to eat. * If there IS A FISH TO EAT, this method will return the location of that fish. */ public Location fishToEat() |
EXTRA EXTRA CREDIT
Ensure that no HungryFish ever eats another HungryFish.
Hint: use the "instanceof" operator
if(fish[index] instanceof HungryFish)
EXTRA EXTRA EXTRA CREDIT
Make your fish smart by having it move towards the closest fish. In this regard, a HungryFish should never move randomly. You will have to create a method to identify the nearest location, and then have the fish move that way. Think of this as playing "Marco Polo". To test your code, you may want to put just two fish in the ocean, one HungryFish and one normal Minnow. It should always follow the Minnow.
If you can finish this last one, you are officially a programming expert!
---------------------------------
Check out the Tadpole program and try to make your fish go through a life cycle.
----------------------------------------------------------------------------------------------------------------
CREATE a new project called
"Minnow2" by copying your project.
This is because you're about to really chop up your code... and having a backup
is always nice. :)
SmellyFish (subclass of Minnow)
For the time being, simply make a constructor that calls
the super constructor.
You do not have to define ANY OTHER METHODS.
If a SmellyFish is in the environment, all other types of
Minnows will move AWAY from the SmellyFish.
In other words, if a SmellyFish is two to the left of a Minnow, even if the
left spot is empty, the Minnow will not move there.
GENERAL IDEA OF HOW TO DO THIS
Using the "instanceof" operator, create a method that iterates through all of the objects in the environment and populates an ArrayList of the locations of SmellyFish. (THINK before you ask for help on how to do this.) (THIS SHOULD BE DONE AS A STAND-ALONE METHOD that returns this ArrayList)
Then, inside of the "nextLocation()" method for Minnow, you will need to determine how far each of your possible moves to your emptyNeighbors is to the closest SmellyFish.
Again, this does not involve all that much code, YOU JUST NEED TO PLAN IT CAREFULLY.
You will want to create a method as seen below:
/**
* This method should return the number of moves it would take to move from mySpot
to anotherSpot
* (PLAN: One way to do this is to use the environment's getDirection
method. Since two objects can't
occupy
* the same spot, we at least know we have to move 'somewhere' to get to that
other location. Use a do while loop
* that: 1) gets the direction
you need to move to get closer to that location, 2) move once in that direction
* The condition should ask if the two locations are the same. Have a variable
that keeps count of the number of
* iterations of the loop. When the two locations are the same, return that number.
*/
public int numStepsToLocation(Location mySpot, Location anotherSpot)
You will need to keep track of which location to move to
brings you closest to a SmellyFish. Realize that you will need 2 looping mechanisms
here:
One that will loop through each SmellyFish's location (from the ArrayList of
SmellyFish locations) and an inner loop that will test each possible move (ie.
N, E, S, W for a Minnow without neighbors) against that Location of a SmellyFish.
Ultimately you should move to the location that takes you farthest from the SmellyFish.
ADDITIONAL METHODS
Overwrite the nextLocation inside of SmellyFish such that SmellyFish all try to move towards one another. Here, you will want to use your ArrayList of SmellyFish locations method to return the ArrayList of SmellyFish Locations. Then, use your mechanism for determining how far it is to the closest SmellyFish to choose your next location. This time, however, you want to move in the direction with the SMALLEST number of steps to that location.
THINGS TO THINK ABOUT
If you only have two SmellyFish on the screen, they will
try to move towards one another.
What if you have more than two SmellyFish? Will they all start to follow each
other?
Think specifically about the number of them: 3, 4, 5, 6, etc. Does it matter
how many there are?