#include "TimeStep.h" // default constructor TimeStep::TimeStep() { Cell newCell; for(int a=0; a<=11; a++) for(int b=0; b<=11; b++) grid[a][b] = newCell; } // constructor -- takes the step number TimeStep::TimeStep(int n) { step = n; Cell newCell; for(int a=0; a<=11; a++) for(int b=0; b<=11; b++) grid[a][b] = newCell; } //returns the particular cell // (the actual object, not just its value) // specified by row x and column y in the 2D CA. Cell& TimeStep::TheCell(int x, int y) { return grid[x][y]; } //returns the number of on cells around a particular cell //notice that I have to use the previous step's information int TimeStep::On(int x, int y) { on_counter = 0; for(int i = x-1; i<=x+1; i++) for(int j= y-1; j<=y+1; j++) { on_counter += TheCell(i,j).status; } on_counter -= TheCell(x,y).status; //subtract the status of the cell itself; we don't want to count it //it's easiest to construct these for loops to count all 9 //of them, then subtract the middle one return on_counter; } //determines the next step; the rule is S23/B3 // ("Conway's Game of Life"): // a cell remains alive if it is surrounded by // 2 or 3 alive cells; a cell is born if it // surrounded by 3 alive cells -- otherwise the cell // dies or remains dead void TimeStep::EvolutionConwaysLife(TimeStep previousStep) { for(int q=1; q<=10; q++) { for(int r=1; r<=10; r++) { if (previousStep.TheCell(q,r).status == 0) { if (previousStep.On(q,r) == 3) { TheCell(q,r).status = 1; //born; otherwise still dead } } else // else alive (status is 1) { if ((previousStep.On(q,r) !=2) && (previousStep.On(q,r)!=3)) { TheCell(q,r).status = 0; // dies; otherwise still alive // (see the "else" below) } else { TheCell(q,r).status = 1; //born; otherwise still dead // must explicitly set the status of the remaining live cells // to 1 because "this" is a newly created TimeStep that was // created with all cells defaulted to a status of 0 } } } } } //determines the next step; the rule is S5/B345 // ("Long Life"): // a cell remains alive if it is surrounded by // 5 alive cells; a cell is born if it // surrounded by 3, 4, or 5 alive cells -- // otherwise the cell // dies or remains dead void TimeStep::EvolutionLongLife(TimeStep previousStep) { for(int q=1; q<=10; q++) { for(int r=1; r<=10; r++) { if (previousStep.TheCell(q,r).status == 0) { if ((previousStep.On(q,r) == 3) || (previousStep.On(q,r) == 4) || (previousStep.On(q,r) == 5)) { TheCell(q,r).status = 1; //born; otherwise still dead } } else // else alive (status is 1) { if ((previousStep.On(q,r) != 5)) { TheCell(q,r).status = 0; // dies; otherwise still alive // (see the "else" below) } else { TheCell(q,r).status = 1; //born; otherwise still dead // must explicitly set the status of the remaining live cells // to 1 because "this" is a newly created TimeStep that was // created with all cells defaulted to a status of 0 } } } } }