/
Vrtxn
/
cours_project
Обзор
Документация
Войти
/
Vrtxn
/
cours_project
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
cours_project.cpp
173 строки
5 KB
Vrtxn
update: cours_project.cpp
03 май 2026, 21:26
Верифицирован
03 май 2026, 21:26
4327e50
Код
Авторство
О чём код?
#include <iostream> #include <fstream> #ifdef _WIN32 #include "windows.h" void super_sleep(unsigned int seconds) { Sleep(1000 * seconds); } #else #include <unistd.h> void super_sleep(unsigned int seconds) { sleep(seconds); } #endif void clear_console() { #ifdef _WIN32 std::system("CLS"); #else std::system("clear"); #endif } int count_neighbors(bool** grid, int rows, int cols, int r, int c) { int count = 0; for (int dr = -1; dr <= 1; ++dr) { for (int dc = -1; dc <= 1; ++dc) { if (dr == 0 && dc == 0) continue; int nr = r + dr; int nc = c + dc; if (nr >= 0 && nr < rows && nc >= 0 && nc < cols) { if (grid[nr][nc]) ++count; } } } return count; } bool** allocate_grid(int rows, int cols) { bool** grid = new bool* [rows]; for (int i = 0; i < rows; ++i) { grid[i] = new bool[cols](); } return grid; } void deallocate_grid(bool** grid, int rows) { for (int i = 0; i < rows; ++i) { delete[] grid[i]; } delete[] grid; } void copy_grid(bool** src, bool** dst, int rows, int cols) { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { dst[i][j] = src[i][j]; } } } bool grids_equal(bool** a, bool** b, int rows, int cols) { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if (a[i][j] != b[i][j]) return false; } } return true; } int count_alive(bool** grid, int rows, int cols) { int cnt = 0; for (int i = 0; i < rows; ++i) for (int j = 0; j < cols; ++j) if (grid[i][j]) ++cnt; return cnt; } void print_grid(bool** grid, int rows, int cols, int generation, int alive, bool final = false, bool stagnation = false) { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { std::cout << (grid[i][j] ? '*' : '-'); if (j < cols - 1) std::cout << ' '; } std::cout << std::endl; } std::cout << "Generation: " << generation << ". Alive cells: " << alive; if (final) { if (stagnation) std::cout << "\nThe world has stagnated. Game over!"; else std::cout << "\nAll cells are dead. Game over!"; } std::cout << std::endl; } int main() { std::ifstream file("in.txt"); if (!file.is_open()) { std::cout << "Could not open file in.txt" << std::endl; return 1; } int rows, cols; file >> rows >> cols; bool** current = allocate_grid(rows, cols); bool** previous = allocate_grid(rows, cols); //инициализация живых клеток int r, c; while (file >> r >> c) { if (r >= 0 && r < rows && c >= 0 && c < cols) current[r][c] = true; } file.close(); int generation = 1; int alive = count_alive(current, rows, cols); //главный цикл while (true) { clear_console(); print_grid(current, rows, cols, generation, alive); if (alive == 0) { break; } //сохранение текущего состояние для сравнения copy_grid(current, previous, rows, cols); //вычисление следующего поколения for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { int neighbors = count_neighbors(previous, rows, cols, i, j); if (previous[i][j]) { //живая клетка if (neighbors < 2 || neighbors > 3) current[i][j] = false; else current[i][j] = true; } else { //мёртвая клетка if (neighbors == 3) current[i][j] = true; else current[i][j] = false; } } } int new_alive = count_alive(current, rows, cols); bool stagnated = grids_equal(current, previous, rows, cols); if (stagnated) { clear_console(); print_grid(current, rows, cols, generation + 1, new_alive, true, true); break; } if (new_alive == 0) { clear_console(); print_grid(current, rows, cols, generation + 1, 0, true, false); break; } generation++; alive = new_alive; super_sleep(1); } deallocate_grid(current, rows); deallocate_grid(previous, rows); return 0; }