-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise1_ca_game_of_life.cpp
More file actions
157 lines (142 loc) · 4.34 KB
/
exercise1_ca_game_of_life.cpp
File metadata and controls
157 lines (142 loc) · 4.34 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
using namespace std;
const char ALIVE = 'X';
const char DEAD = '.';
// Type alias for readability
using Grid = vector<vector<bool>>;
// Print the current state of the grid
void print_grid(const Grid& grid)
{
for (size_t y = 0; y < grid.size(); ++y) {
for (size_t x = 0; x < grid[0].size(); ++x) {
std::cout << (grid[y][x] ? ALIVE : DEAD);
}
std::cout << std::endl;
}
std::cout << std::endl;
}
// Count alive neighbors of a cell at (x, y)
int count_alive_neighbors(const Grid& grid, int x, int y)
{
int count = 0;
for (int dy = -1; dy <= 1; ++dy) {
for (int dx = -1; dx <= 1; ++dx) {
if (dx == 0 && dy == 0)
continue; // skip self
int nx = x + dx;
int ny = y + dy;
// using boundary conditions where all boundaries are considered dead
if (nx >= 0 && nx < grid[0].size() && ny >= 0 && ny < grid.size()) {
count += grid[ny][nx];
}
}
}
return count;
}
// Compute the next generation
Grid advance(const Grid& current)
{
Grid next = current; // create copy of the current grid
for (int y = 0; y < current.size(); ++y) {
for (int x = 0; x < current[0].size(); ++x) {
int neighbors = count_alive_neighbors(current, x, y);
if (current[y][x]) {
next[y][x] = (neighbors == 2 || neighbors == 3);
}
else {
next[y][x] = (neighbors == 3);
}
}
}
return next;
}
// Initialize a grid with random state
Grid random_grid(int dimX, int dimY)
{
Grid grid(dimY, vector<bool>(dimX, false));
for (int y = 0; y < dimY; ++y) {
for (int x = 0; x < dimX; ++x) {
grid[y][x] = rand() % 2;
}
}
return grid;
}
Grid static_grid(int dimX, int dimY)
{
Grid grid(dimY, vector<bool>(dimX, false));
for (int y = 0; y < dimY; ++y) {
for (int x = 0; x < dimX; ++x) {
grid[y][x] = 0;
}
}
grid[dimY / 2][dimX / 2] = 1;
grid[dimY / 2][dimX / 2 + 1] = 1;
grid[dimY / 2 + 1][dimX / 2] = 1;
grid[dimY / 2 + 1][dimX / 2 + 1] = 1;
return grid;
}
Grid alternating_grid(int dimX, int dimY)
{
Grid grid(dimY, vector<bool>(dimX, false));
for (int y = 0; y < dimY; ++y) {
for (int x = 0; x < dimX; ++x) {
grid[y][x] = 0;
}
}
grid[dimY / 2][dimX / 2] = 1;
grid[dimY / 2][dimX / 2 + 1] = 1;
grid[dimY / 2][dimX / 2 - 1] = 1;
return grid;
}
void parse_args(int argc, char* argv[], int& dimX, int& dimY, int& generations)
{
for (int i = 1; i < argc - 1; ++i) {
string arg = argv[i];
if (arg == "--dimX") {
dimX = std::stoi(argv[++i]);
}
else if (arg == "--dimY") {
dimY = std::stoi(argv[++i]);
}
else if (arg == "--gen") {
generations = std::stoi(argv[++i]);
}
else {
std::cerr << "Unknown or malformed argument. Using standard parameters." << std::endl;
std::cerr << "Usage: " << argv[0] << " [--dimX L] [--dimY M] [--gen N]\n";
}
}
}
int main(int argc, char* argv[])
{
int dimX = 10;
int dimY = 10;
int generations = 5;
parse_args(argc, argv, dimX, dimY, generations);
std::cout << "Grid dimensions: " << dimX << "x" << dimY << ", Generations: " << generations << std::endl;
srand(0);
Grid grid = alternating_grid(dimX, dimY);
print_grid(grid);
for (int gen = 0; gen < generations; ++gen) {
grid = advance(grid);
print_grid(grid);
this_thread::sleep_for(std::chrono::milliseconds(200));
}
for (int scale = 1; scale <= 9; ++scale) {
dimX *= 2;
dimY *= 2;
std::cout << "Scaled grid dimensions: " << dimX << "x" << dimY << std::endl;
Grid grid = random_grid(dimX, dimY);
auto start = std::chrono::steady_clock::now();
for (int gen = 0; gen < generations; ++gen) {
grid = advance(grid);
}
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Elapsed time: " << duration.count() << " ms\n";
}
return 0;
}