#include #include #include using namespace std; ////////////////////// // Global constants // Change these to change the simulation parameters // Starfish related // the biggest size a starfish can reach const int maxsize = 5; // the size a starfish is when it can first breed const int breedsize = 3; // Plot related // the maximum number of barnacles in a plot const int maxbarnacles = 10; // Seashore related const int seashoresize = 10; const int breedingperiod = 10; // end Global constants ////////////////////// ////////////////////// // Function Prototypes int RandomNumber(int, int); ////////////////////// // Starfish Class Definition class Starfish{ public: Starfish(); int getSize(); void grow(); void feed(); bool starve(); int breed(); Starfish *getNextStarfish(); void setNextStarfish(Starfish *); private: int size; bool eaten; Starfish *nextStarfish; }; // Starfish start with size 1 and having eaten Starfish::Starfish() // return the current size of the starfish int Starfish::getSize() // If the starfish is not as large as it can get // then it grows by one void Starfish::grow() // If the starfish gets to feed, // then it has eaten void Starfish::feed() // This method checks to see if the seafish // dies of starvation. If it has eaten, then it // does not die, but no longer has eaten. If it // has not eaten, then it dies. This function returns // true if the starfish starves to death. bool Starfish::starve() // If the starfish is of breeding size // and has eaten, then it can breed // It produces a random number of larva // between zero and its own size. The // number of larva is returned. int Starfish::breed()} // Returns the next Starfish on the list Starfish *Starfish::getNextStarfish() // Sets the next starfish on the list void Starfish::setNextStarfish(Starfish *next) // end Starfish Class ////////////////////// ////////////////////// // Plot Class Definition class Plot{ public: Plot(); Starfish *advanceTime(); void addStarfish(Starfish *); int getNumStarfish(); int getNumBarnacles(); int spawnStarfish(); ~Plot(); private: // private variables int barnacles; Starfish *starfishes; // private methods Starfish *feedStarfish(); }; // Constructor // Plots start off full of barnacles // and empty of starfish Plot::Plot() // In each time unit, Starfish will eat barnacles. // - Each starfish will eat one barancle. // - Starfish eat in the order they arrived. // - Starfish that get to eat grow. // - Starfish that do not get to eat are returned on a linked list, as // they need to move to some other plot // - If there is at least one barnacle, another will grow // - If there are no barnacles, none will grow if there are any // starfish present, otherwise one will grow. Starfish * Plot::advanceTime() // Add a starfish to the plot. Best to add it // at the end of the list, so it goes last at feeding time void Plot::addStarfish(Starfish * newstarfish) // returns the number of starfish in the plot // need to count how many are on the list int Plot::getNumStarfish() // returns the number of barnacles in the plot int Plot::getNumBarnacles() // go through the list of starfish // breed each one individually, but return the total // larva produced int Plot::spawnStarfish() // Need to define a destructor to clean up the // list of Starfishes Plot::~Plot() // end Plot Class Definition ////////////////////// ////////////////////// // Seashore Class Definition class Seashore{ public: Seashore(); void advanceTime(); int getTime(); int totalBarnacles(); int totalStarfish(); void seedStarfish(int); private: int time; vector shoreline; }; // Default constructor // set the initial time and vector size Seashore::Seashore() //return the simulation time int Seashore::getTime() // For each time unit // Go through the vector of plots // For each plot // -advance time // - if it is time to spawn, spawn each plot and count larva // -go through the list that is returned // and see if each starfish starves. // - if it doesn't, it moves up or down one plot randomly // - Spawn the new starfish by calling seedStarfish after starfish // have moved. void Seashore::advanceTime() // returns the total number of barnacles on the seashore int Seashore::totalBarnacles() // returns the total number of barnacles on the seashore int Seashore::totalStarfish() // Allow a few starfish to be added to the shoreline // to get the simulation started and to account for // breeding void Seashore::seedStarfish(int numLarva) // end Seashore Class Definition ////////////////////// ////////////////////// // Seashore Simulation Class Definition class Seashore_sim{ public: Seashore_sim(); void run(int); private: Seashore theseashore; }; // construct the seashore Seashore_sim::Seashore_sim(){ } // Run the simulation // this advaces the time on the seashore maxtime times // and counts the starfish and barnacles each time void Seashore_sim::run(int maxtime) // end Seashore Simulation Class Definition ////////////////////// ////////////////////// // StarfishGraph class class StarfishGraph{ public: StarfishGraph(int screenwidth = 73); void addPoint(int,int); void graph(); private: vector barnacles; vector starfish; int screenwidth; }; // Not much to do here StarfishGraph::StarfishGraph(int screen){ screenwidth = screen; } void StarfishGraph::addPoint(int barn, int star){ // add entries to the vectors barnacles.push_back(barn); starfish.push_back(star); } void StarfishGraph::graph(){ // find the largest element to set the scale int biggest = 0; for (int i = 0; i < barnacles.size(); i++){ if (barnacles[i] > biggest) biggest = barnacles[i]; if (starfish[i] > biggest) biggest = starfish[i]; } // now that we know the biggest, set the scale // so that things fit on the screen float scale = (float) screenwidth / biggest; // output the header line cout <<"Time" << setw(45) << "[Barnacle = b Starfish = * Points Cross = +]" << endl; // now go through the vectors and print out the positions // of the starfish and barnacles int starpos, barnpos; for (int i = 0; i < starfish.size(); i++){ cout << setw(4) << i << "|"; //compute where we will print the marks starpos = static_cast (starfish[i] * scale); barnpos = static_cast (barnacles[i] * scale); // now print the right number of spaces and marks for (int x = 0; x <= screenwidth; x++){ if (starpos != barnpos){ if (x == starpos) cout << "*"; else if (x == barnpos) cout << "b"; else cout << " "; } // if both numbers are in the same spot print a + else { if (x == starpos) cout << "+"; else cout << " "; } } cout << endl; } // Now output the bottom scale // to show the numbers cout << " "; for (int i = 0; i < screenwidth; i++) if (i % 5 == 0) cout << "+"; else cout <<"-"; cout << endl; for (int i = 0; i < screenwidth; i++) if ((i % 5 == 0)||(i == 0)) cout << setw(5) << (int) (i / scale); cout << endl; } // end Graph class ////////////////////// ////////////////////// // Main function int main(){ // seed the random number generator // Don't touch this! ;) long ltime = time(NULL); int stime = (unsigned) ltime/2; srand(stime); //start the simulation Seashore_sim simulation; simulation.run(100); return 0; } ////////////////////// // Function declarations // this is ugly, but it works well enough for this project int RandomNumber(int lowest, int highest){ int bin = 0; int numPossResults = highest - lowest + 1; int portion = 10000/numPossResults; int intermediate = (int) (((float) rand()/RAND_MAX) * (10000)); while ((intermediate - portion) > 0){ intermediate -= portion; bin++; } return bin + lowest; }