Question: Create the classes Critter , Lion and Zebra in C++ , following the programming requirements listed below. Critter is a base class used to represent
Create the classes Critter, Lion and Zebra in C++, following the programming requirements listed below.
Critter is a base class used to represent the common data members and methods shared by predators and prey. It also has some abstract methods that must be overridden by its child classes.
Zebras mainly move around and reproduce. There is grass everywhere, so they dont have to worry about eating.
Lions have to worry about how many consecutive meals they have missed. If they miss too many, they will die. Zebras are tasty treats.Lions move around and reproduce as well.
Your Critter class must have the following methods.
Critter( int x, int y, int level ); Sets the data members correctly. All critters are created alive. Allows any non-negative value for x and y. Any values less than 0 should be replaced with 0.
virtual ~Critter( ); The destructor is required, but has and empty block of code for its body.
int getX( ) const; Returns the x position of the critter.
int getY( ) const; Returns the y position of the critter.
int getFoodChainLevel( ) const; Return the food chain level.
bool isAlive( ) const; Return the alive status of the critter.
bool kill( ); If the critter is alive, make it not alive. Return true if the critter is made not alive. Otherwise, return false.
void setPosition( int x, int y ); Sets the position of the critter. Assumes x and y are allowed, does not verify them.
bool positionAvailable( int x, int y, std::vector< Critter* >& critters, int width, int height ); Returns trueif position x,y is a legal position and unoccupied. Otherwise, returns false.
virtual bool move( std::vector< Critter* >& critters, int width, int height ); Randomly chooses one of the neighboring empty locations and moves there. If no locations are empty, do not move. Do not move to a negative x or y position. Do not move to x = width or y = height. Returns true if moved. Otherwise, returns false.
virtual bool eat( std::vector< Critter* >& critters ) = 0; Abstract method, each critter type eats in its own way.
virtual bool reproduce( std::vector< Critter* >& critters ) = 0; Abstract method, each critter type reproduces in its own way.
Your Lion class must have the following methods.
Lion( int x, int y ); All Lions have a food chain level of 10. Initialize the consecutively missed meal count to 0.
virtual ~Lion( ); Required, with empty code block for body.
int getMissedMealCount( ) const; Returns the number of consecutive missed meals.
Critter *findNeighborPrey( std::vector< Critter* >& critters ) const; Find the first critter in the vector that is alive, has a lower food chain level than the lion, and is next to the lion, either vertically or horizontally. If no such critter is found, return the null pointer (0).
virtual bool eat( std::vector< Critter* >& critters ); If no Zebras are nearby, the Lion will miss a meal. If the Lionhas missed 3 or more meals, then the Lion dies. If there is a Zebra next to the Lion, then the Lion moves to the location of the Zebra, the Lion will eat the Zebra and the Zebra dies. This should reset the number of consecutively missed meals to 0. Returns true if the Lion eats, otherwise returns false.
virtual bool reproduce( std::vector< Critter* >& critters ); Returns false, for now. Youll need to have a statement: (void)critters in the body to remove the compiler warnings.
Your Zebra class must have the following methods.
Zebra( int x, int y ); All Zebras have a food chain level of 5.
virtual ~Zebra( ); Required, with empty code block for body.
virtual bool eat( std::vector< Critter* >& critters ); Always returns false. Youll need to have a statement: (void)critters in the body to remove the compiler warnings.
virtual bool reproduce( std::vector< Critter* >& critters ); Returns false, for now. Youll need to have a statement: (void)critters in the body to remove the compiler warnings.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
