Guest User

assignment 2

a guest
Dec 5th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. //Assignment 2
  8. //Created by Maggie Parkhurst-Bartel, Asal Nasiri, Faris Parkar and Jason
  9. //November 25th 2014
  10.  
  11. //This program models the evolution of a population of foxes and rabbits
  12.  
  13.  
  14. //structure of the foxes, contains position, level of hunger and living status
  15. struct Fox
  16. {
  17. int xIndex;
  18. int yIndex;
  19. int fedLevel;
  20. bool alive;
  21. };
  22.  
  23. //the structure of the rabbits, contains position, age, and living status
  24. struct Rabbit
  25. {
  26. int xIndex;
  27. int yIndex;
  28. int age;
  29. bool alive;
  30. };
  31.  
  32. #define MAX_FOXES_PER_GRID 9
  33. #define MAX_RABBITS_PER_GRID 20
  34.  
  35. void PrintGrids(int **FoxBoard, int **RabbitBoard, ostream& outStream, int numRows, int numCols);
  36.  
  37. bool RabbitStep(int **rabbitBoard, Rabbit *listOfRabbits, int& numberRabbitsTotal, int& numberRabbitsAlive,
  38. int numRows, int numCols, int rabbitListLength,int maxNumRabbitsPerGrid, int numChildren);
  39.  
  40. void FoxStep(int **foxBoard, Fox *listOfFoxes, int **rabbitBoard, Rabbit *listOfRabbits, int numberFoxesTotal,
  41. int& numberFoxesAlive, int numberRabbitsTotal, int& numberRabbitsAlive, int numRows, int numCols,
  42. int foxListLength, int rabbitListLength);
  43.  
  44. int findNextLiveFox( Fox *foxList, int Row, int Col, int numberFoxesTotal, int& lastFoxIndex );
  45.  
  46. int FindNextLiveRabbit( Rabbit *rabbitList, int Row, int Col, int numberRabbitsTotal, int& nextRabbitIndex );
  47.  
  48.  
  49.  
  50.  
  51. int main()
  52. {
  53. Fox *foxList = NULL;
  54. Rabbit *rabbitList = NULL;
  55. ofstream outfile;
  56. char outputName[80] = {'\0'};
  57. int numRows = 0;
  58. int numCols = 0;
  59. int numGen = 0;
  60. int seed = 0;
  61. int maxChildren = 0;
  62. double fracFox = 0.0;
  63. double fracRabbit = 0.0;
  64. int maxFoxes = 0;
  65. int maxRabbits = 0;
  66. int **foxGrid = NULL;
  67. int **rabbitGrid = NULL;
  68. int XX = 0;
  69. double increment = 0.0;
  70. double prob = 0.0;
  71. int count = 0; int count2= 0;
  72. int j = 0;
  73. int k = 0;
  74. int x = 0;
  75. int y = 0;
  76. double firstFract = 0.0;
  77. int rabbitsLive = 0;
  78. int rabbitsTotal = 0;
  79. int foxesLive = 0;
  80. int foxesTotal = 0;
  81. int rabbitListLength = 0;
  82. int foxListLength = 0;
  83. bool rabBool = 0;
  84.  
  85.  
  86.  
  87. //prompts for name of output file
  88. cout << "Enter the name of the output file: ";
  89. cin >> outputName;
  90.  
  91. outfile.open(outputName);
  92.  
  93. //if its fails opening, enters this loop
  94. while (outfile.fail())
  95. {
  96. //if it has failed to open six times
  97. if (count == 5)
  98. {
  99. cerr << "ERROR: could not open output file after 6 tries";
  100. return 1;
  101. }
  102.  
  103. //prompts you to enter an output name again and attempts to open it
  104. else
  105. {
  106. cerr << "ERROR: output file not opened correctly" << endl;
  107. cout << "Enter the name of the output file: ";
  108. cin >> outputName;
  109.  
  110. }
  111.  
  112. count++;
  113. }
  114.  
  115.  
  116. //prompts for the number of rows
  117. cout << "Enter the number of rows in the simulation grid: ";
  118.  
  119. count = 1;
  120.  
  121. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  122. while (count <= 7)
  123. {
  124. //if it has tried and failed to open 6 times
  125. if (count == 6)
  126. {
  127. cerr << "ERROR: could not read number of rows after 6 tries";
  128. return 2;
  129. }
  130.  
  131. //if the value entered is not an integer
  132. else if (!(cin >> numRows))
  133. {
  134. cerr << "ERROR: Cannot read number of rows" << endl;
  135. cerr << "Enter the number of rows in the simulation grid: ";
  136. cin >> numRows;
  137.  
  138. count++;
  139. continue;
  140. }
  141.  
  142. //if the value entered exceeds the bounds
  143. else if (numRows <= 0 || numRows >= 15)
  144. {
  145. cerr << "ERROR: Read an illegal number of rows for the board" << endl;
  146. cerr << "Enter the number of rows in the simulation grid: ";
  147. cin >> numRows;
  148.  
  149. count++;
  150. continue;
  151. }
  152.  
  153. //if the value entered for numRows is valid
  154. else
  155. {
  156. break;
  157. }
  158.  
  159. }
  160.  
  161. //prompts for the number of columns
  162. cout << "Enter the number of columns in the simulation grid: ";
  163.  
  164. count = 1;
  165.  
  166. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  167. while (count <= 7)
  168. {
  169. //if it has tried and failed to open 6 times
  170. if (count == 6)
  171. {
  172. cerr << "ERROR: could not read number of columns after 6 tries";
  173. return 3;
  174. }
  175.  
  176. //if the value entered was not an integer
  177. else if (!(cin >> numCols))
  178. {
  179. cerr << "ERROR: Cannot read number of columns" << endl;
  180. cerr << "Enter the number of columns in the simulation grid: ";
  181. cin >> numCols;
  182.  
  183. count++;
  184. continue;
  185. }
  186.  
  187. //if the value entered exceeds the bounds
  188. else if (numCols <= 0 || numCols >= 15)
  189. {
  190. cerr << "ERROR: Read an illegal number of columns for the board" << endl;
  191. cerr << "Enter the number of columns in the simulation grid: ";
  192. cin >> numCols;
  193.  
  194. count++;
  195. continue;
  196. }
  197.  
  198. //if the value entered for numCols is valid
  199. else
  200. {
  201. break;
  202. }
  203. }
  204.  
  205. //prompts for the number of generations
  206. cout << "Enter the number of generations: ";
  207.  
  208. count = 1;
  209.  
  210. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  211. while (count <= 7)
  212. {
  213. //if it has tried and failed to open 6 times
  214. if (count == 6)
  215. {
  216. cerr << "ERROR: could not read number of generations after 6 tries";
  217. return 4;
  218. }
  219.  
  220. //if the value entered was not an integer
  221. else if (!(cin >> numGen))
  222. {
  223. cerr << "ERROR: Cannot read number of generations" << endl;
  224. cerr << "Enter the number of generations in the simulation grid: ";
  225. cin >> numGen;
  226.  
  227. count++;
  228. continue;
  229. }
  230.  
  231. //if the value entered exceeds the bounds
  232. else if (numGen <= 0 || numGen >= 15)
  233. {
  234. cerr << "ERROR: Read an illegal number of generations for the board" << endl;
  235. cerr << "Enter the number of generations in the simulation grid: ";
  236. cin >> numGen;
  237.  
  238. count++;
  239. continue;
  240. }
  241.  
  242. //if the value entered for generations is valid
  243. else
  244. {
  245. break;
  246. }
  247. }
  248.  
  249. //prompts for the value of the the seed
  250. cout << "Enter the seed for the random number generator: ";
  251.  
  252. count = 1;
  253.  
  254. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  255. while (count <= 7)
  256. {
  257. //if it has tried and failed to open 6 times
  258. if (count == 6)
  259. {
  260. cerr << "ERROR: could not read number of generations after 6 tries";
  261. return 5;
  262. }
  263.  
  264. //if the value entered was not an integer
  265. else if (!(cin >> seed))
  266. {
  267. cerr << "ERROR: Cannot read the seed for the random number generator" << endl;
  268. cerr << "Enter the seed for the random number generator: ";
  269. cin >> seed;
  270.  
  271. count++;
  272. continue;
  273. }
  274.  
  275. //if the value entered exceeds the bounds
  276. else if (seed <= 0 || seed >= RAND_MAX)
  277. {
  278. cerr << "ERROR: Read an illegal seed" << endl;
  279. cerr << "Enter the seed for the random number generator: ";
  280. cin >> seed;
  281.  
  282. count++;
  283. continue;
  284. }
  285.  
  286. //if the value entered is valid
  287. else
  288. {
  289. break;
  290. }
  291. }
  292.  
  293. //prompts for the fraction value of squares containing foxes
  294. cout << "Enter the initial fraction of squares in the grid containing foxes: ";
  295.  
  296. count = 1;
  297.  
  298. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  299. while (count <= 7)
  300. {
  301. //if it tried and failed to open 6 times
  302. if (count == 6)
  303. {
  304. cerr << "ERROR: could not read the initial fraction of squares in the grid containing no foxes after 6 tries";
  305. return 5;
  306. }
  307.  
  308. //if the value entered was not a double or integer
  309. else if (!(cin >> fracFox))
  310. {
  311. cerr << "ERROR: Cannot read the initial fraction of squares in the grid containing foxes" << endl;
  312. cerr << "Enter the initial fraction of squares in the grid containing foxes: ";
  313. cin >> fracFox;
  314.  
  315. count++;
  316. continue;
  317. }
  318.  
  319. //if the value entered exceeds the bounds
  320. else if (fracFox <= 0.05 || fracFox >= 0.95)
  321. {
  322. cerr << "ERROR: Read an illegal initial fraction of squares in the grid with foxes" << endl;
  323. cerr << "Enter the initial fraction of squares in the grid containing foxes: ";
  324. cin >> fracFox;
  325.  
  326. count++;
  327. continue;
  328. }
  329.  
  330. //if the value entered was valid
  331. else
  332. {
  333. break;
  334. }
  335. }
  336.  
  337. //prompts for the fraction value of squares containing rabbits
  338. cout << "Enter the initial fraction of squares in the grid containing rabbits: ";
  339.  
  340. count = 1;
  341.  
  342. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  343. while (count <= 7)
  344. {
  345. //if it tried and failed to open 6 times
  346. if (count == 6)
  347. {
  348. cerr << "ERROR: could not read the initial fraction of squares in the grid containing no rabbits after 6 tries";
  349. return 6;
  350. }
  351.  
  352. //if the value entered was not a double or integer
  353. else if (!(cin >> fracRabbit))
  354. {
  355. cerr << "ERROR: Cannot read the initial fraction of squares in the grid containing rabbits" << endl;
  356. cerr << "Enter the initial fraction of squares in the grid containing rabbits: ";
  357. cin >> fracRabbit;
  358.  
  359. count++;
  360. continue;
  361. }
  362.  
  363. //if the value entered exceeds the bounds
  364. else if (fracRabbit <= 0.05 || fracRabbit >= 0.95)
  365. {
  366. cerr << "ERROR: Read an illegal initial fraction of squares in the grid containing foxes " << endl;
  367. cerr << "Enter the initial fraction of squares in the grid containing rabbits: ";
  368. cin >> fracRabbit;
  369.  
  370. count++;
  371. continue;
  372. }
  373.  
  374. //if the value entered was valid
  375. else
  376. {
  377. break;
  378. }
  379. }
  380.  
  381. //prompts for the initial maximum number of foxes per grid
  382. cout << "Enter the initial maximum number of foxes per grid: ";
  383.  
  384. count = 1;
  385.  
  386. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  387. while (count <= 7)
  388. {
  389. //if it tried and failed to open 6 times
  390. if (count == 6)
  391. {
  392. cerr << "ERROR: could not read the initial maximum number of foxes per square after 6 tries";
  393. return 7;
  394. }
  395.  
  396. //if the value entered was not an integer
  397. else if (!(cin >> maxFoxes))
  398. {
  399. cerr << "ERROR: Cannot read the initial maximum number of foxes per grid " << endl;
  400. cerr << "Enter the initial maximum number of foxes per grid: ";
  401. cin >> maxFoxes;
  402.  
  403. count++;
  404. continue;
  405. }
  406.  
  407. //if the value entered exceeds the bounds
  408. else if (maxFoxes <= 0 || maxFoxes > MAX_FOXES_PER_GRID)
  409. {
  410. cerr << "ERROR: Read an illegal initial maximum number of foxes per square" << endl;
  411. cerr << "Enter the initial maximum number of foxes per grid: ";
  412. cin >> maxFoxes;
  413.  
  414. count++;
  415. continue;
  416. }
  417.  
  418. //if the value entered was valid
  419. else
  420. {
  421. break;
  422. }
  423. }
  424.  
  425. //prompts for maximum number of rabbits per grid
  426. cout << "Enter the initial maximum number of rabbits per grid ";
  427.  
  428. count = 1;
  429.  
  430. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  431. while (count <= 7)
  432. {
  433. //if it has tried and failed to open 6 times
  434. if (count == 6)
  435. {
  436. cerr << "ERROR: could not read the initial maximum number of rabbits per grid after 6 tries";
  437. return 8;
  438. }
  439.  
  440. //if value entered was not an integer
  441. else if (!(cin >> maxRabbits))
  442. {
  443. cerr << "ERROR: Cannot read the initial maximum number of rabbits per grid" << endl;
  444. cerr << "Enter the initial maximum number of rabbits per grid ";
  445. cin >> maxRabbits;
  446.  
  447. count++;
  448. continue;
  449. }
  450.  
  451. //if value entered exceeds the bounds
  452. else if (maxRabbits <= 0 || maxRabbits > MAX_RABBITS_PER_GRID)
  453. {
  454. cerr << "ERROR: Read an illegal initial maximum number of rabbits per grid" << endl;
  455. cerr << "Enter the initial maximum number of rabbits per grid ";
  456. cin >> maxRabbits;
  457.  
  458. count++;
  459. continue;
  460. }
  461.  
  462. //if the value entered is valid
  463. else
  464. {
  465. break;
  466. }
  467. }
  468.  
  469. //prompts for the maximum number of children
  470. cout << "Enter the maximum number of children produced when rabbits breed ";
  471.  
  472. count = 1;
  473.  
  474. //loop that takes care of invalid values, values outside of bounds and terminates after 6 tries
  475. while (count <= 7)
  476. {
  477. //if it has tried and failed to open 6 times
  478. if (count == 6)
  479. {
  480. cerr << "ERROR: could not read the maximum number of children produced when rabbits breed after 6 tries";
  481. return 9;
  482. }
  483.  
  484. //if the value entered was not an integer
  485. else if (!(cin >> maxChildren))
  486. {
  487. cerr << "ERROR: Cannot read the maximum number of children produced when rabbits breed" << endl;
  488. cerr << "Enter the maximum number of children produced when rabbits breed ";
  489. cin >> maxChildren;
  490.  
  491. count++;
  492. continue;
  493. }
  494.  
  495. //if the value entered exceeds the bounds
  496. else if (maxChildren <= 0 || maxChildren > MAX_RABBITS_PER_GRID)
  497. {
  498. cerr << "ERROR: Read an illegal maximum number of children produced when rabbits breed" << endl;
  499. cerr << "Enter the maximum number of children produced when rabbits breed ";
  500. cin >> maxChildren;
  501.  
  502. count++;
  503. continue;
  504. }
  505.  
  506. //if the value entered is valid
  507. else
  508. {
  509. break;
  510. }
  511. }
  512.  
  513. cout << endl;
  514. outfile << endl;
  515.  
  516. /////////////////////////////////////////////////////////////////////////DONE INITIALIZATION///////////////////////////////////////////////////////////////
  517.  
  518. //creates a pointer array of numRows spaces
  519. foxGrid = new int* [numRows];
  520.  
  521.  
  522. //pointer array just created goes through a loop where each space in the array
  523. //points at a different array
  524. for (int i = 0; i < numRows; i++)
  525. {
  526. foxGrid[i] = new int [numCols];
  527. }
  528.  
  529.  
  530. //seeds the rand function
  531. srand(seed);
  532.  
  533. //this for loop goes through each space in the 2D array, generates a random number,
  534. //and turns that number into a value between 0 and 9 inclusive
  535. for (int i = 0; i < numRows; i++ )
  536. {
  537. for (j = 0; j < numCols; j++ )
  538. {
  539. foxGrid[i][j] = maxFoxes;
  540. k = rand();
  541. firstFract = 1-fracFox;
  542.  
  543. for (int nFox = 0; nFox <= maxFoxes; nFox++)
  544. {
  545. if ((double)k < firstFract * RAND_MAX)
  546. {
  547. foxGrid[i][j] = nFox;
  548. break;
  549. }
  550.  
  551. firstFract = (1-fracFox)+ (fracFox * (nFox+1))/maxFoxes;
  552. }
  553.  
  554. }
  555. }
  556.  
  557. //creates a 1D array of type Fox that keeps track of the Foxes and the number thereof
  558. foxList = new Fox [numRows*numCols*maxFoxes*2];
  559. foxListLength = numRows*numCols*maxFoxes*2;
  560.  
  561. //initialises each alive fox in the array foxlist
  562. y = 0;
  563. for (j = 0; j < numRows; j++)
  564. {
  565. for (k = 0; k < numCols; k++)
  566. {
  567. for (x = 0; x < foxGrid[j][k]; x++)
  568. {
  569. foxList[y].xIndex = k;
  570. foxList[y].yIndex = j;
  571. foxList[y].fedLevel = rand()%10;
  572. foxList[y].alive = true;
  573. y++;
  574. foxesLive++;
  575. foxesTotal++;
  576. }
  577. }
  578.  
  579. }
  580.  
  581. //creates a pointer array of size numRows
  582. rabbitGrid = new int* [numRows];
  583.  
  584. //the array created above goes through this loop and each space
  585. //in the array points to a different array
  586. for (int i = 0; i < numRows; i++)
  587. {
  588. rabbitGrid[i] = new int [numCols];
  589. }
  590.  
  591. //loop goes through each space in the 2D array rabbitGrid, generates a random number,
  592. //changes that number to a number between 0 and maxRabbits inclusive
  593. for (int i = 0; i < numRows; i++)
  594. {
  595. for (j = 0; j < numCols; j++)
  596. {
  597. rabbitGrid[i][j] = maxRabbits;
  598. k = rand();
  599. firstFract = 1-fracRabbit;
  600.  
  601. for (int nRabbit = 0; nRabbit <= maxRabbits; nRabbit++)
  602. {
  603. if ((double)k < firstFract * RAND_MAX)
  604. {
  605. rabbitGrid[i][j] = nRabbit;
  606. break;
  607. }
  608. firstFract = (1-fracRabbit)+ (fracRabbit * (nRabbit+1))/maxRabbits;
  609. }
  610.  
  611. }
  612. }
  613.  
  614.  
  615. //creates an array of type Rabbit
  616. rabbitList = new Rabbit[numRows*numCols*maxRabbits*20];
  617. rabbitListLength = numRows*numCols*maxRabbits*20;
  618.  
  619. y = 0;
  620. for (j = 0; j < numRows; j++)
  621. {
  622. for (k = 0; k < numCols; k++)
  623. {
  624. for (x = 0; x < rabbitGrid[j][k]; x++)
  625. {
  626. rabbitList[y].xIndex = k;
  627. rabbitList[y].yIndex = j;
  628. rabbitList[y].age = rand()%10;
  629. rabbitList[y].alive = true;
  630. y++;
  631. rabbitsLive++;
  632. rabbitsTotal++;
  633. }
  634. }
  635.  
  636. }
  637.  
  638. cout << "After initialization" << endl;
  639. outfile << "After initialization" << endl;
  640.  
  641. //prints the initial grid to screen and outfile
  642. PrintGrids(foxGrid, rabbitGrid, cout, numRows, numCols);
  643. PrintGrids(foxGrid, rabbitGrid, outfile, numRows, numCols);
  644.  
  645. //calls the functions foxstep and rabbitstep for each generation and prints it to the outfile
  646. for (int g = 0; g < numGen; g++)
  647. {
  648. rabBool = RabbitStep(rabbitGrid, rabbitList, rabbitsTotal, rabbitsLive, numRows, numCols, rabbitListLength, MAX_RABBITS_PER_GRID, maxChildren);
  649.  
  650. if (rabBool == false)
  651. {
  652. cerr << "ERROR: Rabbit function returned false, could not be completed";
  653. return 11;
  654. }
  655.  
  656. FoxStep(foxGrid, foxList, rabbitGrid, rabbitList, foxesTotal, foxesLive, rabbitsTotal, rabbitsLive, numRows, numCols, foxListLength, rabbitListLength);
  657.  
  658. //if it is at the last generation then it prints it to the screen and the outfile
  659. if (g == numGen - 1)
  660. {
  661. cout << "After foxes move and eat" << endl;
  662. outfile << "After foxes move and eat" << endl;
  663.  
  664. PrintGrids(foxGrid, rabbitGrid, cout, numRows, numCols);
  665. PrintGrids(foxGrid, rabbitGrid, outfile, numRows, numCols);
  666. }
  667.  
  668. else
  669. {
  670. outfile << "After foxes move and eat" << endl;
  671. PrintGrids(foxGrid, rabbitGrid, outfile, numRows, numCols);
  672. }
  673. }
  674.  
  675.  
  676. outfile.close();
  677. delete foxList;
  678. delete rabbitList;
  679. delete foxGrid;
  680. delete rabbitGrid;
  681.  
  682. return 0;
  683.  
  684. }
  685.  
  686. /////////////////////////////////////////////////////////////////////////////////////////////USER DEFINED FUNCTIONS//////////////////////////////////////////////////////////
  687.  
  688. //this function prints the current iteration of the board to an outstream (be it cout, outfile, etc)
  689. void PrintGrids(int **FoxBoard, int **RabbitBoard, ostream& outStream, int numRows, int numCols)
  690. {
  691. for (int j = 0; j < numRows; j++)
  692. {
  693. for (int k = 0; k < numCols; k++)
  694. {
  695. outStream << setw(3) << FoxBoard[j][k] << "," << setw(2) << RabbitBoard[j][k];
  696. }
  697.  
  698. outStream << endl;
  699. }
  700. outStream << endl << endl;
  701. }
  702.  
  703. //this function implements the deaths and births of the rabbits, will change the number of rabbits in each grid space
  704. bool RabbitStep(int **rabbitBoard, Rabbit *listOfRabbits, int& numberRabbitsTotal, int& numberRabbitsAlive,
  705. int numRows, int numCols, int rabbitListLength, int maxNumRabbitsPerGrid, int numChildren)
  706. {
  707. int j = 0;
  708. int k = 0;
  709. int i = 0;
  710. int live = 0;
  711. int next = 0;
  712. int newChildren = 0;
  713.  
  714. //points to y index
  715. for (j = 0; j < numRows; j++)
  716. {
  717. //points to x index
  718. for (k = 0; k < numCols; k++)
  719. {
  720. //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
  721. next = 0;
  722. //goes through each alive rabbit in the grid space
  723. for (i = 0; i < rabbitBoard[j][k]; i++)
  724. {
  725. live = FindNextLiveRabbit(listOfRabbits, j, k, numberRabbitsTotal, next);
  726.  
  727. //if a live rabbit was found, the value of its age increments
  728. if (live != -1)
  729. {
  730. listOfRabbits[live].age++;
  731.  
  732. //the rabbit may only live from ages 1 to 10 inclusive, if its age reaches 11, it dies
  733. if (listOfRabbits[live].age == 11)
  734. {
  735. listOfRabbits[live].alive = false;
  736. numberRabbitsAlive--;
  737. rabbitBoard[j][k]--;
  738. }
  739. }
  740.  
  741. //if it couldn't find a live rabbit then it breaks, so it can check the next grid space for a live rabbit
  742. else
  743. {
  744. break;
  745. }
  746. }
  747. }
  748. }
  749.  
  750. //points to y index
  751. for (j = 0; j < numRows; j++)
  752. {
  753. //points to x index
  754. for (k = 0; k < numCols; k++)
  755. {
  756. newChildren = 0; //resets the value of newChildren for each loop, newChildren is only relevant for its own grid space
  757.  
  758. //if space is full, then no children are born
  759. if (rabbitBoard[j][k] == maxNumRabbitsPerGrid || rabbitBoard[j][k] == 0 || rabbitBoard[j][k] == 1)
  760. {
  761. newChildren = 0;
  762. continue;
  763. }
  764. else
  765. {
  766. //loops for number of pairs
  767. for (i = 0; i < (int)(rabbitBoard[j][k]/2); i++)
  768. {
  769. newChildren = newChildren + ((rand() % numChildren) + 1);
  770. }
  771. }
  772. //if newChildren is less than the available space, then all the newChildren are able to be born
  773. if (newChildren < (maxNumRabbitsPerGrid - rabbitBoard[j][k]))
  774. {
  775. numberRabbitsAlive += newChildren;
  776. numberRabbitsTotal += newChildren;
  777. rabbitBoard[j][k] += newChildren;
  778. }
  779.  
  780. //if there is not enough space in the grid space for all the newChildren, then the number of children born will be
  781. //equal to the number of available spaces
  782. else if (newChildren > (maxNumRabbitsPerGrid - rabbitBoard[j][k]))
  783. {
  784. newChildren = maxNumRabbitsPerGrid - rabbitBoard[j][k];
  785. numberRabbitsAlive += newChildren;
  786. numberRabbitsTotal += newChildren;
  787. rabbitBoard[j][k] += newChildren;
  788. }
  789.  
  790. //if the total number of rabbits has exceeded the available space in the rabbitList array then it returns false
  791. if (numberRabbitsTotal > rabbitListLength)
  792. {
  793. cerr << "ERROR: The rabbit list is full";
  794. return false;
  795. }
  796.  
  797. else
  798. {
  799. for (i = 1; i <= newChildren; i++)
  800. {
  801. listOfRabbits[numberRabbitsTotal - i].alive = true;
  802. listOfRabbits[numberRabbitsTotal - i].age = 1;
  803. listOfRabbits[numberRabbitsTotal - i].yIndex = j;
  804. listOfRabbits[numberRabbitsTotal - i].xIndex = k;
  805. }
  806.  
  807. }
  808. }
  809. }
  810.  
  811. return true;
  812. }
  813.  
  814. void FoxStep(int **foxBoard, Fox *listOfFoxes, int **rabbitBoard, Rabbit *listOfRabbits, int numberFoxesTotal,
  815. int& numberFoxesAlive, int numberRabbitsTotal, int& numberRabbitsAlive, int numRows, int numCols,
  816. int foxListLength, int rabbitListLength)
  817. {
  818. int j = 0;
  819. int k = 0;
  820. int i = 0;
  821. int liveFox = 0;
  822. int liveRabbit = 0;
  823. int nextFox = 1;
  824. int nextRabbit = 1;
  825. int direction = 0;
  826.  
  827. for (j = 0; j < numRows; j++)
  828. {
  829. for (k = 0; k < numCols; k++)
  830. {
  831. nextFox = 0;
  832. nextRabbit = 0;
  833. for (i = 0; i < foxBoard[j][k]; i++)
  834. {
  835. liveFox = findNextLiveFox(listOfFoxes, j, k, numberFoxesTotal, nextFox);
  836.  
  837. if (liveFox == -1)
  838. {
  839. break;
  840. }
  841.  
  842. liveRabbit = FindNextLiveRabbit(listOfRabbits, j, k, numberRabbitsTotal, nextRabbit);
  843.  
  844. if (liveRabbit == -1)
  845. {
  846. listOfFoxes[liveFox].fedLevel--;
  847. }
  848.  
  849. else
  850. {
  851. listOfRabbits[liveRabbit].alive = false;
  852. listOfFoxes[liveFox].fedLevel++;
  853. rabbitBoard[j][k]--;
  854. numberRabbitsAlive--;
  855. }
  856.  
  857. if (listOfFoxes[liveFox].fedLevel <= 0)
  858. {
  859. listOfFoxes[liveFox].alive = false;
  860. numberFoxesAlive--;
  861. foxBoard[j][k]--;
  862. }
  863. }
  864. }
  865. }
  866.  
  867. nextFox = 0;
  868.  
  869.  
  870. for (int i = 0; i < numberFoxesTotal; i++)
  871. {
  872. j = listOfFoxes[i].yIndex;
  873. k = listOfFoxes[i].xIndex;
  874.  
  875. if (listOfFoxes[i].alive == true)
  876. {
  877. //this determines the direction value (it will be an int between 0 and 7)
  878. direction = (rand() % 8);
  879.  
  880. //if direction is 0 then it goes down
  881. if (direction == 0)
  882. {
  883. if (j == (numRows - 1))
  884. {
  885. listOfFoxes[i].yIndex = 0;
  886. foxBoard[j][k]--;
  887. foxBoard[0][k]++;
  888. }
  889.  
  890. else
  891. {
  892. listOfFoxes[i].yIndex++;
  893. foxBoard[j][k]--;
  894. foxBoard[j+1][k]++;
  895. }
  896. }
  897.  
  898. //if direction is 1 then it goes down-right
  899. else if (direction == 1)
  900. {
  901. if (j == (numRows - 1) && k == (numCols - 1))
  902. {
  903. listOfFoxes[i].yIndex = 0;
  904. listOfFoxes[i].xIndex = 0;
  905. foxBoard[j][k]--;
  906. foxBoard[0][0]++;
  907. }
  908.  
  909. else if (k == (numCols - 1) && j != (numRows - 1))
  910. {
  911. listOfFoxes[i].yIndex++;
  912. listOfFoxes[i].xIndex = 0;
  913. foxBoard[j][k]--;
  914. foxBoard[j+1][0]++;
  915. }
  916.  
  917. else if (k != (numCols - 1) && j == (numRows - 1))
  918. {
  919. listOfFoxes[i].yIndex = 0;
  920. listOfFoxes[i].xIndex++;
  921. foxBoard[j][k]--;
  922. foxBoard[0][k+1]++;
  923. }
  924.  
  925. else
  926. {
  927. listOfFoxes[i].yIndex++;
  928. listOfFoxes[i].xIndex++;
  929. foxBoard[j][k]--;
  930. foxBoard[j+1][k+1]++;
  931. }
  932. }
  933.  
  934. //if direction is 2 then it goes right
  935. else if (direction == 2)
  936. {
  937. if (k == (numCols - 1))
  938. {
  939. listOfFoxes[i].xIndex = 0;
  940. foxBoard[j][k]--;
  941. foxBoard[j][0]++;
  942. }
  943.  
  944. else
  945. {
  946. listOfFoxes[i].xIndex++;
  947. foxBoard[j][k]--;
  948. foxBoard[j][k+1]++;
  949. }
  950.  
  951. }
  952.  
  953. //if direction is 3 then it goes up-right
  954. else if (direction == 3)
  955. {
  956. if (j == 0 && k == (numCols - 1))
  957. {
  958. listOfFoxes[i].yIndex = (numRows - 1);
  959. listOfFoxes[i].xIndex = 0;
  960. foxBoard[j][k]--;
  961. foxBoard[numRows-1][0]++;
  962. }
  963.  
  964. else if (j == 0 && k != (numCols - 1))
  965. {
  966. listOfFoxes[i].yIndex = (numRows - 1);
  967. listOfFoxes[i].xIndex++;
  968. foxBoard[j][k]--;
  969. foxBoard[numRows-1][k+1]++;
  970. }
  971.  
  972. else if (j != 0 && k == (numCols - 1))
  973. {
  974. listOfFoxes[i].yIndex--;
  975. listOfFoxes[i].xIndex = 0;
  976. foxBoard[j][k]--;
  977. foxBoard[j-1][0]++;
  978. }
  979.  
  980. else
  981. {
  982. listOfFoxes[i].yIndex--;
  983. listOfFoxes[i].xIndex++;
  984. foxBoard[j][k]--;
  985. foxBoard[j-1][k+1]++;
  986. }
  987. }
  988.  
  989. //if direction is 4 then it goes up
  990. else if (direction == 4)
  991. {
  992. if (j == 0)
  993. {
  994. listOfFoxes[i].yIndex = (numRows - 1);
  995. foxBoard[j][k]--;
  996. foxBoard[numRows-1][k]++;
  997. }
  998.  
  999. else
  1000. {
  1001. listOfFoxes[i].yIndex--;
  1002. foxBoard[j][k]--;
  1003. foxBoard[j-1][k]++;
  1004. }
  1005. }
  1006.  
  1007. //if direction is 5 then it goes up-left
  1008. else if (direction == 5)
  1009. {
  1010. if (j == 0 && k == 0)
  1011. {
  1012. listOfFoxes[i].yIndex = (numRows - 1);
  1013. listOfFoxes[i].xIndex = (numCols -1);
  1014. foxBoard[j][k]--;
  1015. foxBoard[numRows-1][numCols-1]++;
  1016. }
  1017.  
  1018. else if (j != 0 && k == 0)
  1019. {
  1020. listOfFoxes[i].yIndex--;
  1021. listOfFoxes[i].xIndex = (numCols - 1);
  1022. foxBoard[j][k]--;
  1023. foxBoard[j-1][numCols-1]++;
  1024. }
  1025.  
  1026. else if (j == 0 && k != 0 )
  1027. {
  1028. listOfFoxes[i].yIndex = (numRows - 1);
  1029. listOfFoxes[i].xIndex--;
  1030. foxBoard[j][k]--;
  1031. foxBoard[numRows-1][k-1]++;
  1032. }
  1033.  
  1034. else
  1035. {
  1036. listOfFoxes[i].yIndex--;
  1037. listOfFoxes[i].xIndex--;
  1038. foxBoard[j][k]--;
  1039. foxBoard[j-1][k-1]++;
  1040. }
  1041. }
  1042.  
  1043. //if direction is 6 then it goes left
  1044. else if (direction == 6)
  1045. {
  1046. if (k == 0)
  1047. {
  1048. listOfFoxes[i].xIndex = (numCols - 1);
  1049. foxBoard[j][k]--;
  1050. foxBoard[j][numCols-1]++;
  1051. }
  1052.  
  1053. else
  1054. {
  1055. listOfFoxes[i].xIndex--;
  1056. foxBoard[j][k]--;
  1057. foxBoard[j][k-1]++;
  1058. }
  1059. }
  1060.  
  1061. //if direction is 7 then it goes down-left
  1062. else if (direction == 7)
  1063. {
  1064. if (k == 0 && j == (numRows - 1))
  1065. {
  1066. listOfFoxes[i].yIndex = 0;
  1067. listOfFoxes[i].xIndex = (numCols - 1);
  1068. foxBoard[j][k]--;
  1069. foxBoard[0][numCols-1]++;
  1070. }
  1071.  
  1072. else if (k != 0 && j == (numRows - 1))
  1073. {
  1074. listOfFoxes[i].yIndex = 0;
  1075. listOfFoxes[i].xIndex--;
  1076. foxBoard[j][k]--;
  1077. foxBoard[0][k-1]++;
  1078. }
  1079.  
  1080. else if (k == 0 && j != (numRows - 1))
  1081. {
  1082. listOfFoxes[i].yIndex++;
  1083. listOfFoxes[i].xIndex = (numCols - 1);
  1084. foxBoard[j][k]--;
  1085. foxBoard[j+1][numCols-1]++;
  1086. }
  1087.  
  1088. else
  1089. {
  1090. listOfFoxes[i].yIndex++;
  1091. listOfFoxes[i].xIndex--;
  1092. foxBoard[j][k]--;
  1093. foxBoard[j+1][k-1]++;
  1094. }
  1095. }
  1096.  
  1097. }
  1098.  
  1099. else
  1100. {
  1101. continue;
  1102. }
  1103. }
  1104.  
  1105. }
  1106.  
  1107. int findNextLiveFox( Fox *foxList, int Row, int Col, int numberFoxesTotal, int& lastFoxIndex)
  1108. {
  1109. for (int i = lastFoxIndex; i < numberFoxesTotal; i++)
  1110. {
  1111. if (foxList[i].yIndex == Row && foxList[i].xIndex == Col && foxList[i].alive == true)
  1112. {
  1113. lastFoxIndex = i + 1;
  1114. return i;
  1115. }
  1116.  
  1117. else if (i == numberFoxesTotal-1)
  1118. {
  1119. lastFoxIndex = 0;
  1120. return -1;
  1121. }
  1122.  
  1123. else
  1124. {
  1125. continue;
  1126. }
  1127. }
  1128. }
  1129.  
  1130. int FindNextLiveRabbit( Rabbit *rabbitList, int Row, int Col, int numberRabbitsTotal, int& nextRabbitIndex)
  1131. {
  1132. for (int i = nextRabbitIndex; i < numberRabbitsTotal; i++)
  1133. {
  1134. if (rabbitList[i].yIndex == Row && rabbitList[i].xIndex == Col && rabbitList[i].alive == true)
  1135. {
  1136. nextRabbitIndex = i + 1;
  1137. return i;
  1138. }
  1139.  
  1140. else if (i == numberRabbitsTotal-1)
  1141. {
  1142. nextRabbitIndex = 0;
  1143. return -1;
  1144. }
  1145.  
  1146. else
  1147. {
  1148. continue;
  1149. }
  1150. }
  1151. }
Advertisement
Add Comment
Please, Sign In to add comment