diff --git a/lab1/CellularAutomaton.cpp b/lab1/CellularAutomaton.cpp index aca171d..3419bd4 100644 --- a/lab1/CellularAutomaton.cpp +++ b/lab1/CellularAutomaton.cpp @@ -24,6 +24,43 @@ void CellularAutomaton::initializeRandom() } } +int CellularAutomaton::getCellState(int x, int y) const +{ + if (x < 0 || x >= m_fieldWidth || y < 0 || y >= m_fieldHeight) + { + // : 1 + return 1; + } + return field[y][x]; +} + +int CellularAutomaton::getNeighborhoodIndex(int x, int y) const +{ + int s0 = getCellState(x, y); + int s1 = getCellState(x, y - 1); + int s2 = getCellState(x, y + 1); + int s3 = getCellState(x - 1, y); + int s4 = getCellState(x + 1, y); + + int index = (s0 << 4) | (s1 << 3) | (s2 << 2) | (s3 << 1) | s4; + + return index; +} + +void CellularAutomaton::update() +{ + for (int y = 0; y < m_fieldHeight; ++y) + { + for (int x = 0; x < m_fieldWidth; ++x) + { + int neighborhood = getNeighborhoodIndex(x, y); + fieldNextState[y][x] = (functionValues >> neighborhood) & 1; + } + } + + field.swap(fieldNextState); +} + void CellularAutomaton::displayField() const { for (const auto& row : field) diff --git a/lab1/CellularAutomaton.h b/lab1/CellularAutomaton.h index c6fcef2..e1c987d 100644 --- a/lab1/CellularAutomaton.h +++ b/lab1/CellularAutomaton.h @@ -6,14 +6,19 @@ class CellularAutomaton { + static const unsigned int functionValues = 0b00000110100000000010110010110110; + int m_fieldWidth, m_fieldHeight; std::vector> field; std::vector> fieldNextState; void initializeRandom(); + int getCellState(int x, int y) const; + int getNeighborhoodIndex(int x, int y) const; public: CellularAutomaton(int width, int height); - + + void update(); void displayField() const; }; diff --git a/lab1/lab1.cpp b/lab1/lab1.cpp index 39946d8..298c7c9 100644 --- a/lab1/lab1.cpp +++ b/lab1/lab1.cpp @@ -40,9 +40,17 @@ int main() cout << "Укажите количество итераций (min 1): "; int iterationsCount = inputNumber(1); + clear(); + CellularAutomaton ca(fieldWidth, fieldHeight); - ca.displayField(); + for (int i = 0; i < iterationsCount; ++i) + { + std::cout << "\nИтерация " << i + 1 << ":\n"; + ca.update(); + ca.displayField(); + } + waitForEnter(); } } \ No newline at end of file