/************************************************************************\ * Filename: creation.c * * Author: Sue Evans * * Date Written: 3/20/05 * * Section: 01XX & 02XX * * Email: bogar@cs.umbc.edu * * * * This file is meant to be compiled separately and linked with the * * students' proj3.c and wator.c files as a part of their project 3 * * assignment, WATOR. * * * * This file contains the function definitions of the function * * PopulateOcean() and it's helper functions: PlaceSharks(), PlaceTunas() * * and PlaceAlgae(), all of which depend upon the function * * GetRandomNumber(), also found in this file. * * * * The functions in this file will allow the user to originally populate * * the ocean with creatures in the numbers passed in. They will be * * randomly placed within the ocean and will have varying times until * * starvation, reproduction or growth. These times will vary from 1/2 * * the starve, repro or grow times to the full starve, repro or grow * * times. This will populate the ocean so that no individual will be on * * the verge of starvation or about to reproduce when the simulation * * begins. * \************************************************************************/ #include #include "wator.h" #include "creation.h" /* seed to be used for creation ONLY. A new seed is to be read in for the simulation itself */ #define SEED 42 /******************** * PopulateOcean() * Inputs: ocean - a 2D array of CELLs of size ROWS X COLS to be * populated with lifeforms in this function * rows - integer number of rows in the array, ocean * cols - integer number of columns in the array, ocean * numSharks - integer number of sharks to have initially * numTunas - integer number of tunas to have initially * numAlgae - integer number of algae to have initially * starveShark - integer number of timeclicks until starvation * for a shark * starveTuna - integer number of timeclicks until starvation * for a tuna * reproShark - integer number of timeclicks until reproduction * for a shark * reproTuna - integer number of timeclicks until reproduction * for a tuna * grow - integer number of timeclicks until growing for * an alga * * Output: None, but the contents of the array, ocean, is modified by the * function. * * Assumptions : Relies on #define of COLS * Relies on typedefs of CELL, SHARK, TUNA, and ALGA * * PopulateOcean() populates the array, ocean, passed into it with CELLs * that contain lifeforms in the quantities passed in as the parameters * numSharks, numTunas and numAlgae. Each of the individuals are placed in * random positions in the ocean. These lifeforms will be initialized by this * function to have varying times until reproduction and starvation for all * fish and varying times until growth for algae. All of these times are * within the maximum time limits passed into the function as the parameters * starveShark, starveTuna, reproShark, reproTuna and grow. ********************/ void PopulateOcean(CELL ocean[][COLS], int rows, int cols, int numSharks, int numTunas, int numAlgae, int starveShark, int starveTuna, int reproShark, int reproTuna, int grow) { srand(SEED); ClearOcean (ocean, rows, cols); PlaceSharks(ocean, rows, cols, numSharks, reproShark, starveShark); PlaceTunas (ocean, rows, cols, numTunas, reproTuna, starveTuna); PlaceAlgae (ocean, rows, cols, numAlgae, grow); } /******************** * ClearOcean () * Inputs: 2D array of CELLs called ocean, and its size: an integer # of rows and an integer # of columns * Output: None, but the array passed in is modified * * ClearOcean() fills the array passed in with 0s in all of the cells * members. ********************/ void ClearOcean(CELL ocean[][COLS], int rows, int cols) { int i, j; CELL cell; /* initialize the memory space for one of each type of creature, meaning there is no creature */ SHARK shark = {0, 0, 0, 0, 0}; TUNA tuna = {0, 0, 0, 0, 0}; ALGA alga = {0, 0}; /* copy the "empty" creatures into a cell meaning there are no creatures in this cell */ cell.s = shark; cell.t = tuna; cell.a = alga; /* fill the ocean with empty cells */ for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { ocean[i][j] = cell; } } } /******************** * GetRandomNumber() * * Inputs: min - integer minimum value desired * max - integer maximum value desired * * Output: a random number in the range min - max, inclusive * * GetRandomNumber() returns a random number in the range of * min to max, integer values that are passed into the function. ********************/ int GetRandomNumber(int min, int max) { int r ; /* Scale the random number within range. */ r = rand() % (max - min + 1) + min ; return r ; } /******************** * PlaceSharks() * Inputs: 2D array of CELLs called ocean, and its size: * an integer # of rows and an integer # of columns, * an integer number of SHARKs to place, an integer * number of time clicks before reproduction, and an * integer number of time clicks before starvation. * Output: None, but the array passed in is modified * * PlaceSharks() places the number of SHARKs passed in at random positions * within the ocean. The time clicks until reproduction is a random number * between half the reproShark and the maximum reproShark that was passed in. * Likewise the time clicks until starvation is a random number between half * the starveShark and the maximum starveShark passed in. ********************/ void PlaceSharks(CELL ocean[][COLS], int rows, int cols, int numSharks, int reproShark, int starveShark) { int i, j, k; /* for each of the sharks */ for(k = 0; k < numSharks; k++) { /* get a random location */ i = GetRandomNumber(0, rows - 1); j = GetRandomNumber(0, cols - 1); /* if already occupied, get a new location */ while(ocean[i][j].s.is) { i = GetRandomNumber(0, rows - 1); j = GetRandomNumber(0, cols - 1); } /* write shark info into that cell */ ocean[i][j].s.is = 1; ocean[i][j].s.reproduce = GetRandomNumber(reproShark / 2, reproShark); ocean[i][j].s.starve = GetRandomNumber(starveShark / 2, starveShark); } } /******************** * PlaceTunas() * Inputs: 2D array of CELLs called ocean, and its size: * an integer # of rows and an integer # of columns, * an integer number of TUNAs to place, an integer * number of time clicks before reproduction, and an * integer number of time clicks before starvation. * Output: None, but the array passed in is modified * * PlaceTunas() places the number of TUNAs passed in at random positions * within the ocean. Tunas are not placed in the same CELLS as SHARKs, but * a new position is determined for their placement. The time clicks until * reproduction is a random number between half the reproTuna and the maximum * reproTuna that was passed in. Likewise the time clicks until starvation is * a random number between half the starveTuna and the maximum starveTuna * passed in. ********************/ void PlaceTunas(CELL ocean[][COLS], int rows, int cols, int numTunas, int reproTuna, int starveTuna) { int i, j, k; /* for each of the tunas */ for(k = 0; k < numTunas; k++) { /* get a random location */ i = GetRandomNumber(0, rows - 1); j = GetRandomNumber(0, cols - 1); /* if a shark or a tuna is already there, get a new location */ while(ocean[i][j].s.is || ocean[i][j].t.is) { i = GetRandomNumber(0, rows - 1); j = GetRandomNumber(0, cols - 1); } /* write tuna info into that cell */ ocean[i][j].t.is = 1; ocean[i][j].t.reproduce = GetRandomNumber(reproTuna / 2, reproTuna); ocean[i][j].t.starve = GetRandomNumber(starveTuna / 2, starveTuna); } } /******************** * PlaceAlgae() * Inputs: 2D array of CELLs called ocean, and its size: * an integer # of rows and an integer # of columns, * an integer number of ALGAe to place, and an * integer number of time clicks before growth. * Output: None, but the array passed in is modified * * PlaceAlgae() places the number of ALGAe passed in at random positions * within the ocean. Algae are not placed in the same CELLS as TUNAs, but * a new position is determined for their placement. The time clicks until * growth is a random number between half the grow and the maximum grow that * was passed in. ********************/ void PlaceAlgae(CELL ocean[][COLS], int rows, int cols, int numAlgae, int grow) { int i, j, k; /* for each alga */ for(k = 0; k < numAlgae; k++) { /* get a random location */ i = GetRandomNumber(0, rows - 1); j = GetRandomNumber(0, cols - 1); /* if algae or a tuna is already there, get a new location */ while(ocean[i][j].a.is || ocean[i][j].t.is) { i = GetRandomNumber(0, rows - 1); j = GetRandomNumber(0, cols - 1); } /* write alga info into that cell */ ocean[i][j].a.is = 1; ocean[i][j].a.grow = GetRandomNumber(grow / 2, grow); } }