Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "simulation_rules.h"
- #include "timer.h"
- #include "constants.h"
- //ignore this and the printstatement stuff
- #include <fstream>
- #include <iostream>
- using namespace std;
- void printstatement(Board aBoard);
- void printstatement2(Board aBoard);
- int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject);
- Simulation_Rules::Simulation_Rules()
- {
- }
- void Simulation_Rules::SimulateTurn(Board &aBoardObject, Timer &Time)
- {
- if (Time.is_paused() == false)
- {
- if (Time.get_ticks() >= 300)
- {
- 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;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
- aBoardObject.setSwitcher();
- if (NumberOfAdjacentLifeForms < 2 && aBoardObject.getIsCellOccupied(counter) == true)
- aBoardObject.setIsCellOccupied(counter, false);
- 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;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
- aBoardObject.setSwitcher();
- if (NumberOfAdjacentLifeForms == 2 || NumberOfAdjacentLifeForms == 3 && aBoardObject.getIsCellOccupied(counter) == true)
- aBoardObject.setIsCellOccupied(counter, true);
- 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;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
- aBoardObject.setSwitcher();
- if (NumberOfAdjacentLifeForms >= 4 && aBoardObject.getIsCellOccupied(counter) == true)
- aBoardObject.setIsCellOccupied(counter, false);
- 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;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- NumberOfAdjacentLifeForms = DetermineNumberOfNeighbors(counter, aBoardObject);
- aBoardObject.setSwitcher();
- if (NumberOfAdjacentLifeForms == 3 && aBoardObject.getIsCellOccupied(counter) == false)
- {
- aBoardObject.setIsCellOccupied(counter, true);
- //aBoardObject.setColor(counter, aBoardObject.getColor(counter));
- }
- aBoardObject.setSwitcher();
- counter++;
- }
- }
- int DetermineNumberOfNeighbors(int WhichOne, Board &aBoardObject)
- {
- int NumberOfAdjacentLifeForms = 0;
- const int LastSquareOnxPlane = SCREEN_WIDTH - SQUARE_WIDTH;
- const int LastSquareOnyPlane = SCREEN_HEIGHT - SQUARE_HEIGHT;
- //left and right
- if (aBoardObject.getCell(WhichOne).x != 0 && aBoardObject.getIsCellOccupied(WhichOne - 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne - 1))
- NumberOfAdjacentLifeForms++;
- if (aBoardObject.getCell(WhichOne).x != LastSquareOnxPlane && aBoardObject.getIsCellOccupied(WhichOne + 1) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne + 1))
- NumberOfAdjacentLifeForms++;
- //up and down
- if (aBoardObject.getCell(WhichOne).y != 0 && aBoardObject.getIsCellOccupied(WhichOne - NUMBER_OF_CELLS_HORIZONTAL) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne - NUMBER_OF_CELLS_HORIZONTAL))
- NumberOfAdjacentLifeForms++;
- if (aBoardObject.getCell(WhichOne).y != LastSquareOnyPlane && aBoardObject.getIsCellOccupied(WhichOne + NUMBER_OF_CELLS_HORIZONTAL) == true && aBoardObject.getColor(WhichOne) == aBoardObject.getColor(WhichOne + NUMBER_OF_CELLS_HORIZONTAL))
- NumberOfAdjacentLifeForms++;
- //bottom left and bottom right
- 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))
- NumberOfAdjacentLifeForms++;
- 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))
- NumberOfAdjacentLifeForms++;
- //top left and top right
- 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))
- NumberOfAdjacentLifeForms++;
- 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))
- NumberOfAdjacentLifeForms++;
- 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 aBoard)
- {
- ofstream myfile;
- myfile.open ("bugs2.txt");
- myfile << endl << endl << "after" << endl;
- //myfile << "IN SWITCH 0";
- int counter = 0;
- 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();
- }
Advertisement
Add Comment
Please, Sign In to add comment