enum CellProcessing checkRule(struct Cell *cell, const struct Rule *rule) { enum CellProcessing cProc; unsigned char aliveCounter = 0; bool satisfy; bool cellAlive; cellAlive = isCellAlive(cell); aliveCounter = getCellRefs(cell); if (cellAlive) { satisfy = checkSubrule(rule->survive, aliveCounter); if (satisfy) cProc = GOL_SURVIVE; else cProc = GOL_KILL; } else{ satisfy = checkSubrule(rule->birth, aliveCounter); if (satisfy) cProc = GOL_REVIVE; else cProc = GOL_KEEP_DEAD; } return cProc; }
int CellGrid::getNumLiveNeighbors( int px, int py ) { int ln = ( isCellAlive( px, py ) ) ? -1 : 0; for( int x = px - 1, xc = 0; xc < 3; xc++, x++ ) { for( int y = py - 1, yc = 0; yc < 3; yc++, y++ ) { if( isValidPos( x, y ) && isCellAlive( x, y ) ) { ln++; } } } return ln; }
void CellGrid::iterateCell( int x, int y ) { int ln = getNumLiveNeighbors( x, y ); if( isCellAlive( x, y ) ) { if( ln < 2 ) setNextCellState( x, y, false ); else if( ln == 2 || ln == 3 ) setNextCellState( x, y, true ); else if( ln > 3 ) setNextCellState( x, y, false ); } else { if( ln == 3 ) setNextCellState( x, y, true ); } }
void CellGrid::draw( sf::RenderWindow& w, float cellSize, float cellSpacing, sf::Color deadColor, sf::Color aliveColor, sf::Vector2f offset ) { sf::RectangleShape s; s.setSize( sf::Vector2f( cellSize, cellSize ) ); for( int i = 0; i < mWidth; i++ ) { for( int j = 0; j < mHeight; j++ ) { if( isCellAlive( i, j ) ) s.setFillColor( aliveColor ); else s.setFillColor( deadColor ); s.setPosition( sf::Vector2f( i * ( cellSize + cellSpacing), j * ( cellSize + cellSpacing) ) + offset ); w.draw( s ); } } }
bool CellGrid::isCellAlive( sf::Vector2i v ) const { return isCellAlive( v.x, v.y ); }