Добавил функцию для подсчёта живых клеток

This commit is contained in:
2024-12-07 11:23:39 +03:00
parent a7043f4843
commit 772bc2446a
3 changed files with 20 additions and 0 deletions

View File

@@ -101,4 +101,20 @@ void CellularAutomaton::displayField() const
}
std::cout << '\n';
}
}
int CellularAutomaton::countLiveCells() const
{
int liveCount = 0;
for (const auto& row : m_field)
{
for (const auto& cell : row)
{
if (cell == 1)
{
++liveCount;
}
}
}
return liveCount;
}

View File

@@ -29,5 +29,7 @@ public:
void update();
void displayField() const;
int countLiveCells() const;
};

View File

@@ -51,12 +51,14 @@ int main()
cout << "\nИтерация 0:\n";
ca.displayField();
//cout << ca.countLiveCells() << "\n";
for (int i = 0; i < iterationsCount; ++i)
{
cout << "\nИтерация " << i + 1 << ":\n";
ca.update();
ca.displayField();
//cout << ca.countLiveCells() << "\n";
}
cout << "\nНажмите на enter, чтобы продолжить...";