Advertisement
Suby

simulation_rules.cpp

Jun 1st, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.37 KB | None | 0 0
  1. #include "simulation_rules.h"
  2. #include "timer.h"
  3. #include "constants.h"
  4.  
  5. int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject, int &SurroundedByMoreRedOrGreen);
  6.  
  7. Simulation_Rules::Simulation_Rules()
  8. {
  9. }
  10.  
  11. void Simulation_Rules::SimulateTurn(Board &aBoardObject, Timer &Time, Menu &aMenuObject)
  12. {
  13.     if (Time.is_paused() == false)
  14.     {
  15.         if (Time.get_ticks() >= Time.getInterval())
  16.         {
  17.             aMenuObject.AddOneToGenerationCount();
  18.             SimulateRuleOne(aBoardObject); 
  19.             SimulateRuleTwo(aBoardObject); 
  20.             SimulateRuleThree(aBoardObject);   
  21.             SimulateRuleFour(aBoardObject);
  22.             aBoardObject.resetIsCellOccupied();
  23.             aBoardObject.setSwitcher();    
  24.             Time.start();
  25.         }
  26.     }
  27. }
  28.  
  29. void Simulation_Rules::SimulateRuleOne(Board &aBoardObject)
  30. {
  31.     //Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  32.     int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
  33.  
  34.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  35.     {
  36.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
  37.  
  38.         if (NumberOfAdjacentLifeForms < 2)
  39.             if (aBoardObject.getIsCellOccupied(counter) == true)
  40.             {
  41.                 aBoardObject.setSwitcher();
  42.                 aBoardObject.setIsCellOccupied(counter, false);
  43.                 aBoardObject.setColor(counter, NO_COLOR);
  44.                 aBoardObject.setSwitcher();
  45.             }
  46.  
  47.         counter++; 
  48.     }  
  49.        
  50. }
  51.  
  52.  
  53. void Simulation_Rules::SimulateRuleTwo(Board &aBoardObject)
  54. {
  55.     //Any live cell with two or three live neighbours lives on to the next generation.
  56.     int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
  57.  
  58.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  59.     {
  60.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
  61.            
  62.         if (NumberOfAdjacentLifeForms == 2)
  63.             if (aBoardObject.getIsCellOccupied(counter) == true)
  64.             {
  65.                 aBoardObject.setSwitcher();
  66.                 aBoardObject.setIsCellOccupied(counter, true);
  67.                 aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
  68.                 aBoardObject.setSwitcher();
  69.             }
  70.        
  71.         if (NumberOfAdjacentLifeForms == 3)
  72.             if (aBoardObject.getIsCellOccupied(counter) == true)
  73.             {
  74.                 aBoardObject.setSwitcher();
  75.                 aBoardObject.setIsCellOccupied(counter, true);
  76.                 aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
  77.                 aBoardObject.setSwitcher();
  78.             }
  79.  
  80.         counter++;
  81.     }
  82. }
  83.  
  84. void Simulation_Rules::SimulateRuleThree(Board &aBoardObject)
  85. {
  86.     //Any live cell with more than three live neighbours dies, as if by overcrowding.
  87.     int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
  88.  
  89.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  90.     {
  91.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
  92.        
  93.         if (NumberOfAdjacentLifeForms >= 4)
  94.             if (aBoardObject.getIsCellOccupied(counter) == true)
  95.             {
  96.                 aBoardObject.setSwitcher();
  97.                 aBoardObject.setIsCellOccupied(counter, false);
  98.                 aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
  99.                 aBoardObject.setSwitcher();
  100.             }
  101.  
  102.         counter++;
  103.     }
  104. }
  105.  
  106. void Simulation_Rules::SimulateRuleFour(Board &aBoardObject)
  107. {
  108.     //Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
  109.     int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
  110.  
  111.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  112.     {
  113.         NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
  114.  
  115.         if (NumberOfAdjacentLifeForms == 3)
  116.             if (aBoardObject.getIsCellOccupied(counter) == false)
  117.             {
  118.                 aBoardObject.setSwitcher();
  119.                 aBoardObject.setIsCellOccupied(counter, true);
  120.                 aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
  121.                 aBoardObject.setSwitcher();
  122.             }  
  123.  
  124.         counter++;
  125.     }
  126. }
  127.  
  128.  
  129. int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject, int &SurroundedByMoreRedOrGreen)
  130. {
  131.     int NumberOfAdjacentLifeForms = 0;
  132.     int CounterForGreenRed[2] = {0};
  133.  
  134.     const int LastSquareOnxPlane = SCREEN_WIDTH - SQUARE_WIDTH;
  135.     const int LastSquareOnyPlane = SCREEN_HEIGHT - SQUARE_HEIGHT;
  136.  
  137.     const int CellToLeft = WhichOne - 1;
  138.     const int CellToRight = WhichOne + 1;
  139.    
  140.     const int CellToTop = WhichOne - NUMBER_OF_CELLS_HORIZONTAL;
  141.     const int CellToBottom = WhichOne + NUMBER_OF_CELLS_HORIZONTAL;
  142.    
  143.     const int CellToBottomLeft = WhichOne + NUMBER_OF_CELLS_HORIZONTAL - 1;
  144.     const int CellToBottomRight = WhichOne + NUMBER_OF_CELLS_HORIZONTAL + 1;
  145.  
  146.     const int CellToTopLeft = WhichOne - NUMBER_OF_CELLS_HORIZONTAL - 1;
  147.     const int CellToTopRight = WhichOne - NUMBER_OF_CELLS_HORIZONTAL + 1;
  148.    
  149.  
  150.     //left
  151.     if (aBoardObject.getCell(WhichOne).x != 0)
  152.         if (aBoardObject.getIsCellOccupied(CellToLeft) == true)
  153.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToLeft) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  154.             {
  155.                 NumberOfAdjacentLifeForms++;
  156.                 CounterForGreenRed[aBoardObject.getColor(CellToLeft)]++;
  157.             }
  158.  
  159.     //right
  160.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
  161.         if (aBoardObject.getIsCellOccupied(CellToRight) == true)
  162.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToRight) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  163.             {
  164.                 NumberOfAdjacentLifeForms++;
  165.                 CounterForGreenRed[aBoardObject.getColor(CellToRight)]++;
  166.             }
  167.  
  168.     //top
  169.     if (aBoardObject.getCell(WhichOne).y != 0)
  170.         if (aBoardObject.getIsCellOccupied(CellToTop) == true)
  171.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTop) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  172.             {
  173.                 NumberOfAdjacentLifeForms++;   
  174.                 CounterForGreenRed[aBoardObject.getColor(CellToTop)]++;
  175.             }
  176.  
  177.     //down
  178.     if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
  179.         if (aBoardObject.getIsCellOccupied(CellToBottom) == true)
  180.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottom) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  181.             {
  182.                 NumberOfAdjacentLifeForms++;
  183.                 CounterForGreenRed[aBoardObject.getColor(CellToBottom)]++;
  184.             }
  185.    
  186.     //bottom left
  187.     if (aBoardObject.getCell(WhichOne).x != 0)
  188.         if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
  189.             if (aBoardObject.getIsCellOccupied(CellToBottomLeft) == true)
  190.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomLeft) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  191.                 {
  192.                     NumberOfAdjacentLifeForms++;   
  193.                     CounterForGreenRed[aBoardObject.getColor(CellToBottomLeft)]++;
  194.                 }
  195.  
  196.     //bottom right
  197.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
  198.         if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
  199.             if (aBoardObject.getIsCellOccupied(CellToBottomRight) == true)
  200.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomRight) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  201.                 {
  202.                     NumberOfAdjacentLifeForms++;       
  203.                     CounterForGreenRed[aBoardObject.getColor(CellToBottomRight)]++;
  204.                 }
  205.                
  206.     //top left
  207.     if (aBoardObject.getCell(WhichOne).x != 0)
  208.         if (aBoardObject.getCell(WhichOne).y != 0)
  209.             if (aBoardObject.getIsCellOccupied(CellToTopLeft) == true)
  210.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopLeft) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  211.                 {
  212.                     NumberOfAdjacentLifeForms++;
  213.                     CounterForGreenRed[aBoardObject.getColor(CellToTopLeft)]++;
  214.                 }
  215.  
  216.     //top right
  217.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
  218.         if (aBoardObject.getCell(WhichOne).y != 0)
  219.             if (aBoardObject.getIsCellOccupied(CellToTopRight) == true)
  220.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopRight) || aBoardObject.getColor(WhichOne) == NO_COLOR)
  221.                 {
  222.                     NumberOfAdjacentLifeForms++;
  223.                     CounterForGreenRed[aBoardObject.getColor(CellToTopRight)]++;
  224.                 }
  225.  
  226.    
  227.     if (CounterForGreenRed[GREEN] > CounterForGreenRed[RED])
  228.         SurroundedByMoreRedOrGreen = GREEN;
  229.     else if (CounterForGreenRed[GREEN] < CounterForGreenRed[RED])
  230.         SurroundedByMoreRedOrGreen = RED;
  231.     else if (CounterForGreenRed[GREEN] == CounterForGreenRed[RED])
  232.     {
  233.         SurroundedByMoreRedOrGreen = NO_COLOR;
  234.         return 0;
  235.     }
  236.  
  237.     return NumberOfAdjacentLifeForms;
  238. }
  239. /*
  240. void printstatement(Board aBoard)
  241. {
  242.     ofstream myfile;
  243.     myfile.open ("bugs.txt");
  244.     int counter = 0;
  245.     myfile << "before" << endl;
  246.     while (counter < NUMBER_OF_CELLS_ON_GRID)
  247.     {
  248.         if (aBoard.getIsCellOccupied(counter) == 1)
  249.             myfile << "cOUNTER: " << counter << " x: " << aBoard.getCell(counter).x << "Y: " << aBoard.getCell(counter).y << " Occupied? " << aBoard.getIsCellOccupied(counter) << endl;
  250.         counter++;
  251.     }
  252.  
  253.     myfile.close();
  254. }
  255.  
  256. void Board::SwitcherValue(bool value)
  257. {
  258.     Switcher = value;
  259. }
  260.  
  261. void printstatement2(Board aBoardObject, int adjacent, int WhichOne)
  262. {
  263.  
  264.     ofstream myfile;
  265.     myfile.open ("bugs2.txt");
  266.     int counter = 81;
  267.     SDL_Rect OUTTHIS;
  268.         OUTTHIS = aBoardObject.getCell(81);
  269.         myfile << "counter: " << counter << " " << OUTTHIS.x << endl;
  270.        
  271.         counter = 0;
  272.                 OUTTHIS = aBoardObject.getCell(counter);
  273.                 myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  274.  
  275.         //myfile << endl << endl << " Numer of adjacent shits " << adjacent  << endl;
  276.                 counter = 1;
  277.                 OUTTHIS = aBoardObject.getCell(counter);
  278. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  279.         //myfile << endl << endl << " Numer of adjacent shits " << adjacent  << endl;
  280.                         counter = 2;
  281.                 OUTTHIS = aBoardObject.getCell(counter);
  282. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  283.         //myfile << endl << endl << " Numer of adjacent shits " << adjacent  << endl;
  284.             //myfile << endl << endl << " Numer of adjacent shits " << adjacent  << endl;
  285.                         counter = 80;
  286.                 OUTTHIS = aBoardObject.getCell(counter);
  287.         myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  288.         //myfile << endl << endl << " Numer of adjacent shits " << adjacent  << endl;
  289.                     //myfile << endl << endl << " Numer of adjacent shits " << adjacent  << endl;
  290.                         counter = 80 + 80;
  291.                 OUTTHIS = aBoardObject.getCell(counter);
  292. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  293.     //myfile << "IN SWITCH 0";
  294.                     //myfile << endl << endl << " Numer of adjacent shits " << adjacent  << endl;
  295.                         counter += 1;
  296.                 OUTTHIS = aBoardObject.getCell(counter);
  297. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  298.     //myfile << "IN SWITCH 0";
  299.  
  300.  
  301.  
  302.  
  303.                         counter = 81;
  304.                 OUTTHIS = aBoardObject.getCell(counter);
  305. myfile << endl << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  306.     //myfile << "IN SWITCH 0";
  307.  
  308.                         counter = 80;
  309.                 OUTTHIS = aBoardObject.getCell(counter);
  310. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  311.     //myfile << "IN SWITCH 0";
  312.  
  313.                         counter = 82;
  314.                 OUTTHIS = aBoardObject.getCell(counter);
  315. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  316.     //myfile << "IN SWITCH 0";
  317.  
  318.                         counter = 81 - 80;
  319.                 OUTTHIS = aBoardObject.getCell(counter);
  320. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  321.     //myfile << "IN SWITCH 0";
  322.     //myfile << "IN SWITCH 0";
  323.  
  324.                         counter = 81 + 80;
  325.                 OUTTHIS = aBoardObject.getCell(counter);
  326. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  327.     //myfile << "IN SWITCH 0";
  328.  
  329.  
  330.                         counter = 81 + 80 + 1;
  331.                 OUTTHIS = aBoardObject.getCell(counter);
  332. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  333.     //myfile << "IN SWITCH 0";
  334.  
  335.  
  336.                         counter = 81 + 80 - 1;
  337.                 OUTTHIS = aBoardObject.getCell(counter);
  338. myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y  << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
  339.     //myfile << "IN SWITCH 0";
  340.  
  341. myfile << endl << endl;
  342. counter = 81;
  343.  
  344.  
  345.     int NumberOfAdjacentLifeForms = 0;
  346.  
  347.     const int LastSquareOnxPlane = SCREEN_WIDTH - SQUARE_WIDTH;
  348.     const int LastSquareOnyPlane = SCREEN_HEIGHT - SQUARE_HEIGHT;
  349.  
  350.     const int CellToLeft = WhichOne - 1;
  351.     const int CellToRight = WhichOne + 1;
  352.    
  353.     const int CellToTop = WhichOne - NUMBER_OF_CELLS_HORIZONTAL;
  354.     const int CellToBottom = WhichOne + NUMBER_OF_CELLS_HORIZONTAL;
  355.    
  356.     const int CellToBottomLeft = WhichOne + NUMBER_OF_CELLS_HORIZONTAL - 1;
  357.     const int CellToBottomRight = WhichOne + NUMBER_OF_CELLS_HORIZONTAL + 1;
  358.  
  359.     const int CellToTopLeft = WhichOne - NUMBER_OF_CELLS_HORIZONTAL - 1;
  360.     const int CellToTopRight = WhichOne - NUMBER_OF_CELLS_HORIZONTAL + 1;
  361.  
  362. //left
  363.     if (aBoardObject.getCell(WhichOne).x != 0)
  364.         if (aBoardObject.getIsCellOccupied(CellToLeft) == true)
  365.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToLeft))
  366.                 myfile << "Left++" << endl;
  367.  
  368.     //right
  369.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
  370.         if (aBoardObject.getIsCellOccupied(CellToRight) == true)
  371.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToRight))
  372.                 myfile << "Right++" << endl;
  373.  
  374.  
  375.  
  376.     //top
  377.     if (aBoardObject.getCell(WhichOne).y != 0)
  378.         if (aBoardObject.getIsCellOccupied(CellToTop) == true)
  379.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTop))
  380.                 myfile << "Top++" << endl;
  381.  
  382.     //down
  383.     if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
  384.         if (aBoardObject.getIsCellOccupied(CellToBottom) == true)
  385.             if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottom))
  386.                 myfile << "Bottom++" << endl;
  387.    
  388.  
  389.  
  390.     //bottom left
  391.     if (aBoardObject.getCell(WhichOne).x != 0)
  392.         if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
  393.             if (aBoardObject.getIsCellOccupied(CellToBottomLeft) == true)
  394.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomLeft))
  395.                     myfile << "BottomLeft++" << endl;  
  396.  
  397.     //bottom right
  398.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
  399.         if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
  400.             if (aBoardObject.getIsCellOccupied(CellToBottomRight) == true)
  401.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomRight))
  402.                     myfile << "BottomRight++" << endl;     
  403.                
  404.  
  405.  
  406.     //top left
  407.     if (aBoardObject.getCell(WhichOne).x != 0)
  408.         if (aBoardObject.getCell(WhichOne).y != 0)
  409.             if (aBoardObject.getIsCellOccupied(CellToTopLeft) == true)
  410.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopLeft))
  411.                     myfile << "TopLeft++" << endl;
  412.  
  413.  
  414.     //top right
  415.     if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
  416.         if (aBoardObject.getCell(WhichOne).y != 0)
  417.             if (aBoardObject.getIsCellOccupied(CellToTopRight) == true)
  418.                 if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopRight))
  419.                     myfile << "TopRight++" << endl;
  420.    
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.     myfile.close();
  434.    
  435. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement