How to resolve the algorithm Elementary cellular automaton step by step in the C# programming language
How to resolve the algorithm Elementary cellular automaton step by step in the C# programming language
Table of Contents
Problem Statement
An elementary cellular automaton is a one-dimensional cellular automaton where there are two possible states (labeled 0 and 1) and the rule to determine the state of a cell in the next generation depends only on the current state of the cell and its two immediate neighbors. Those three values can be encoded with three bits. The rules of evolution are then encoded with eight bits indicating the outcome of each of the eight possibilities 111, 110, 101, 100, 011, 010, 001 and 000 in this order. Thus for instance the rule 13 means that a state is updated to 1 only in the cases 011, 010 and 000, since 13 in binary is 0b00001101.
Create a subroutine, program or function that allows to create and visualize the evolution of any of the 256 possible elementary cellular automaton of arbitrary space length and for any given initial state. You can demonstrate your solution with any automaton of your choice. The space state should wrap: this means that the left-most cell should be considered as the right neighbor of the right-most cell, and reciprocally. This task is basically a generalization of one-dimensional cellular automata.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Elementary cellular automaton step by step in the C# programming language
The code is written in C# and implements a simple implementation of an elementary cellular automaton.
The code implements the automata model within the Automata
class, which contains the logic for running the automaton.
The automata is a one-dimensional array of cells, each of which can be either true
(alive) or false
(dead).
The evolution of the automata is determined by a rule, which specifies the state of a cell at the next time step based on the state of the cell itself and its two neighbors.
The run
method initializes the automata and then enters a loop in which it repeatedly updates the state of the automata based on the rule.
The doRule
method takes a rule number as input and sets up the initial state of the automata based on that rule.
The getCells
method retrieves the state of the three cells surrounding a given cell and returns a value that is used to determine the state of the central cell in the next time step.
The getBase2
method converts an integer to its binary representation.
The doRule
method updates the state of the automata based on the rule.
It first clears the console and then prints the rule number.
It then initializes the automata to an empty state and sets the state of the central cell to true
.
It then enters a loop in which it iterates through 51 generations of the automata.
For each generation, it prints the generation number and the state of the automata, and then it updates the state of the automata based on the rule.
The Main
method creates an instance of the Automata
class and calls its run
method, which starts the automata running.
Source code in the csharp programming language
using System;
using System.Collections;
namespace ElementaryCellularAutomaton
{
class Automata
{
BitArray cells, ncells;
const int MAX_CELLS = 19;
public void run()
{
cells = new BitArray(MAX_CELLS);
ncells = new BitArray(MAX_CELLS);
while (true)
{
Console.Clear();
Console.WriteLine("What Rule do you want to visualize");
doRule(int.Parse(Console.ReadLine()));
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
private byte getCells(int index)
{
byte b;
int i1 = index - 1,
i2 = index,
i3 = index + 1;
if (i1 < 0) i1 = MAX_CELLS - 1;
if (i3 >= MAX_CELLS) i3 -= MAX_CELLS;
b = Convert.ToByte(
4 * Convert.ToByte(cells.Get(i1)) +
2 * Convert.ToByte(cells.Get(i2)) +
Convert.ToByte(cells.Get(i3)));
return b;
}
private string getBase2(int i)
{
string s = Convert.ToString(i, 2);
while (s.Length < 8)
{ s = "0" + s; }
return s;
}
private void doRule(int rule)
{
Console.Clear();
string rl = getBase2(rule);
cells.SetAll(false);
ncells.SetAll(false);
cells.Set(MAX_CELLS / 2, true);
Console.WriteLine("Rule: " + rule + "\n----------\n");
for (int gen = 0; gen < 51; gen++)
{
Console.Write("{0, 4}", gen + ": ");
foreach (bool b in cells)
Console.Write(b ? "#" : ".");
Console.WriteLine("");
int i = 0;
while (true)
{
byte b = getCells(i);
ncells[i] = '1' == rl[7 - b] ? true : false;
if (++i == MAX_CELLS) break;
}
i = 0;
foreach (bool b in ncells)
cells[i++] = b;
}
Console.WriteLine("");
}
};
class Program
{
static void Main(string[] args)
{
Automata t = new Automata();
t.run();
}
}
}
You may also check:How to resolve the algorithm Distance and Bearing step by step in the Nim programming language
You may also check:How to resolve the algorithm Word wrap step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Fantom programming language
You may also check:How to resolve the algorithm 100 doors step by step in the SQL PL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Racket programming language