#include "FileMaker.h" #include #include "randgen.h" #include "apmatrix.h" FileMaker::FileMaker(apstring filename, int rows, int cols, int preds, int preys) // precondition: filename of file to create // rows > 0. The number of rows in the environment to be created // cols > 0. The number of columns in the environment to be created // preds >= 0. The number of predators to randomly place in the environment // preys >= 0. The number of prey to randomly place in the environment { if (preds + preys > rows * cols) { cout << "Too many fish to place in environment" << endl; return; } ofstream f; f.open(filename.c_str()); RandGen random; apmatrix ocean(rows,cols,0); int r, c; f << rows << " " << cols << endl; // Size of environment if (preds == 0) // Constant population - Add only prey { f << 0 << " " << 0 << " " << 0 << endl; // Prey species" preyBreed, preyStarve, preyLastMeal // There will not be any predators in this data file f << 0 << " " << 0 << " " << 0 << endl; // Pred species" preyBreed, preyStarve, preyLastMeal for (int k = 0; k < preys; k++) { do { r = random.RandInt(0,rows-1); c = random.RandInt(0,cols-1); } while (ocean[r][c] == 1); ocean[r][c] = 1; f << r << " " << c << " " << 0 << " " << 0 << endl; // row, col, age, speciesID } } else // Dynmanic population { // Prey species will breed 75% of the time f << 0.75 << " " << 0 << " " << 0 << endl; // Prey species" preyBreed, preyStarve, preyLastMeal // Predator species will breed 75% of the time // and will die if it hasn't eaten after 10 steps // and must breed within 5 days of eating f << 0.75 << " " << 10 << " " << 5 << endl; // Pred species" preyBreed, preyStarve, preyLastMeal // Add the prey and preds int preysLeft = preys; int predsLeft = preds; for (int k = 0; k < preys + preds; k++) { do { r = random.RandInt(0,rows-1); c = random.RandInt(0,cols-1); } while (ocean[r][c] == 1); ocean[r][c] = 1; // Pick prey or pred int newAge = random.RandInt(0,10); if ((predsLeft > 0 && random.RandInt(100) < 20) || preysLeft == 0) { // Add a pred predsLeft--; f << r << " " << c << " " << newAge << " " << 1 << endl; // row, col, age, speciesID } else { // Add a prey preysLeft--; f << r << " " << c << " " << newAge << " " << 0 << endl; // row, col, age, speciesID } } } f.close(); }