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 Grid::update() { if (!m_IsPlaying) return; // Maps coordinates to its new state std::map<std::pair<int,int>, int> stateChanges; for (int x = 0; x < NUM_CELLS; ++x) { for (int y = 0; y < NUM_CELLS; ++y) { auto numLiveNeighbors = getNumLiveNeighbors(x, y); if (m_Cells[x][y] == 1) { if ((numLiveNeighbors < 2) || (numLiveNeighbors > 3)) stateChanges[std::make_pair(x, y)] = 0; } else { if (numLiveNeighbors == 3) stateChanges[std::make_pair(x, y)] = 1; } } } for (auto& pair : stateChanges) { auto x = pair.first.first; auto y = pair.first.second; setCellState(x, y, pair.second); } ++m_NumGenerations; }