Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_ghost_gol_toroidal_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
47#include <iostream>
48#include <utility>
49
50#include <ca-traits.H>
51#include <tpl_ca_storage.H>
53#include <tpl_ca_neighborhood.H>
54#include <tpl_ca_rule.H>
55#include <tpl_ca_engine.H>
56
57using namespace Aleph;
58using namespace Aleph::CA;
59
60namespace
61{
62 constexpr ca_size_t N = 12;
63 constexpr std::size_t TOTAL_STEPS = 48;
64 constexpr std::size_t PRINT_EVERY = 4;
65
69
72 {
73 // Canonical 5-cell glider:
74 // . X .
75 // . . X
76 // X X X
77 lat.set({ 0, 1 }, 1);
78 lat.set({ 1, 2 }, 1);
79 lat.set({ 2, 0 }, 1);
80 lat.set({ 2, 1 }, 1);
81 lat.set({ 2, 2 }, 1);
82 }
83
84 void render_frame(const GhostL &lat, std::size_t step)
85 {
86 std::cout << "\nStep " << step << '\n';
87 std::cout << "+";
88 for (ca_size_t j = 0; j < lat.size(1); ++j)
89 std::cout << '-';
90 std::cout << "+\n";
91 for (ca_index_t i = 0; i < static_cast<ca_index_t>(lat.size(0)); ++i)
92 {
93 std::cout << '|';
94 for (ca_index_t j = 0; j < static_cast<ca_index_t>(lat.size(1)); ++j)
95 std::cout << (lat.at({ i, j }) ? '#' : ' ');
96 std::cout << "|\n";
97 }
98 std::cout << "+";
99 for (ca_size_t j = 0; j < lat.size(1); ++j)
100 std::cout << '-';
101 std::cout << "+\n";
102 }
103}
104
105int main()
106{
107 std::cout <<
108 "Game of Life on a 12x12 Ghost_Lattice (PeriodicBoundary, halo=1).\n"
109 "A glider is stamped near the top-left corner. It walks diagonally\n"
110 "at one cell every four generations and wraps cleanly across the\n"
111 "torus because the engine refreshes the halo before each step.\n";
112
113 GhostL lat({ N, N }, 0);
116
117 render_frame(engine.frame(), 0);
118 for (std::size_t s = 1; s <= TOTAL_STEPS; ++s)
119 {
120 engine.step();
121 if (s % PRINT_EVERY == 0)
122 render_frame(engine.frame(), s);
123 }
124 return 0;
125}
Common typedefs and tag types for the Cellular Automata module.
Row-major dense storage for N-dimensional cellular automata.
Lattice with Halo ghost layers around the user-visible cells.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine.
#define N
Definition fib.C:294
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
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
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
static mt19937 engine
Synchronous double-buffered engine for cellular automata.
Ghost-layer lattice for high-performance boundary handling.
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).