Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_hex_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
44#include <cstdio>
45
46#include <ca-traits.H>
47#include <tpl_ca_engine.H>
48#include <tpl_ca_hex_lattice.H>
49#include <tpl_ca_neighborhood.H>
50#include <tpl_ca_rule.H>
51#include <tpl_ca_storage.H>
52
53using namespace Aleph;
54using namespace Aleph::CA;
55
56namespace {
57
58struct Bays_B2_S34_Functor
59{
60 template <typename State>
61 [[nodiscard]] constexpr State operator()(const State &current,
62 const std::size_t alive) const noexcept
63 {
64 const bool alive_now = current != State{};
65 const bool next_alive = (alive_now and (alive == 3 or alive == 4))
66 or (not alive_now and alive == 2);
67 return next_alive ? static_cast<State>(1) : static_cast<State>(0);
68 }
69};
70
72
73void print_offset_view(const L &lat, std::size_t step)
74{
75 std::size_t alive = 0;
76 for (ca_size_t r = 0; r < lat.rows(); ++r)
77 for (ca_size_t q = 0; q < lat.cols(); ++q)
78 if (lat.at_axial(Hex_Axial{static_cast<ca_index_t>(q), static_cast<ca_index_t>(r)})
79 != 0)
80 ++alive;
81
82 std::printf("\nStep %2zu alive=%zu\n", step, alive);
83 for (ca_size_t r = 0; r < lat.rows(); ++r)
84 {
85 // Indent odd rows by one space to visually approximate the hex
86 // offset-r staggered layout.
87 if ((r & 1) != 0)
88 std::putchar(' ');
89 for (ca_size_t q = 0; q < lat.cols(); ++q)
90 {
91 const auto v = lat.at_axial(
92 Hex_Axial{static_cast<ca_index_t>(q), static_cast<ca_index_t>(r)});
93 std::printf("%c ", v != 0 ? '#' : '.');
94 }
95 std::putchar('\n');
96 }
97}
98
99} // namespace
100
101int main()
102{
103 L seed({16, 16}, 0);
104
105 // Seed a small symmetric "ring" pattern (alive cells on the
106 // hex distance-2 circle around (8, 8)).
107 const Hex_Axial centre{8, 8};
108 for (ca_index_t dq = -2; dq <= 2; ++dq)
109 for (ca_index_t dr = -2; dr <= 2; ++dr)
110 {
111 const Hex_Axial p{centre.q + dq, centre.r + dr};
112 if (hex_distance(p, centre) == 2)
113 seed.set_axial(p, 1);
114 }
115
116 std::printf("Aleph::CA Phase-6 example: Bays' B2/S34 hex life on a 16x16 toroidal lattice\n");
118
121 seed, Rule{Bays_B2_S34_Functor{}}, Hex_Neighborhood{});
122
123 for (std::size_t s = 1; s <= 6; ++s)
124 {
125 eng.step();
126 print_offset_view(eng.frame(), s);
127 }
128
129 std::printf("\n--- TikZ render of step 6 ---\n");
130 std::fputs(render_hex_lattice_tikz(eng.frame(),
131 [](int v) {
132 return v != 0 ? "black" : "white";
133 },
134 /*radius=*/0.25)
135 .c_str(),
136 stdout);
137
138 return 0;
139}
Common typedefs and tag types for the Cellular Automata module.
int main()
Hexagonal lattice with axial accessors over a 2D storage.
Six-neighbour hex pattern in axial coordinates over a 2D lattice.
Rule whose next state depends on (current, alive_count).
Definition tpl_ca_rule.H:99
Synchronous double-buffered engine.
void step()
Apply the rule to every cell once and swap buffers.
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
void render_hex_lattice_tikz(std::ostream &os, const Lattice &lat, Palette &&palette, double radius=0.5, bool pointy_top=true)
Render a hex frame as a stand-alone TikZ picture.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
constexpr ca_size_t hex_distance(Hex_Axial a, Hex_Axial b) noexcept
Hex distance between two axial coordinates.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Axial integer coordinates (q, r) of a hex cell.
ca_index_t q
column index
The lattice wraps around on every axis.
Definition ca-traits.H:124
ValueArg< size_t > seed
Definition testHash.C:53
gsl_rng * r
Synchronous double-buffered engine for cellular automata.
Hexagonal lattice with axial / offset / cube coordinate conversions and a TikZ-compatible pixel mappi...
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).