Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- //Assignment 2
- //Created by Maggie Parkhurst-Bartel, Asal Nasiri, Faris Parkar and Jason
- //November 25th 2014
- //This program models the evolution of a population of foxes and rabbits
- //structure of the foxes, contains position, level of hunger and living status
- struct Fox
- {
- int xIndex;
- int yIndex;
- int fedLevel;
- bool alive;
- };
- //the structure of the rabbits, contains position, age, and living status
- struct Rabbit
- {
- int xIndex;
- int yIndex;
- int age;
- bool alive;
- };
- #define MAX_FOXES_PER_GRID 9
- #define MAX_RABBITS_PER_GRID 20
- void PrintGrids(int **FoxBoard, int **RabbitBoard, ostream& outStream, int numRows, int numCols);
- bool RabbitStep(int **rabbitBoard, Rabbit *listOfRabbits, int& numberRabbitsTotal, int& numberRabbitsAlive,
- int numRows, int numCols, int rabbitListLength,int maxNumRabbitsPerGrid, int numChildren);
- void FoxStep(int **foxBoard, Fox *listOfFoxes, int **rabbitBoard, Rabbit *listOfRabbits, int numberFoxesTotal,
- int& numberFoxesAlive, int numberRabbitsTotal, int& numberRabbitsAlive, int numRows, int numCols,
- int foxListLength, int rabbitListLength);
- int findNextLiveFox( Fox *foxList, int Row, int Col, int numberFoxesTotal, int& lastFoxIndex );
- int FindNextLiveRabbit( Rabbit *rabbitList, int Row, int Col, int numberRabbitsTotal, int& nextRabbitIndex );
- int main()
- {
- Fox *foxList = NULL;
- Rabbit *rabbitList = NULL;
- ofstream outfile;
- char outputName[80] = {'\0'};
- int numRows = 0;
- int numCols = 0;
- int numGen = 0;
- int seed = 0;
- int maxChildren = 0;
- double fracFox = 0.0;
- double fracRabbit = 0.0;
- int maxFoxes = 0;
- int maxRabbits = 0;
- int **foxGrid = NULL;
- int **rabbitGrid = NULL;
- int XX = 0;
- double increment = 0.0;
- double prob = 0.0;
- int count = 0; int count2= 0;
- int j = 0;
- int k = 0;
- int x = 0;
- int y = 0;
- double firstFract = 0.0;
- int rabbitsLive = 0;
- int rabbitsTotal = 0;
- int foxesLive = 0;
- int foxesTotal = 0;
- int rabbitListLength = 0;
- int foxListLength = 0;
- bool rabBool = 0;
- //prompts for name of output file
- cout << "Enter the name of the output file: ";
- cin >> outputName;
- outfile.open(outputName);
- //if its fails opening, enters this loop
- while (outfile.fail())
- {
- //if it has failed to open six times
- if (count == 5)
- {
- cerr << "ERROR: could not open output file after 6 tries";
- return 1;
- }
- //prompts you to enter an output name again and attempts to open it
- else
- {
- cerr << "ERROR: output file not opened correctly" << endl;
- cout << "Enter the name of the output file: ";
- cin >> outputName;
- }
- count++;
- }
- //prompts for the number of rows
- cout << "Enter the number of rows in the simulation grid: ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it has tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read number of rows after 6 tries";
- return 2;
- }
- //if the value entered is not an integer
- else if (!(cin >> numRows))
- {
- cerr << "ERROR: Cannot read number of rows" << endl;
- cerr << "Enter the number of rows in the simulation grid: ";
- cin >> numRows;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (numRows <= 0 || numRows >= 15)
- {
- cerr << "ERROR: Read an illegal number of rows for the board" << endl;
- cerr << "Enter the number of rows in the simulation grid: ";
- cin >> numRows;
- count++;
- continue;
- }
- //if the value entered for numRows is valid
- else
- {
- break;
- }
- }
- //prompts for the number of columns
- cout << "Enter the number of columns in the simulation grid: ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it has tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read number of columns after 6 tries";
- return 3;
- }
- //if the value entered was not an integer
- else if (!(cin >> numCols))
- {
- cerr << "ERROR: Cannot read number of columns" << endl;
- cerr << "Enter the number of columns in the simulation grid: ";
- cin >> numCols;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (numCols <= 0 || numCols >= 15)
- {
- cerr << "ERROR: Read an illegal number of columns for the board" << endl;
- cerr << "Enter the number of columns in the simulation grid: ";
- cin >> numCols;
- count++;
- continue;
- }
- //if the value entered for numCols is valid
- else
- {
- break;
- }
- }
- //prompts for the number of generations
- cout << "Enter the number of generations: ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it has tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read number of generations after 6 tries";
- return 4;
- }
- //if the value entered was not an integer
- else if (!(cin >> numGen))
- {
- cerr << "ERROR: Cannot read number of generations" << endl;
- cerr << "Enter the number of generations in the simulation grid: ";
- cin >> numGen;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (numGen <= 0 || numGen >= 15)
- {
- cerr << "ERROR: Read an illegal number of generations for the board" << endl;
- cerr << "Enter the number of generations in the simulation grid: ";
- cin >> numGen;
- count++;
- continue;
- }
- //if the value entered for generations is valid
- else
- {
- break;
- }
- }
- //prompts for the value of the the seed
- cout << "Enter the seed for the random number generator: ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it has tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read number of generations after 6 tries";
- return 5;
- }
- //if the value entered was not an integer
- else if (!(cin >> seed))
- {
- cerr << "ERROR: Cannot read the seed for the random number generator" << endl;
- cerr << "Enter the seed for the random number generator: ";
- cin >> seed;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (seed <= 0 || seed >= RAND_MAX)
- {
- cerr << "ERROR: Read an illegal seed" << endl;
- cerr << "Enter the seed for the random number generator: ";
- cin >> seed;
- count++;
- continue;
- }
- //if the value entered is valid
- else
- {
- break;
- }
- }
- //prompts for the fraction value of squares containing foxes
- cout << "Enter the initial fraction of squares in the grid containing foxes: ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read the initial fraction of squares in the grid containing no foxes after 6 tries";
- return 5;
- }
- //if the value entered was not a double or integer
- else if (!(cin >> fracFox))
- {
- cerr << "ERROR: Cannot read the initial fraction of squares in the grid containing foxes" << endl;
- cerr << "Enter the initial fraction of squares in the grid containing foxes: ";
- cin >> fracFox;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (fracFox <= 0.05 || fracFox >= 0.95)
- {
- cerr << "ERROR: Read an illegal initial fraction of squares in the grid with foxes" << endl;
- cerr << "Enter the initial fraction of squares in the grid containing foxes: ";
- cin >> fracFox;
- count++;
- continue;
- }
- //if the value entered was valid
- else
- {
- break;
- }
- }
- //prompts for the fraction value of squares containing rabbits
- cout << "Enter the initial fraction of squares in the grid containing rabbits: ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read the initial fraction of squares in the grid containing no rabbits after 6 tries";
- return 6;
- }
- //if the value entered was not a double or integer
- else if (!(cin >> fracRabbit))
- {
- cerr << "ERROR: Cannot read the initial fraction of squares in the grid containing rabbits" << endl;
- cerr << "Enter the initial fraction of squares in the grid containing rabbits: ";
- cin >> fracRabbit;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (fracRabbit <= 0.05 || fracRabbit >= 0.95)
- {
- cerr << "ERROR: Read an illegal initial fraction of squares in the grid containing foxes " << endl;
- cerr << "Enter the initial fraction of squares in the grid containing rabbits: ";
- cin >> fracRabbit;
- count++;
- continue;
- }
- //if the value entered was valid
- else
- {
- break;
- }
- }
- //prompts for the initial maximum number of foxes per grid
- cout << "Enter the initial maximum number of foxes per grid: ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read the initial maximum number of foxes per square after 6 tries";
- return 7;
- }
- //if the value entered was not an integer
- else if (!(cin >> maxFoxes))
- {
- cerr << "ERROR: Cannot read the initial maximum number of foxes per grid " << endl;
- cerr << "Enter the initial maximum number of foxes per grid: ";
- cin >> maxFoxes;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (maxFoxes <= 0 || maxFoxes > MAX_FOXES_PER_GRID)
- {
- cerr << "ERROR: Read an illegal initial maximum number of foxes per square" << endl;
- cerr << "Enter the initial maximum number of foxes per grid: ";
- cin >> maxFoxes;
- count++;
- continue;
- }
- //if the value entered was valid
- else
- {
- break;
- }
- }
- //prompts for maximum number of rabbits per grid
- cout << "Enter the initial maximum number of rabbits per grid ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it has tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read the initial maximum number of rabbits per grid after 6 tries";
- return 8;
- }
- //if value entered was not an integer
- else if (!(cin >> maxRabbits))
- {
- cerr << "ERROR: Cannot read the initial maximum number of rabbits per grid" << endl;
- cerr << "Enter the initial maximum number of rabbits per grid ";
- cin >> maxRabbits;
- count++;
- continue;
- }
- //if value entered exceeds the bounds
- else if (maxRabbits <= 0 || maxRabbits > MAX_RABBITS_PER_GRID)
- {
- cerr << "ERROR: Read an illegal initial maximum number of rabbits per grid" << endl;
- cerr << "Enter the initial maximum number of rabbits per grid ";
- cin >> maxRabbits;
- count++;
- continue;
- }
- //if the value entered is valid
- else
- {
- break;
- }
- }
- //prompts for the maximum number of children
- cout << "Enter the maximum number of children produced when rabbits breed ";
- count = 1;
- //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
- while (count <= 7)
- {
- //if it has tried and failed to open 6 times
- if (count == 6)
- {
- cerr << "ERROR: could not read the maximum number of children produced when rabbits breed after 6 tries";
- return 9;
- }
- //if the value entered was not an integer
- else if (!(cin >> maxChildren))
- {
- cerr << "ERROR: Cannot read the maximum number of children produced when rabbits breed" << endl;
- cerr << "Enter the maximum number of children produced when rabbits breed ";
- cin >> maxChildren;
- count++;
- continue;
- }
- //if the value entered exceeds the bounds
- else if (maxChildren <= 0 || maxChildren > MAX_RABBITS_PER_GRID)
- {
- cerr << "ERROR: Read an illegal maximum number of children produced when rabbits breed" << endl;
- cerr << "Enter the maximum number of children produced when rabbits breed ";
- cin >> maxChildren;
- count++;
- continue;
- }
- //if the value entered is valid
- else
- {
- break;
- }
- }
- cout << endl;
- outfile << endl;
- /////////////////////////////////////////////////////////////////////////DONE INITIALIZATION///////////////////////////////////////////////////////////////
- //creates a pointer array of numRows spaces
- foxGrid = new int* [numRows];
- //pointer array just created goes through a loop where each space in the array
- //points at a different array
- for (int i = 0; i < numRows; i++)
- {
- foxGrid[i] = new int [numCols];
- }
- //seeds the rand function
- srand(seed);
- //this for loop goes through each space in the 2D array, generates a random number,
- //and turns that number into a value between 0 and 9 inclusive
- for (int i = 0; i < numRows; i++ )
- {
- for (j = 0; j < numCols; j++ )
- {
- foxGrid[i][j] = maxFoxes;
- k = rand();
- firstFract = 1-fracFox;
- for (int nFox = 0; nFox <= maxFoxes; nFox++)
- {
- if ((double)k < firstFract * RAND_MAX)
- {
- foxGrid[i][j] = nFox;
- break;
- }
- firstFract = (1-fracFox)+ (fracFox * (nFox+1))/maxFoxes;
- }
- }
- }
- //creates a 1D array of type Fox that keeps track of the Foxes and the number thereof
- foxList = new Fox [numRows*numCols*maxFoxes*2];
- foxListLength = numRows*numCols*maxFoxes*2;
- //initialises each alive fox in the array foxlist
- y = 0;
- for (j = 0; j < numRows; j++)
- {
- for (k = 0; k < numCols; k++)
- {
- for (x = 0; x < foxGrid[j][k]; x++)
- {
- foxList[y].xIndex = k;
- foxList[y].yIndex = j;
- foxList[y].fedLevel = rand()%10;
- foxList[y].alive = true;
- y++;
- foxesLive++;
- foxesTotal++;
- }
- }
- }
- //creates a pointer array of size numRows
- rabbitGrid = new int* [numRows];
- //the array created above goes through this loop and each space
- //in the array points to a different array
- for (int i = 0; i < numRows; i++)
- {
- rabbitGrid[i] = new int [numCols];
- }
- //loop goes through each space in the 2D array rabbitGrid, generates a random number,
- //changes that number to a number between 0 and maxRabbits inclusive
- for (int i = 0; i < numRows; i++)
- {
- for (j = 0; j < numCols; j++)
- {
- rabbitGrid[i][j] = maxRabbits;
- k = rand();
- firstFract = 1-fracRabbit;
- for (int nRabbit = 0; nRabbit <= maxRabbits; nRabbit++)
- {
- if ((double)k < firstFract * RAND_MAX)
- {
- rabbitGrid[i][j] = nRabbit;
- break;
- }
- firstFract = (1-fracRabbit)+ (fracRabbit * (nRabbit+1))/maxRabbits;
- }
- }
- }
- //creates an array of type Rabbit
- rabbitList = new Rabbit[numRows*numCols*maxRabbits*20];
- rabbitListLength = numRows*numCols*maxRabbits*20;
- y = 0;
- for (j = 0; j < numRows; j++)
- {
- for (k = 0; k < numCols; k++)
- {
- for (x = 0; x < rabbitGrid[j][k]; x++)
- {
- rabbitList[y].xIndex = k;
- rabbitList[y].yIndex = j;
- rabbitList[y].age = rand()%10;
- rabbitList[y].alive = true;
- y++;
- rabbitsLive++;
- rabbitsTotal++;
- }
- }
- }
- cout << "After initialization" << endl;
- outfile << "After initialization" << endl;
- //prints the initial grid to screen and outfile
- PrintGrids(foxGrid, rabbitGrid, cout, numRows, numCols);
- PrintGrids(foxGrid, rabbitGrid, outfile, numRows, numCols);
- //calls the functions foxstep and rabbitstep for each generation and prints it to the outfile
- for (int g = 0; g < numGen; g++)
- {
- rabBool = RabbitStep(rabbitGrid, rabbitList, rabbitsTotal, rabbitsLive, numRows, numCols, rabbitListLength, MAX_RABBITS_PER_GRID, maxChildren);
- if (rabBool == false)
- {
- cerr << "ERROR: Rabbit function returned false, could not be completed";
- return 11;
- }
- FoxStep(foxGrid, foxList, rabbitGrid, rabbitList, foxesTotal, foxesLive, rabbitsTotal, rabbitsLive, numRows, numCols, foxListLength, rabbitListLength);
- //if it is at the last generation then it prints it to the screen and the outfile
- if (g == numGen - 1)
- {
- cout << "After foxes move and eat" << endl;
- outfile << "After foxes move and eat" << endl;
- PrintGrids(foxGrid, rabbitGrid, cout, numRows, numCols);
- PrintGrids(foxGrid, rabbitGrid, outfile, numRows, numCols);
- }
- else
- {
- outfile << "After foxes move and eat" << endl;
- PrintGrids(foxGrid, rabbitGrid, outfile, numRows, numCols);
- }
- }
- outfile.close();
- delete foxList;
- delete rabbitList;
- delete foxGrid;
- delete rabbitGrid;
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////USER DEFINED FUNCTIONS//////////////////////////////////////////////////////////
- //this function prints the current iteration of the board to an outstream (be it cout, outfile, etc)
- void PrintGrids(int **FoxBoard, int **RabbitBoard, ostream& outStream, int numRows, int numCols)
- {
- for (int j = 0; j < numRows; j++)
- {
- for (int k = 0; k < numCols; k++)
- {
- outStream << setw(3) << FoxBoard[j][k] << "," << setw(2) << RabbitBoard[j][k];
- }
- outStream << endl;
- }
- outStream << endl << endl;
- }
- //this function implements the deaths and births of the rabbits, will change the number of rabbits in each grid space
- bool RabbitStep(int **rabbitBoard, Rabbit *listOfRabbits, int& numberRabbitsTotal, int& numberRabbitsAlive,
- int numRows, int numCols, int rabbitListLength, int maxNumRabbitsPerGrid, int numChildren)
- {
- int j = 0;
- int k = 0;
- int i = 0;
- int live = 0;
- int next = 0;
- int newChildren = 0;
- //points to y index
- for (j = 0; j < numRows; j++)
- {
- //points to x index
- for (k = 0; k < numCols; k++)
- {
- //this makes sure that each time you start looking in a different grid space, that you start at the beginning of the rabbit list array
- next = 0;
- //goes through each alive rabbit in the grid space
- for (i = 0; i < rabbitBoard[j][k]; i++)
- {
- live = FindNextLiveRabbit(listOfRabbits, j, k, numberRabbitsTotal, next);
- //if a live rabbit was found, the value of its age increments
- if (live != -1)
- {
- listOfRabbits[live].age++;
- //the rabbit may only live from ages 1 to 10 inclusive, if its age reaches 11, it dies
- if (listOfRabbits[live].age == 11)
- {
- listOfRabbits[live].alive = false;
- numberRabbitsAlive--;
- rabbitBoard[j][k]--;
- }
- }
- //if it couldn't find a live rabbit then it breaks, so it can check the next grid space for a live rabbit
- else
- {
- break;
- }
- }
- }
- }
- //points to y index
- for (j = 0; j < numRows; j++)
- {
- //points to x index
- for (k = 0; k < numCols; k++)
- {
- newChildren = 0; //resets the value of newChildren for each loop, newChildren is only relevant for its own grid space
- //if space is full, then no children are born
- if (rabbitBoard[j][k] == maxNumRabbitsPerGrid || rabbitBoard[j][k] == 0 || rabbitBoard[j][k] == 1)
- {
- newChildren = 0;
- continue;
- }
- else
- {
- //loops for number of pairs
- for (i = 0; i < (int)(rabbitBoard[j][k]/2); i++)
- {
- newChildren = newChildren + ((rand() % numChildren) + 1);
- }
- }
- //if newChildren is less than the available space, then all the newChildren are able to be born
- if (newChildren < (maxNumRabbitsPerGrid - rabbitBoard[j][k]))
- {
- numberRabbitsAlive += newChildren;
- numberRabbitsTotal += newChildren;
- rabbitBoard[j][k] += newChildren;
- }
- //if there is not enough space in the grid space for all the newChildren, then the number of children born will be
- //equal to the number of available spaces
- else if (newChildren > (maxNumRabbitsPerGrid - rabbitBoard[j][k]))
- {
- newChildren = maxNumRabbitsPerGrid - rabbitBoard[j][k];
- numberRabbitsAlive += newChildren;
- numberRabbitsTotal += newChildren;
- rabbitBoard[j][k] += newChildren;
- }
- //if the total number of rabbits has exceeded the available space in the rabbitList array then it returns false
- if (numberRabbitsTotal > rabbitListLength)
- {
- cerr << "ERROR: The rabbit list is full";
- return false;
- }
- else
- {
- for (i = 1; i <= newChildren; i++)
- {
- listOfRabbits[numberRabbitsTotal - i].alive = true;
- listOfRabbits[numberRabbitsTotal - i].age = 1;
- listOfRabbits[numberRabbitsTotal - i].yIndex = j;
- listOfRabbits[numberRabbitsTotal - i].xIndex = k;
- }
- }
- }
- }
- return true;
- }
- void FoxStep(int **foxBoard, Fox *listOfFoxes, int **rabbitBoard, Rabbit *listOfRabbits, int numberFoxesTotal,
- int& numberFoxesAlive, int numberRabbitsTotal, int& numberRabbitsAlive, int numRows, int numCols,
- int foxListLength, int rabbitListLength)
- {
- int j = 0;
- int k = 0;
- int i = 0;
- int liveFox = 0;
- int liveRabbit = 0;
- int nextFox = 1;
- int nextRabbit = 1;
- int direction = 0;
- for (j = 0; j < numRows; j++)
- {
- for (k = 0; k < numCols; k++)
- {
- nextFox = 0;
- nextRabbit = 0;
- for (i = 0; i < foxBoard[j][k]; i++)
- {
- liveFox = findNextLiveFox(listOfFoxes, j, k, numberFoxesTotal, nextFox);
- if (liveFox == -1)
- {
- break;
- }
- liveRabbit = FindNextLiveRabbit(listOfRabbits, j, k, numberRabbitsTotal, nextRabbit);
- if (liveRabbit == -1)
- {
- listOfFoxes[liveFox].fedLevel--;
- }
- else
- {
- listOfRabbits[liveRabbit].alive = false;
- listOfFoxes[liveFox].fedLevel++;
- rabbitBoard[j][k]--;
- numberRabbitsAlive--;
- }
- if (listOfFoxes[liveFox].fedLevel <= 0)
- {
- listOfFoxes[liveFox].alive = false;
- numberFoxesAlive--;
- foxBoard[j][k]--;
- }
- }
- }
- }
- nextFox = 0;
- for (int i = 0; i < numberFoxesTotal; i++)
- {
- j = listOfFoxes[i].yIndex;
- k = listOfFoxes[i].xIndex;
- if (listOfFoxes[i].alive == true)
- {
- //this determines the direction value (it will be an int between 0 and 7)
- direction = (rand() % 8);
- //if direction is 0 then it goes down
- if (direction == 0)
- {
- if (j == (numRows - 1))
- {
- listOfFoxes[i].yIndex = 0;
- foxBoard[j][k]--;
- foxBoard[0][k]++;
- }
- else
- {
- listOfFoxes[i].yIndex++;
- foxBoard[j][k]--;
- foxBoard[j+1][k]++;
- }
- }
- //if direction is 1 then it goes down-right
- else if (direction == 1)
- {
- if (j == (numRows - 1) && k == (numCols - 1))
- {
- listOfFoxes[i].yIndex = 0;
- listOfFoxes[i].xIndex = 0;
- foxBoard[j][k]--;
- foxBoard[0][0]++;
- }
- else if (k == (numCols - 1) && j != (numRows - 1))
- {
- listOfFoxes[i].yIndex++;
- listOfFoxes[i].xIndex = 0;
- foxBoard[j][k]--;
- foxBoard[j+1][0]++;
- }
- else if (k != (numCols - 1) && j == (numRows - 1))
- {
- listOfFoxes[i].yIndex = 0;
- listOfFoxes[i].xIndex++;
- foxBoard[j][k]--;
- foxBoard[0][k+1]++;
- }
- else
- {
- listOfFoxes[i].yIndex++;
- listOfFoxes[i].xIndex++;
- foxBoard[j][k]--;
- foxBoard[j+1][k+1]++;
- }
- }
- //if direction is 2 then it goes right
- else if (direction == 2)
- {
- if (k == (numCols - 1))
- {
- listOfFoxes[i].xIndex = 0;
- foxBoard[j][k]--;
- foxBoard[j][0]++;
- }
- else
- {
- listOfFoxes[i].xIndex++;
- foxBoard[j][k]--;
- foxBoard[j][k+1]++;
- }
- }
- //if direction is 3 then it goes up-right
- else if (direction == 3)
- {
- if (j == 0 && k == (numCols - 1))
- {
- listOfFoxes[i].yIndex = (numRows - 1);
- listOfFoxes[i].xIndex = 0;
- foxBoard[j][k]--;
- foxBoard[numRows-1][0]++;
- }
- else if (j == 0 && k != (numCols - 1))
- {
- listOfFoxes[i].yIndex = (numRows - 1);
- listOfFoxes[i].xIndex++;
- foxBoard[j][k]--;
- foxBoard[numRows-1][k+1]++;
- }
- else if (j != 0 && k == (numCols - 1))
- {
- listOfFoxes[i].yIndex--;
- listOfFoxes[i].xIndex = 0;
- foxBoard[j][k]--;
- foxBoard[j-1][0]++;
- }
- else
- {
- listOfFoxes[i].yIndex--;
- listOfFoxes[i].xIndex++;
- foxBoard[j][k]--;
- foxBoard[j-1][k+1]++;
- }
- }
- //if direction is 4 then it goes up
- else if (direction == 4)
- {
- if (j == 0)
- {
- listOfFoxes[i].yIndex = (numRows - 1);
- foxBoard[j][k]--;
- foxBoard[numRows-1][k]++;
- }
- else
- {
- listOfFoxes[i].yIndex--;
- foxBoard[j][k]--;
- foxBoard[j-1][k]++;
- }
- }
- //if direction is 5 then it goes up-left
- else if (direction == 5)
- {
- if (j == 0 && k == 0)
- {
- listOfFoxes[i].yIndex = (numRows - 1);
- listOfFoxes[i].xIndex = (numCols -1);
- foxBoard[j][k]--;
- foxBoard[numRows-1][numCols-1]++;
- }
- else if (j != 0 && k == 0)
- {
- listOfFoxes[i].yIndex--;
- listOfFoxes[i].xIndex = (numCols - 1);
- foxBoard[j][k]--;
- foxBoard[j-1][numCols-1]++;
- }
- else if (j == 0 && k != 0 )
- {
- listOfFoxes[i].yIndex = (numRows - 1);
- listOfFoxes[i].xIndex--;
- foxBoard[j][k]--;
- foxBoard[numRows-1][k-1]++;
- }
- else
- {
- listOfFoxes[i].yIndex--;
- listOfFoxes[i].xIndex--;
- foxBoard[j][k]--;
- foxBoard[j-1][k-1]++;
- }
- }
- //if direction is 6 then it goes left
- else if (direction == 6)
- {
- if (k == 0)
- {
- listOfFoxes[i].xIndex = (numCols - 1);
- foxBoard[j][k]--;
- foxBoard[j][numCols-1]++;
- }
- else
- {
- listOfFoxes[i].xIndex--;
- foxBoard[j][k]--;
- foxBoard[j][k-1]++;
- }
- }
- //if direction is 7 then it goes down-left
- else if (direction == 7)
- {
- if (k == 0 && j == (numRows - 1))
- {
- listOfFoxes[i].yIndex = 0;
- listOfFoxes[i].xIndex = (numCols - 1);
- foxBoard[j][k]--;
- foxBoard[0][numCols-1]++;
- }
- else if (k != 0 && j == (numRows - 1))
- {
- listOfFoxes[i].yIndex = 0;
- listOfFoxes[i].xIndex--;
- foxBoard[j][k]--;
- foxBoard[0][k-1]++;
- }
- else if (k == 0 && j != (numRows - 1))
- {
- listOfFoxes[i].yIndex++;
- listOfFoxes[i].xIndex = (numCols - 1);
- foxBoard[j][k]--;
- foxBoard[j+1][numCols-1]++;
- }
- else
- {
- listOfFoxes[i].yIndex++;
- listOfFoxes[i].xIndex--;
- foxBoard[j][k]--;
- foxBoard[j+1][k-1]++;
- }
- }
- }
- else
- {
- continue;
- }
- }
- }
- int findNextLiveFox( Fox *foxList, int Row, int Col, int numberFoxesTotal, int& lastFoxIndex)
- {
- for (int i = lastFoxIndex; i < numberFoxesTotal; i++)
- {
- if (foxList[i].yIndex == Row && foxList[i].xIndex == Col && foxList[i].alive == true)
- {
- lastFoxIndex = i + 1;
- return i;
- }
- else if (i == numberFoxesTotal-1)
- {
- lastFoxIndex = 0;
- return -1;
- }
- else
- {
- continue;
- }
- }
- }
- int FindNextLiveRabbit( Rabbit *rabbitList, int Row, int Col, int numberRabbitsTotal, int& nextRabbitIndex)
- {
- for (int i = nextRabbitIndex; i < numberRabbitsTotal; i++)
- {
- if (rabbitList[i].yIndex == Row && rabbitList[i].xIndex == Col && rabbitList[i].alive == true)
- {
- nextRabbitIndex = i + 1;
- return i;
- }
- else if (i == numberRabbitsTotal-1)
- {
- nextRabbitIndex = 0;
- return -1;
- }
- else
- {
- continue;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment