/
ArtT
/
Netology
Обзор
Документация
Войти
/
ArtT
/
Netology
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Part1/Game_of_Life.cpp
151 строка
4 KB
ArtT
Навёл порядок
14 ноя 2025, 15:46
14 ноя 2025, 15:46
27cf136
Код
Авторство
О чём код?
#include <iostream> #include <fstream> #include <cstdlib> #include <unistd.h> using namespace std; static int countLife{0}; int** createArray(const int nRows, const int nCols) { int** arr = new int*[nRows]; for (int i = 0; i < nRows; i++){ arr[i] = new int[nCols](); } return arr; } void deleteArray(int ** arr, const int nRows) { for (int i = 0; i < nRows; i ++) delete[] arr[i]; delete[] arr; } void printCondition(int ** currentCondition, const int nRows, const int nCols) { system("cls"); static int step{1}; for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols; j++) { cout << (currentCondition[i][j]==1 ? "*" : "-") << " "; } cout << endl; } cout << "Generation: " << step << ". Alive cells: " << countLife << endl; step++; } int getState(int ** priorCondition, const int nRows, const int nCols, const int curRow, const int curCol) { int minIndR{-1}, maxIndR{1}, minIndC{-1}, maxIndC{1}; if (curRow == 0) minIndR = 0; else if (curRow == nRows - 1) maxIndR = 0; if (curCol == 0) minIndC = 0; else if (curCol == nCols - 1) maxIndC = 0; int livek{0}; int state = (priorCondition[curRow][curCol]); for (int i = minIndR; i <= maxIndR; i++) { for (int j = minIndC; j <= maxIndC; j++) { if (i == 0 && j == 0) continue; if (priorCondition[curRow+i][curCol+j] == 1) livek++; } } if (state == 1) //живая клетка { if (livek < 2 || livek > 3) state = 0; //умирает «от одиночества» или «от перенаселённости» } else //В пустой (мёртвой) клетке { if (livek == 3) state = 1; //рядом с которой ровно три живых клетки, зарождается жизнь. } return state; } bool CalculateCondition(int ** priorCondition, int **currentCondition, const int nRows, const int nCols) { bool needContinue = false; countLife = 0; for (int i = 0; i < nRows; i ++) { for (int j = 0; j < nCols; j++) { int state = getState(priorCondition, nRows, nCols, i, j); if (state == 1) countLife++; if (!needContinue && currentCondition[i][j] != state) needContinue = true; currentCondition[i][j]=state; } } return needContinue; } void rewriteCondition(int ** priorCondition, int **currentCondition, const int nRows, const int nCols) { for (int i = 0; i < nRows; i ++) { for (int j = 0; j < nCols; j++) { priorCondition[i][j] = currentCondition[i][j]; } } } int main() { ifstream fin("in_game_of_life.txt"); if(fin.is_open()) { int nRows, nCols; fin >> nRows >> nCols; int **priorCondition = createArray(nRows, nCols); int val{0}, i{0}, curRow{0}, curCol{0}; while (fin >> val){ if (i % 2 ==0) { curRow = val; } else { curCol = val; priorCondition[curRow][curCol] = 1; countLife++; } i++; } fin.close(); printCondition(priorCondition, nRows, nCols); int **currentCondition = createArray(nRows, nCols); bool needContinue = true; while(needContinue && countLife > 0){ sleep(1); needContinue = CalculateCondition(priorCondition, currentCondition, nRows, nCols); printCondition(currentCondition, nRows, nCols); rewriteCondition(priorCondition, currentCondition, nRows, nCols); } if (countLife == 0) cout << "All cells are dead. Game over." << endl; else cout << "The world has stagnated. Game over." << endl; deleteArray(currentCondition, nRows); deleteArray(priorCondition, nRows); } else { cout << "Не получилось открыть файл!" << endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }