Grid NextTurn() { GridSet targets; GRID_SET_RESERVE(targets, m_cellMap.size() * 9); for (GridKey key : m_cellMap) { int y = key.first; int x = key.second; targets.insert(MakeKeySafe(x - 1, y - 1)); targets.insert(MakeKeySafe(x - 1, y + 0)); targets.insert(MakeKeySafe(x - 1, y + 1)); targets.insert(MakeKeySafe(x + 1, y - 1)); targets.insert(MakeKeySafe(x + 1, y + 0)); targets.insert(MakeKeySafe(x + 1, y + 1)); targets.insert(MakeKeySafe(x + 0, y - 1)); targets.insert(MakeKeySafe(x + 0, y + 0)); targets.insert(MakeKeySafe(x + 0, y + 1)); } Grid next(m_width, m_height); for (GridKey key : targets) { int y = key.first; int x = key.second; bool target = FindCell(x, y); int num = NumNeighbors(x, y); // Any live cell with fewer than two live neighbors dies, as if caused by underpopulation. // Any live cell with two or three live neighbors lives on to the next generation. // Any live cell with more than three live neighbors dies, as if by overpopulation. // Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. if (!target) { // Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. if (num == 3) { next.SetCell(x, y); } } else if (num == 2 || num == 3) { // Any live cell with fewer than two live neighbors dies, as if caused by under population. next.SetCell(x, y); } // Any live cell with more than three live neighbors dies, as if by overpopulation. ? } return std::move(next); }
void PopulateRandomlyUntilCount(int count) { GRID_SET_RESERVE(m_cellMap, count); for (int x = 0; x < m_width; ++x) { for (int y = 0; y < m_height; ++y) { if ((std::rand() % 16) == 0) { m_cellMap.insert(MakeKeySafe(x + 0, y + 0)); m_cellMap.insert(MakeKeySafe(x + 1, y + 0)); m_cellMap.insert(MakeKeySafe(x + 1, y + 1)); } if ((std::rand() % 8) == 0) { m_cellMap.insert(MakeKeySafe(x, y)); } if (m_cellMap.size() >= count) { return; } } } }