-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
80 lines (69 loc) · 2.15 KB
/
game.cpp
File metadata and controls
80 lines (69 loc) · 2.15 KB
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
//
// Created by Arthur Yuzeev on 10/18/23.
//
#include <cstdlib>
#include "game.h"
int count_neighbors(const int i, const int j, const bool *game_map) {
int count = 0;
for (int k = -1; k < 2; ++k) {
if (i - 1 >= 0 && j + k >= 0 && j + k < count_cell) {
count += game_map[(i - 1) * count_cell + (j + k)];
}
if (i + 1 < count_cell && j + k >= 0 && j + k < count_cell) {
count += game_map[(i + 1) * count_cell + (j + k)];
}
}
if (j + 1 < count_cell) {
count += game_map[i * count_cell + (j + 1)];
}
if (j - 1 >= 0) {
count += game_map[i * count_cell + (j - 1)];
}
return count;
}
void updateGameMap(bool *game_map) {
bool future_game_map[count_cell * count_cell];
for (bool &i: future_game_map) {
i = false;
}
for (int i = 0; i < count_cell; ++i) {
for (int j = 0; j < count_cell; ++j) {
if (game_map[i * count_cell + j] &&
(count_neighbors(i, j, game_map) < 2 || count_neighbors(i, j, game_map) > 3)) {
future_game_map[i * count_cell + j] = false;
} else if (!game_map[i * count_cell + j] && count_neighbors(i, j, game_map) == 3) {
future_game_map[i * count_cell + j] = true;
} else {
future_game_map[i * count_cell + j] = game_map[i * count_cell + j];
}
draw(i, j, future_game_map[i * count_cell + j]);
}
}
for (int i = 0; i < count_cell; ++i) {
for (int j = 0; j < count_cell; ++j) {
game_map[i * count_cell + j] = future_game_map[i * count_cell + j];
}
}
}
void app() {
bool game_map[count_cell * count_cell];
for (bool &i: game_map) {
i = false;
}
for (int i = 0; i < count_cell; ++i) {
for (int j = 0; j < count_cell; ++j) {
if (rand() % 2 == 0) {
game_map[i * count_cell + j] = true;
}
draw(i, j, game_map[i * count_cell + j]);
}
}
flush();
while (1) {
checkClose();
updateGameMap(game_map);
if (!flush()) {
return;
}
}
}