Suby

simulation_rules.cpp

May 25th, 2012
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.70 KB | None | 0 0
  1. #include "simulation_rules.h"
  2. #include "timer.h"
  3. #include "constants.h"
  4.  
  5. //ignore this and the printstatement stuff
  6. #include <fstream>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. void printstatement(Board aBoard);
  12. void printstatement2(Board aBoard);
  13.  
  14. int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject);
  15.  
  16. Simulation_Rules::Simulation_Rules()
  17. {
  18. }
  19.  
  20. void Simulation_Rules::SimulateTurn(Board &aBoardObject, Timer &Time)
  21. {
  22.     if (Time.is_paused() == false)
  23.     {
  24.         if (Time.get_ticks() >= 300)
  25.         {
  26.             SimulateRuleOne(aBoardObject); 
  27.             SimulateRuleTwo(aBoardObject); 
  28.             SimulateRuleThree(aBoardObject);   
  29.             SimulateRuleFour(aBoardObject);
  30.             aBoardObject.resetIsCellOccupied();
  31.             aBoardObject.setSwitcher();    
  32.             Time.start();
  33.         }
  34.     }
  35. }
  36.  
  37. void Simulation_Rules::SimulateRuleOne(Board &aBoardObject)
  38. {
  39.     //Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  40.     int counter = 0, NumberOfAdjacentLifeForms = 0;
  41.  
  42.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  43.     {
  44.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
  45.         aBoardObject.setSwitcher();
  46.            
  47.         if (NumberOfAdjacentLifeForms < 2 && aBoardObject.getIsCellOccupied(counter) == true)
  48.             aBoardObject.setIsCellOccupied(counter, false);
  49.        
  50.         aBoardObject.setSwitcher();
  51.         counter++; 
  52.     }  
  53.        
  54. }
  55.  
  56.  
  57. void Simulation_Rules::SimulateRuleTwo(Board &aBoardObject)
  58. {
  59.     //Any live cell with two or three live neighbours lives on to the next generation.
  60.     int counter = 0, NumberOfAdjacentLifeForms = 0;
  61.  
  62.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  63.     {
  64.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
  65.         aBoardObject.setSwitcher();
  66.            
  67.         if (NumberOfAdjacentLifeForms == 2 || NumberOfAdjacentLifeForms == 3 && aBoardObject.getIsCellOccupied(counter) == true)
  68.             aBoardObject.setIsCellOccupied(counter, true);
  69.            
  70.         aBoardObject.setSwitcher();
  71.        
  72.         counter++;
  73.     }
  74. }
  75.  
  76. void Simulation_Rules::SimulateRuleThree(Board &aBoardObject)
  77. {
  78.     //Any live cell with more than three live neighbours dies, as if by overcrowding.
  79.     int counter = 0, NumberOfAdjacentLifeForms = 0;
  80.  
  81.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  82.     {
  83.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
  84.         aBoardObject.setSwitcher();
  85.        
  86.         if (NumberOfAdjacentLifeForms >= 4 && aBoardObject.getIsCellOccupied(counter) == true)
  87.             aBoardObject.setIsCellOccupied(counter, false);
  88.            
  89.         aBoardObject.setSwitcher();
  90.         counter++;
  91.     }
  92. }
  93.  
  94. void Simulation_Rules::SimulateRuleFour(Board &aBoardObject)
  95. {
  96.     //Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
  97.     int counter = 0, NumberOfAdjacentLifeForms = 0;
  98.  
  99.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  100.     {
  101.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
  102.         aBoardObject.setSwitcher();
  103.  
  104.         if (NumberOfAdjacentLifeForms == 3 && aBoardObject.getIsCellOccupied(counter) == false)
  105.         {
  106.             aBoardObject.setIsCellOccupied(counter, true);
  107.             //aBoardObject.setColor(counter, aBoardObject.getColor(counter));  
  108.         }
  109.         aBoardObject.setSwitcher();
  110.         counter++;
  111.     }
  112. }
  113.  
  114.  
  115. int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject)
  116. {
  117.     int NumberOfAdjacentLifeForms = 0;
  118.     const int LastSquareOnxPlane = SCREEN_WIDTH - SQUARE_WIDTH;
  119.     const int LastSquareOnyPlane = SCREEN_HEIGHT - SQUARE_HEIGHT;
  120.  
  121.     //left and right
  122.     if (aBoardObject.getCell(WhichOne).x != 0 && aBoardObject.getIsCellOccupied(WhichOne - 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne - 1))
  123.         NumberOfAdjacentLifeForms++;
  124.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane && aBoardObject.getIsCellOccupied(WhichOne + 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne + 1))
  125.         NumberOfAdjacentLifeForms++;
  126.  
  127.     //up and down
  128.     if (aBoardObject.getCell(WhichOne).y != 0 && aBoardObject.getIsCellOccupied(WhichOne - NUMBER_OF_CELLS_HORIZONTAL) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne - NUMBER_OF_CELLS_HORIZONTAL))
  129.         NumberOfAdjacentLifeForms++;           
  130.     if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane && aBoardObject.getIsCellOccupied(WhichOne + NUMBER_OF_CELLS_HORIZONTAL) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne + NUMBER_OF_CELLS_HORIZONTAL))
  131.         NumberOfAdjacentLifeForms++;
  132.    
  133.     //bottom left and bottom right
  134.     if (aBoardObject.getCell(WhichOne).x != 0 && aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane && aBoardObject.getIsCellOccupied(WhichOne + NUMBER_OF_CELLS_HORIZONTAL - 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne + NUMBER_OF_CELLS_HORIZONTAL - 1))
  135.         NumberOfAdjacentLifeForms++;       
  136.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane && aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane && aBoardObject.getIsCellOccupied(WhichOne + NUMBER_OF_CELLS_HORIZONTAL + 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne + NUMBER_OF_CELLS_HORIZONTAL + 1))
  137.         NumberOfAdjacentLifeForms++;       
  138.                
  139.     //top left and top right
  140.     if (aBoardObject.getCell(WhichOne).x != 0 && aBoardObject.getCell(WhichOne).y != 0 && aBoardObject.getIsCellOccupied(WhichOne - NUMBER_OF_CELLS_HORIZONTAL - 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne - NUMBER_OF_CELLS_HORIZONTAL - 1))
  141.         NumberOfAdjacentLifeForms++;
  142.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane && aBoardObject.getCell(WhichOne).y != 0 && aBoardObject.getIsCellOccupied(WhichOne - NUMBER_OF_CELLS_HORIZONTAL + 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne - NUMBER_OF_CELLS_HORIZONTAL + 1))
  143.         NumberOfAdjacentLifeForms++;
  144.    
  145.     return NumberOfAdjacentLifeForms;
  146. }
  147.  
  148. void printstatement(Board aBoard)
  149. {
  150.     ofstream myfile;
  151.     myfile.open ("bugs.txt");
  152.     int counter = 0;
  153.     myfile << "before" << endl;
  154.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  155.     {
  156.         if (aBoard.getIsCellOccupied(counter) == 1)
  157.             myfile << "cOUNTER: " << counter << " x: " << aBoard.getCell(counter).x << "Y: " << aBoard.getCell(counter).y << " Occupied? " << aBoard.getIsCellOccupied(counter) << endl;
  158.         counter++;
  159.     }
  160.  
  161.     myfile.close();
  162. }
  163.  
  164. void Board::SwitcherValue(bool value)
  165. {
  166.     Switcher = value;
  167. }
  168.  
  169. void printstatement2(Board aBoard)
  170. {
  171.     ofstream myfile;
  172.     myfile.open ("bugs2.txt");
  173.     myfile << endl << endl << "after" << endl;
  174.     //myfile << "IN SWITCH 0";
  175.  
  176.     int counter = 0;
  177.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  178.     {
  179.         if (aBoard.getIsCellOccupied(counter) == 1)
  180.             myfile << "cOUNTER: " << counter << " x: " << aBoard.getCell(counter).x << "Y: " << aBoard.getCell(counter).y << " Occupied? " << aBoard.getIsCellOccupied(counter) << endl;
  181.         counter++;
  182.     }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.     myfile.close();
  190. }
Advertisement
Add Comment
Please, Sign In to add comment