-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife-game.cpp
144 lines (120 loc) · 3.69 KB
/
life-game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#define matrix vector<vector<int>>
#define stop_program { cout << "Некорректный ввод."; return 0; }
#include <iostream>
#include <vector>
#include <array>
#include <fstream>
#include <windows.h>
using namespace std;
string FILE_NAME = "board.txt";
void genereateBoard(int width, int height, int letterCount)
{
ofstream file(FILE_NAME);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
file << (char)(rand() % letterCount + 'A') << " ";
file << endl;
}
}
matrix initBoard(int width, int height, char letterPicked)
{
ifstream file(FILE_NAME);
matrix board(height, vector<int>(width, 0));
char tmp;
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
file >> tmp;
if (tmp == letterPicked)
board[x][y] = 1;
}
return board;
}
char lifeLevelSymbol(int lifeLevel)
{
return (lifeLevel < 10) ? lifeLevel + 48 : 'A' + lifeLevel - 10;
}
void writeBoardToFile(matrix board)
{
ofstream file(FILE_NAME, ios_base::app);
file << endl;
for (int x = 0; x < board.size(); x++)
{
for (int y = 0; y < board[0].size(); y++)
file << lifeLevelSymbol(board[x][y]) << " ";
file << endl;
}
}
vector<array<int, 2>> getNeighbors(int x0, int y0, int width, int height)
{
vector<array<int, 2>> neighbors;
for (int x = -1; x <= 1; x++)
if (x0 + x >= 0 && x0 + x < width)
for (int y = -1; y <= 1; y++)
if (y0 + y >= 0 && y0 + y < height && (x != 0 || y != 0))
neighbors.push_back({ x0 + x, y0 + y });
return neighbors;
}
matrix updateBoard(matrix board)
{
int width = board.size(), height = board[0].size();
matrix newBoard(height, vector<int>(width, 0));
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
vector<array<int, 2>> neighborsCoord = getNeighbors(x, y, width, height);
int alive = 0;
for (auto neighbor : neighborsCoord)
if (board[neighbor[0]][neighbor[1]] != 0)
alive++;
if (((alive == 2 && board[x][y] != 0) || alive == 3) && board[x][y] < 11)
newBoard[x][y] = board[x][y] + 1;
}
return newBoard;
}
void printBoard(matrix board)
{
cout << endl;
for (int x = 0; x < board.size(); x++)
{
for (int y = 0; y < board[0].size(); y++)
cout << (board[x][y] != 0 ? lifeLevelSymbol(board[x][y]) : ' ') << " ";
cout << endl;
}
}
int main()
{
setlocale(LC_ALL, "Russian");
srand(time(NULL));
int letterCount;
cout << "Введите, сколько букв использовать для генерации поля: ";
cin >> letterCount;
if (letterCount > 26 || letterCount < 1)
stop_program
int width, height;
cout << "Введите длину и высоту поля: ";
cin >> width >> height;
genereateBoard(width, height, letterCount);
char randomLetter = (rand() % letterCount) + 'A';
matrix board = initBoard(width, height, randomLetter);
writeBoardToFile(board);
printBoard(board);
int updateCount;
cout << endl << "Введите количество ходов: ";
cin >> updateCount;
if (updateCount < 0)
stop_program
int delay;
cout << "Введите время между обновлениями (в миллисекундах): ";
cin >> delay;
if (delay < 0)
stop_program
for (int i = 0; i < updateCount; i++)
{
board = updateBoard(board);
writeBoardToFile(board);
printBoard(board);
Sleep(delay);
}
}