From 1d5ed538c385145e26a800a42c068050c1719672 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Tue, 3 Dec 2024 19:17:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=B1=D0=BE=D1=80=20=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=87=D0=BD=D1=8B=D1=85=20=D1=83=D1=81=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab1/CellularAutomaton.cpp | 18 +++++++++++++++--- lab1/CellularAutomaton.h | 8 +++++++- lab1/lab1.cpp | 10 ++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lab1/CellularAutomaton.cpp b/lab1/CellularAutomaton.cpp index 006063a..c1b3aff 100644 --- a/lab1/CellularAutomaton.cpp +++ b/lab1/CellularAutomaton.cpp @@ -1,6 +1,7 @@ #include "CellularAutomaton.h" -CellularAutomaton::CellularAutomaton(int width, int height, bool fillWithRandom) : m_fieldWidth(width), m_fieldHeight(height) +CellularAutomaton::CellularAutomaton(int width, int height, bool fillWithRandom, BoundaryCondition boundaryCondition) + : m_fieldWidth(width), m_fieldHeight(height), m_boundaryCondition(boundaryCondition) { field.resize(m_fieldHeight, std::vector(m_fieldWidth, 0)); fieldNextState.resize(m_fieldHeight, std::vector(m_fieldWidth, 0)); @@ -46,8 +47,19 @@ int CellularAutomaton::getCellState(int x, int y) const { if (x < 0 || x >= m_fieldWidth || y < 0 || y >= m_fieldHeight) { - // Åäèíè÷íûå ãðàíè÷íûå óñëîâèÿ: çà ïðåäåëàìè ïîëÿ êëåòêà ñ÷èòàåòñÿ ðàâíîé 1 - return 1; + switch (m_boundaryCondition) + { + case BOUNDARY_ONES: + return 1; + case BOUNDARY_ZEROS: + return 0; + case BOUNDARY_TOROIDAL: + x = (x + m_fieldWidth) % m_fieldWidth; + y = (y + m_fieldHeight) % m_fieldHeight; + return field[y][x]; + default: + return 0; + } } return field[y][x]; } diff --git a/lab1/CellularAutomaton.h b/lab1/CellularAutomaton.h index 5abd91c..d0e51f7 100644 --- a/lab1/CellularAutomaton.h +++ b/lab1/CellularAutomaton.h @@ -4,6 +4,11 @@ #include #include "io.h" +enum BoundaryCondition { + BOUNDARY_ONES, + BOUNDARY_ZEROS, + BOUNDARY_TOROIDAL +}; class CellularAutomaton { @@ -12,6 +17,7 @@ class CellularAutomaton int m_fieldWidth, m_fieldHeight; std::vector> field; std::vector> fieldNextState; + BoundaryCondition m_boundaryCondition; void initializeRandom(); void initializeManual(); @@ -19,7 +25,7 @@ class CellularAutomaton int getCellState(int x, int y) const; int getNeighborhoodIndex(int x, int y) const; public: - CellularAutomaton(int width, int height, bool fillWithRandom); + CellularAutomaton(int width, int height, bool fillWithRandom, BoundaryCondition boundaryCondition); void update(); void displayField() const; diff --git a/lab1/lab1.cpp b/lab1/lab1.cpp index 4d7536b..e5d5d95 100644 --- a/lab1/lab1.cpp +++ b/lab1/lab1.cpp @@ -42,7 +42,13 @@ int main() cout << "Заполнить поле Ñлучайными значениÑми? (yes/no)\n"; bool fillWithRandom = userApprove(); - CellularAutomaton ca(fieldWidth, fieldHeight, fillWithRandom); + cout << "\nВыберите граничные уÑловиÑ:\n" + "Ðулевые (0)\n" + "Единичные (1)\n" + "Торроидальные (2)\n\n"; + BoundaryCondition boundaryCondition = static_cast(inputNumber(0, 2)); + + CellularAutomaton ca(fieldWidth, fieldHeight, fillWithRandom, boundaryCondition); clear(); cout << "\nÐ˜Ñ‚ÐµÑ€Ð°Ñ†Ð¸Ñ 0:\n"; @@ -55,7 +61,7 @@ int main() ca.displayField(); } - cout << "Ðажмите на enter, чтобы продолжить..."; + cout << "\nÐажмите на enter, чтобы продолжить..."; waitForEnter(); } } \ No newline at end of file