Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "simulation_rules.h"
- #include "timer.h"
- #include "constants.h"
- int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject, int &SurroundedByMoreRedOrGreen);
- Simulation_Rules::Simulation_Rules()
- {
- }
- void Simulation_Rules::SimulateTurn(Board &aBoardObject, Timer &Time, Menu &aMenuObject)
- {
- if (Time.is_paused() == false)
- {
- if (Time.get_ticks() >= Time.getInterval())
- {
- aMenuObject.AddOneToGenerationCount();
- SimulateRuleOne(aBoardObject);
- SimulateRuleTwo(aBoardObject);
- SimulateRuleThree(aBoardObject);
- SimulateRuleFour(aBoardObject);
- aBoardObject.resetIsCellOccupied();
- aBoardObject.setSwitcher();
- Time.start();
- }
- }
- }
- void Simulation_Rules::SimulateRuleOne(Board &aBoardObject)
- {
- //Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
- if (NumberOfAdjacentLifeForms < 2)
- if (aBoardObject.getIsCellOccupied(counter) == true)
- {
- aBoardObject.setSwitcher();
- aBoardObject.setIsCellOccupied(counter, false);
- aBoardObject.setColor(counter, NO_COLOR);
- aBoardObject.setSwitcher();
- }
- counter++;
- }
- }
- void Simulation_Rules::SimulateRuleTwo(Board &aBoardObject)
- {
- //Any live cell with two or three live neighbours lives on to the next generation.
- int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
- if (NumberOfAdjacentLifeForms == 2)
- if (aBoardObject.getIsCellOccupied(counter) == true)
- {
- aBoardObject.setSwitcher();
- aBoardObject.setIsCellOccupied(counter, true);
- aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
- aBoardObject.setSwitcher();
- }
- if (NumberOfAdjacentLifeForms == 3)
- if (aBoardObject.getIsCellOccupied(counter) == true)
- {
- aBoardObject.setSwitcher();
- aBoardObject.setIsCellOccupied(counter, true);
- aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
- aBoardObject.setSwitcher();
- }
- counter++;
- }
- }
- void Simulation_Rules::SimulateRuleThree(Board &aBoardObject)
- {
- //Any live cell with more than three live neighbours dies, as if by overcrowding.
- int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
- if (NumberOfAdjacentLifeForms >= 4)
- if (aBoardObject.getIsCellOccupied(counter) == true)
- {
- aBoardObject.setSwitcher();
- aBoardObject.setIsCellOccupied(counter, false);
- aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
- aBoardObject.setSwitcher();
- }
- counter++;
- }
- }
- void Simulation_Rules::SimulateRuleFour(Board &aBoardObject)
- {
- //Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
- int counter = 0, NumberOfAdjacentLifeForms = 0, SurroundedByMoreRedOrGreen = 0;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject, SurroundedByMoreRedOrGreen);
- if (NumberOfAdjacentLifeForms == 3)
- if (aBoardObject.getIsCellOccupied(counter) == false)
- {
- aBoardObject.setSwitcher();
- aBoardObject.setIsCellOccupied(counter, true);
- aBoardObject.setColor(counter, SurroundedByMoreRedOrGreen);
- aBoardObject.setSwitcher();
- }
- counter++;
- }
- }
- int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject, int &SurroundedByMoreRedOrGreen)
- {
- int NumberOfAdjacentLifeForms = 0;
- int CounterForGreenRed[2] = {0};
- const int LastSquareOnxPlane = SCREEN_WIDTH - SQUARE_WIDTH;
- const int LastSquareOnyPlane = SCREEN_HEIGHT - SQUARE_HEIGHT;
- const int CellToLeft = WhichOne - 1;
- const int CellToRight = WhichOne + 1;
- const int CellToTop = WhichOne - NUMBER_OF_CELLS_HORIZONTAL;
- const int CellToBottom = WhichOne + NUMBER_OF_CELLS_HORIZONTAL;
- const int CellToBottomLeft = WhichOne + NUMBER_OF_CELLS_HORIZONTAL - 1;
- const int CellToBottomRight = WhichOne + NUMBER_OF_CELLS_HORIZONTAL + 1;
- const int CellToTopLeft = WhichOne - NUMBER_OF_CELLS_HORIZONTAL - 1;
- const int CellToTopRight = WhichOne - NUMBER_OF_CELLS_HORIZONTAL + 1;
- //left
- if (aBoardObject.getCell(WhichOne).x != 0)
- if (aBoardObject.getIsCellOccupied(CellToLeft) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToLeft) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToLeft)]++;
- }
- //right
- if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
- if (aBoardObject.getIsCellOccupied(CellToRight) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToRight) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToRight)]++;
- }
- //top
- if (aBoardObject.getCell(WhichOne).y != 0)
- if (aBoardObject.getIsCellOccupied(CellToTop) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTop) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToTop)]++;
- }
- //down
- if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
- if (aBoardObject.getIsCellOccupied(CellToBottom) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottom) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToBottom)]++;
- }
- //bottom left
- if (aBoardObject.getCell(WhichOne).x != 0)
- if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
- if (aBoardObject.getIsCellOccupied(CellToBottomLeft) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomLeft) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToBottomLeft)]++;
- }
- //bottom right
- if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
- if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
- if (aBoardObject.getIsCellOccupied(CellToBottomRight) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomRight) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToBottomRight)]++;
- }
- //top left
- if (aBoardObject.getCell(WhichOne).x != 0)
- if (aBoardObject.getCell(WhichOne).y != 0)
- if (aBoardObject.getIsCellOccupied(CellToTopLeft) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopLeft) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToTopLeft)]++;
- }
- //top right
- if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
- if (aBoardObject.getCell(WhichOne).y != 0)
- if (aBoardObject.getIsCellOccupied(CellToTopRight) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopRight) || aBoardObject.getColor(WhichOne) == NO_COLOR)
- {
- NumberOfAdjacentLifeForms++;
- CounterForGreenRed[aBoardObject.getColor(CellToTopRight)]++;
- }
- if (CounterForGreenRed[GREEN] > CounterForGreenRed[RED])
- SurroundedByMoreRedOrGreen = GREEN;
- else if (CounterForGreenRed[GREEN] < CounterForGreenRed[RED])
- SurroundedByMoreRedOrGreen = RED;
- else if (CounterForGreenRed[GREEN] == CounterForGreenRed[RED])
- {
- SurroundedByMoreRedOrGreen = NO_COLOR;
- return 0;
- }
- return NumberOfAdjacentLifeForms;
- }
- /*
- void printstatement(Board aBoard)
- {
- ofstream myfile;
- myfile.open ("bugs.txt");
- int counter = 0;
- myfile << "before" << endl;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- if (aBoard.getIsCellOccupied(counter) == 1)
- myfile << "cOUNTER: " << counter << " x: " << aBoard.getCell(counter).x << "Y: " << aBoard.getCell(counter).y << " Occupied? " << aBoard.getIsCellOccupied(counter) << endl;
- counter++;
- }
- myfile.close();
- }
- void Board::SwitcherValue(bool value)
- {
- Switcher = value;
- }
- void printstatement2(Board aBoardObject, int adjacent, int WhichOne)
- {
- ofstream myfile;
- myfile.open ("bugs2.txt");
- int counter = 81;
- SDL_Rect OUTTHIS;
- OUTTHIS = aBoardObject.getCell(81);
- myfile << "counter: " << counter << " " << OUTTHIS.x << endl;
- counter = 0;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << endl << endl << " Numer of adjacent shits " << adjacent << endl;
- counter = 1;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << endl << endl << " Numer of adjacent shits " << adjacent << endl;
- counter = 2;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << endl << endl << " Numer of adjacent shits " << adjacent << endl;
- //myfile << endl << endl << " Numer of adjacent shits " << adjacent << endl;
- counter = 80;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << endl << endl << " Numer of adjacent shits " << adjacent << endl;
- //myfile << endl << endl << " Numer of adjacent shits " << adjacent << endl;
- counter = 80 + 80;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- //myfile << endl << endl << " Numer of adjacent shits " << adjacent << endl;
- counter += 1;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- counter = 81;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << endl << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- counter = 80;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- counter = 82;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- counter = 81 - 80;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- //myfile << "IN SWITCH 0";
- counter = 81 + 80;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- counter = 81 + 80 + 1;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- counter = 81 + 80 - 1;
- OUTTHIS = aBoardObject.getCell(counter);
- myfile << "counter: " << counter << " " << OUTTHIS.x << " " << OUTTHIS.y << " IS CELL OCC: " << aBoardObject.getIsCellOccupied(counter) << endl;
- //myfile << "IN SWITCH 0";
- myfile << endl << endl;
- counter = 81;
- int NumberOfAdjacentLifeForms = 0;
- const int LastSquareOnxPlane = SCREEN_WIDTH - SQUARE_WIDTH;
- const int LastSquareOnyPlane = SCREEN_HEIGHT - SQUARE_HEIGHT;
- const int CellToLeft = WhichOne - 1;
- const int CellToRight = WhichOne + 1;
- const int CellToTop = WhichOne - NUMBER_OF_CELLS_HORIZONTAL;
- const int CellToBottom = WhichOne + NUMBER_OF_CELLS_HORIZONTAL;
- const int CellToBottomLeft = WhichOne + NUMBER_OF_CELLS_HORIZONTAL - 1;
- const int CellToBottomRight = WhichOne + NUMBER_OF_CELLS_HORIZONTAL + 1;
- const int CellToTopLeft = WhichOne - NUMBER_OF_CELLS_HORIZONTAL - 1;
- const int CellToTopRight = WhichOne - NUMBER_OF_CELLS_HORIZONTAL + 1;
- //left
- if (aBoardObject.getCell(WhichOne).x != 0)
- if (aBoardObject.getIsCellOccupied(CellToLeft) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToLeft))
- myfile << "Left++" << endl;
- //right
- if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
- if (aBoardObject.getIsCellOccupied(CellToRight) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToRight))
- myfile << "Right++" << endl;
- //top
- if (aBoardObject.getCell(WhichOne).y != 0)
- if (aBoardObject.getIsCellOccupied(CellToTop) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTop))
- myfile << "Top++" << endl;
- //down
- if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
- if (aBoardObject.getIsCellOccupied(CellToBottom) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottom))
- myfile << "Bottom++" << endl;
- //bottom left
- if (aBoardObject.getCell(WhichOne).x != 0)
- if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
- if (aBoardObject.getIsCellOccupied(CellToBottomLeft) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomLeft))
- myfile << "BottomLeft++" << endl;
- //bottom right
- if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
- if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane)
- if (aBoardObject.getIsCellOccupied(CellToBottomRight) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToBottomRight))
- myfile << "BottomRight++" << endl;
- //top left
- if (aBoardObject.getCell(WhichOne).x != 0)
- if (aBoardObject.getCell(WhichOne).y != 0)
- if (aBoardObject.getIsCellOccupied(CellToTopLeft) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopLeft))
- myfile << "TopLeft++" << endl;
- //top right
- if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane)
- if (aBoardObject.getCell(WhichOne).y != 0)
- if (aBoardObject.getIsCellOccupied(CellToTopRight) == true)
- if (aBoardObject.getColor(WhichOne) == aBoardObject.getColor(CellToTopRight))
- myfile << "TopRight++" << endl;
- myfile.close();
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement