Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_game_of_life_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
54# include <chrono>
55# include <cerrno>
56# include <cstdlib>
57# include <iostream>
58# include <limits>
59# include <string>
60# include <thread>
61# include <utility>
62# include <vector>
63
64# include <ca-traits.H>
65# include <tpl_ca_storage.H>
66# include <tpl_ca_lattice.H>
67# include <ca-engine-utils.H>
68
69using namespace Aleph;
70using namespace Aleph::CA;
71
72namespace
73{
75 constexpr std::size_t Max_Steps = 100000;
76 constexpr ca_size_t Max_Dimension = 4096;
77
78 bool parse_positive_size(const char * text, const char * name,
79 std::size_t max_value, std::size_t & out)
80 {
81 if (text == nullptr or text[0] == '-')
82 {
83 std::cerr << "Invalid " << name << ": '"
84 << (text == nullptr ? "" : text)
85 << "' (expected a positive integer)\n";
86 return false;
87 }
88 errno = 0;
89 char * end = nullptr;
90 const unsigned long long value = std::strtoull(text, &end, 10);
91 if (errno == ERANGE or end == text or *end != '\0' or value == 0 or
92 value > std::numeric_limits<std::size_t>::max() or
93 value > max_value)
94 {
95 std::cerr << "Invalid " << name << ": '" << text
96 << "' (expected an integer in [1, "
97 << max_value << "])\n";
98 return false;
99 }
100 out = static_cast<std::size_t>(value);
101 return true;
102 }
103
104 bool parse_positive_ca_size(const char * text, const char * name,
105 ca_size_t & out)
106 {
107 std::size_t parsed = 0;
109 return false;
110 const auto ca_max = std::numeric_limits<ca_size_t>::max();
111 const auto idx_max = static_cast<std::size_t>(
112 std::numeric_limits<ca_index_t>::max());
113 if (parsed > ca_max or parsed > idx_max)
114 {
115 std::cerr << "Invalid " << name << ": '" << text
116 << "' (value is too large)\n";
117 return false;
118 }
119 out = static_cast<ca_size_t>(parsed);
120 return true;
121 }
122
124 void render(const Grid & g, std::size_t step, std::size_t alive)
125 {
126 const auto rows = static_cast<ca_index_t>(g.size(0));
127 const auto cols = static_cast<ca_index_t>(g.size(1));
128
129 std::cout << "\033[H"; // move cursor home; the caller has cleared the screen
130 std::cout << "Game of Life — step " << step
131 << " (" << alive << " alive)\n";
132 std::cout << '+' << std::string(cols, '-') << "+\n";
133 for (ca_index_t i = 0; i < rows; ++i)
134 {
135 std::cout << '|';
136 for (ca_index_t j = 0; j < cols; ++j)
137 std::cout << (g.at({ i, j }) ? '#' : ' ');
138 std::cout << "|\n";
139 }
140 std::cout << '+' << std::string(cols, '-') << "+\n";
141 std::cout.flush();
142 }
143
145 std::size_t alive_count(const Grid & g)
146 {
147 const auto rows = static_cast<ca_index_t>(g.size(0));
148 const auto cols = static_cast<ca_index_t>(g.size(1));
149 std::size_t a = 0;
150 for (ca_index_t i = 0; i < rows; ++i)
151 for (ca_index_t j = 0; j < cols; ++j)
152 if (g.at({ i, j })) ++a;
153 return a;
154 }
155
159 void stamp(Grid & g, ca_index_t cr, ca_index_t cc,
160 const std::vector<std::pair<int, int>> & pattern)
161 {
162 const auto rows = static_cast<ca_index_t>(g.size(0));
163 const auto cols = static_cast<ca_index_t>(g.size(1));
164 if (rows == 0 or cols == 0)
165 return;
166 for (auto [r, c] : pattern)
167 {
168 ca_index_t i = ((cr + r) % rows + rows) % rows;
169 ca_index_t j = ((cc + c) % cols + cols) % cols;
170 g.set({ i, j }, 1);
171 }
172 }
173
175 std::vector<std::pair<int, int>> pattern_for(const std::string & name)
176 {
177 if (name == "block")
178 return { {0,0}, {0,1}, {1,0}, {1,1} };
179
180 if (name == "blinker")
181 return { {0,-1}, {0,0}, {0,1} };
182
183 if (name == "toad")
184 return { {0,1}, {0,2}, {0,3},
185 {1,0}, {1,1}, {1,2} };
186
187 if (name == "beacon")
188 return { {0,0}, {0,1}, {1,0}, {1,1},
189 {2,2}, {2,3}, {3,2}, {3,3} };
190
191 if (name == "r-pentomino")
192 return { {0,1}, {0,2}, {1,0}, {1,1}, {2,1} };
193
194 if (name == "gosper")
195 return {
196 // Left block
197 {4,0}, {4,1}, {5,0}, {5,1},
198 // Left ship
199 {2,12}, {2,13}, {3,11}, {3,15}, {4,10}, {4,16}, {5,10}, {5,14},
200 {5,16}, {5,17}, {6,10}, {6,16}, {7,11}, {7,15}, {8,12}, {8,13},
201 // Right ship
202 {0,24}, {1,22}, {1,24}, {2,20}, {2,21}, {3,20}, {3,21},
203 {4,20}, {4,21}, {5,22}, {5,24}, {6,24},
204 // Right block
205 {2,34}, {2,35}, {3,34}, {3,35}
206 };
207
208 // Default
209 return { {0,1}, {1,2}, {2,0}, {2,1}, {2,2} }; // glider
210 }
211}
212
213int main(int argc, char ** argv)
214{
215 std::string pattern = (argc >= 2) ? argv[1] : "glider";
216 std::size_t steps = 40;
217 ca_size_t width = 32;
218 ca_size_t height = 16;
219
220 if (argc >= 3 and not parse_positive_size(argv[2], "steps",
222 return 1;
223 if (argc >= 4 and not parse_positive_ca_size(argv[3], "width", width))
224 return 1;
225 if (argc >= 5 and not parse_positive_ca_size(argv[4], "height", height))
226 return 1;
227
228 // Gosper's gun is wider than the default grid; auto-grow if needed.
229 if (pattern == "gosper")
230 {
231 if (width < 50) width = 50;
232 if (height < 20) height = 20;
233 }
234
235 Grid grid({ height, width }, 0);
236 stamp(grid, static_cast<ca_index_t>(height / 2),
237 static_cast<ca_index_t>(width / 2),
238 pattern_for(pattern));
239
240 auto engine = make_gol_engine(std::move(grid));
241
242 std::cout << "\033[2J"; // clear screen once; render() repaints in place
243 for (std::size_t t = 0; t <= steps; ++t)
244 {
245 const Grid & frame = engine.frame();
246 render(frame, engine.steps_run(), alive_count(frame));
247 if (t == steps) break;
248 std::this_thread::sleep_for(std::chrono::milliseconds(80));
249 engine.step();
250 }
251
252 return 0;
253}
int main()
size_t steps
Definition ca-c-api.h:126
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
Convenience builders for the Phase 3 synchronous engine.
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
Game_Of_Life_Engine make_gol_engine(ca_size_t rows, ca_size_t cols)
Build a Conway's Game of Life engine of the given shape.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
auto max_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute maximum value.
Definition stat_utils.H:294
The lattice wraps around on every axis.
Definition ca-traits.H:124
static mt19937 engine
gsl_rng * r
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).