How to resolve the algorithm Wireworld step by step in the C programming language
How to resolve the algorithm Wireworld step by step in the C programming language
Table of Contents
Problem Statement
Wireworld is a cellular automaton with some similarities to Conway's Game of Life. It is capable of doing sophisticated computations with appropriate programs (it is actually Turing complete), and is much simpler to program for. A Wireworld arena consists of a Cartesian grid of cells, each of which can be in one of four states. All cell transitions happen simultaneously. The cell transition rules are this:
Create a program that reads a Wireworld program from a file and displays an animation of the processing. Here is a sample description file (using "H" for an electron head, "t" for a tail, "." for a conductor and a space for empty) you may wish to test with, which demonstrates two cycle-3 generators and an inhibit gate: While text-only implementations of this task are possible, mapping cells to pixels is advisable if you wish to be able to display large designs. The logic is not significantly more complex.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Wireworld step by step in the C programming language
This C code simulates the Game of Life, a cellular automaton popularized by John Conway in 1970, on a 14x7 grid and displays the result in an infinite loop. Let's break down the code step by step:
-
Game of Life Simulation:
-
world_7x14[2][512]
: A 2D array of characters representing the current and next game states. -
next_world(const char *in, char *out, int w, int h)
: This function calculates the next generation of the Game of Life based on the current state (in
) and stores it in the output buffer (out
). It iterates through each cell and applies the following rules:- A space stays a space.
- A 't' becomes a '.'.
- An 'H' becomes a 't'.
- A '.' becomes an 'H' if it has exactly 1 or 2 neighboring 'H' cells.
-
-
Main Game Loop:
-
The
main
function enters an infinite loop, alternating between the two segments of theworld_7x14
array. -
In each iteration of the loop:
- It prints the current world state from
world_7x14[f]
. - It calculates the next world state using
next_world
. - For visualization purposes, it uses VT100 escape sequences (if
ANIMATE_VT100_POSIX
is defined) to move the cursor to the top left corner and refresh the display. - It pauses for 100 milliseconds using
nanosleep
to create an animation effect.
- It prints the current world state from
-
-
VT100 Animation (Optional):
- If
ANIMATE_VT100_POSIX
is defined, the code uses VT100 escape sequences to position the cursor and refresh the display, creating an animation effect. - The escape sequences used are:
\x1b[%dA
: Move the cursor upn
lines.\x1b[%dD
: Move the cursor rightn
columns.
- If
Source code in the c programming language
/* 2009-09-27 <kaz@kylheku.com> */
#define ANIMATE_VT100_POSIX
#include <stdio.h>
#include <string.h>
#ifdef ANIMATE_VT100_POSIX
#include <time.h>
#endif
char world_7x14[2][512] = {
{
"+-----------+\n"
"|tH.........|\n"
"|. . |\n"
"| ... |\n"
"|. . |\n"
"|Ht.. ......|\n"
"+-----------+\n"
}
};
void next_world(const char *in, char *out, int w, int h)
{
int i;
for (i = 0; i < w*h; i++) {
switch (in[i]) {
case ' ': out[i] = ' '; break;
case 't': out[i] = '.'; break;
case 'H': out[i] = 't'; break;
case '.': {
int hc = (in[i-w-1] == 'H') + (in[i-w] == 'H') + (in[i-w+1] == 'H') +
(in[i-1] == 'H') + (in[i+1] == 'H') +
(in[i+w-1] == 'H') + (in[i+w] == 'H') + (in[i+w+1] == 'H');
out[i] = (hc == 1 || hc == 2) ? 'H' : '.';
break;
}
default:
out[i] = in[i];
}
}
out[i] = in[i];
}
int main()
{
int f;
for (f = 0; ; f = 1 - f) {
puts(world_7x14[f]);
next_world(world_7x14[f], world_7x14[1-f], 14, 7);
#ifdef ANIMATE_VT100_POSIX
printf("\x1b[%dA", 8);
printf("\x1b[%dD", 14);
{
static const struct timespec ts = { 0, 100000000 };
nanosleep(&ts, 0);
}
#endif
}
return 0;
}
You may also check:How to resolve the algorithm Iterated digits squaring step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the C++ programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the JavaScript programming language
You may also check:How to resolve the algorithm String case step by step in the RPL programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Neko programming language