/
mando
/
LIfeIsAGame
Обзор
Документация
Войти
/
mando
/
LIfeIsAGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Source.cpp
109 строк
3 KB
mando
LIfeIsAGame
04 мар 2026, 23:18
Верифицирован
04 мар 2026, 23:18
805cc2b
Код
Авторство
О чём код?
#include <iostream> #include <vector> #include <fstream> #include <windows.h> #include <string> using namespace std; int countAlive(const vector<vector<bool>>& field, int H, int W) { int count = 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (field[i][j]) count++; return count; } int main() { ifstream inputFile("input.txt"); if (!inputFile.is_open()) { cout << "Error: Could not open input.txt" << endl; return 1; } int H, W; if (!(inputFile >> H >> W)) return 1; vector<vector<bool>> field(H, vector<bool>(W, false)); int r, c; while (inputFile >> r >> c) { if (r >= 0 && r < H && c >= 0 && c < W) { field[r][c] = true; } } inputFile.close(); int generation = 0; string reason = ""; while (true) { system("cls"); int aliveCount = countAlive(field, H, W); cout << "Generation: " << generation << " | Alive cell: " << aliveCount << endl; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << (field[i][j] ? "*" : "-") << " "; } cout << endl; } if (aliveCount == 0) { reason = "All cell are dead."; break; } vector<vector<bool>> next = field; bool changed = false; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int neighbors = 0; for (int y = -1; y <= 1; y++) { for (int x = -1; x <= 1; x++) { if (y == 0 && x == 0) continue; int ni = i + y, nj = j + x; if (ni >= 0 && ni < H && nj >= 0 && nj < W && field[ni][nj]) neighbors++; } } if (field[i][j]) { next[i][j] = (neighbors == 2 || neighbors == 3); } else { next[i][j] = (neighbors == 3); } if (next[i][j] != field[i][j]) changed = true; } } if (!changed) { reason = "Population has stabilized."; break; } if (GetAsyncKeyState(VK_ESCAPE)) { reason = "Stopped by user."; break; } field = next; generation++; Sleep(200); } cout << "\n--- GAME OVER ---" << endl; cout << "Reason: " << reason << endl; cout << "Final Generation: " << generation << endl; cout << "Alive cells: " << countAlive(field, H, W) << endl; return 0; }