Выбор граничных условий

This commit is contained in:
2024-12-03 19:17:26 +03:00
parent a3b00e15e2
commit 1d5ed538c3
3 changed files with 30 additions and 6 deletions

View File

@@ -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<int>(m_fieldWidth, 0));
fieldNextState.resize(m_fieldHeight, std::vector<int>(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)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 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];
}

View File

@@ -4,6 +4,11 @@
#include <iostream>
#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<std::vector<int>> field;
std::vector<std::vector<int>> 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;

View File

@@ -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<BoundaryCondition>(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();
}
}