// APCS
// 2001 AP Computer Sceince AB Free Response
// Question # 2
//
////////////////// a)
// Look for the fish in the environment,
// remove it from the fishlist, resize the fishlist,
// and update myFishCount.
void Environment::RemoveFish(const Position & pos)
{
int j;
apvector <Fish> fishList;
fishList = AllFish();
if(isEmpty(pos))
{
cerr << "error - attempt to remove nonexistent fish at:"
<< pos <<endl;
return;
}
// begin
while(fishList[j] != myworld[pos.Row()][pos.Col()])
j++;
fishList[j] = fishList[myFishCount-1];
fishList[j].resize(myFishCount-1);
myFishCount--; // current fish in pool not including dead
// while myFishCreated is total ever lived, including dead
}
////////////////// b)
// Gets the fishlist from the accessor function to the
// environment class, finds out if each of the neighboring
// positions in the environment is empty or not, and if
// it is empty then go ahead an add the fish at that
// position. The age and probability of the fish dying
// is assumed to be initialized with the Fish class
// constructor.
void Fish::Breed(Environment & env)
{
int j;
apvector <Fish> fishList;
while(fishList[j].Location() != myPos)
j++;
if(!env.IsEmpty(fishList[j].Location().North()))
env.AddFish(fishList[j].Location().North());
else if (!env.IsEmpty(fishList[j].Location().South()))
env.AddFish(fishList[j].Location().South());
else if(!env.IsEmpty(fishList[j].Location().East()))
env.AddFish(fishList[j].Location().East());
else if(!env.IsEmpty(fishList[j].Location().West()))
env.AddFish(fishList[j].Location().West());
}
///////////////// c)
// Assume that the integer myAge is a private
// variable of the Fish class, Age()
// calls that variable, and it is initialized
// as 0 with the constructor. myProbDie is also
// initialized as whatever it is supposed to be.
void Fish::Act(Environment & env)
{
int j;
RandGen randdie;
apvector <Fish> fishList;
while(fishList[j].Location() != myPos)
j++;
if(randdie.RandInt(0, myProbDie*2) == myProbDie)
env.RemoveFish(fishList[j].Location());
else if (fishList[j].Age() == 3)
fishList[j].Breed(env);
else
fishList[j].Age()++;
}
|