One-dimensional cellular automata: Difference between revisions

→‎{{header|C}}: code simplification
(→‎{{header|C}}: code simplification)
Line 327:
=={{header|C}}==
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char trans[] = "___#_##_";
#define SPACEDIM 20
#define GENERATION 10
 
#define ALIVEv(i) (cell[i] != '#_')
int evolve(char cell[], char backup[], int len)
#define DEAD '_'
 
/* what happens out of the space: is the world a circle, or
it really ends? */
#define CCOND 0
 
char space[SPACEDIM];
char tspace[SPACEDIM];
 
int rrand(int l)
{
int i, diff = 0;
return (int)((double)l*(double)rand()/((double)RAND_MAX+1.0));
}
 
for (i = 0; i < len; i++) {
void initspace(char *s, int d)
/* use left, self, right as binary number bits for table index */
{
backup[i] = trans[ v(i-1) * 4 + v(i) * 2 + v(i + 1) ];
int i;
diff += (backup[i] != cell[i]);
static const char *tp = "_###_##_#_#_#_#__#__";
}
for(i=0; (i < strlen(tp)) && (i<d) ; i++)
{
s[i] = (tp[i] == ALIVE) ? 1 : 0;
}
}
 
strcpy(cell, backup);
void initspace_random(char *s, int d)
return diff;
{
int i;
for (i=0; i<d; i++)
{
s[i] = rrand(2);
}
}
 
/*
count the Number of Alive in the Neighbourhood
two kind of "bound condition" can be choosen
at compile time
*/
int nalive(const char *s, int i, int d)
{
switch ( CCOND )
{
case 0:
return ((i-1)<0 ? 0 : s[i-1]) + ((i+1)<d ? s[i+1] : 0 );
case 1:
return s[ (i+1)%d ] + s[ (i+d-1)%d ];
}
}
 
void evolve(const char *from, char *to, int d)
{
int i;
for(i=0; i<d; i++)
{
if ( from[i] )
{ /* 0 neighbour is solitude, 2 are one too much; 1, he's a friend */
if ( nalive(from, i, d) == 1 )
{
to[i] = 1;
} else {
to[i] = 0;
}
} else {
if ( nalive(from, i, d) == 2 )
{ /* there must be two, to make a child ... */
to[i] = 1;
} else {
to[i] = 0;
}
}
}
}
 
void show(const char *s, int d)
{
int i;
for(i=0; i<d; i++)
{
printf("%c", s[i] ? ALIVE : DEAD);
}
printf("\n");
}
 
 
int main()
{
char c[] = "_###_##_#_#_#_#__#__\n",
int i;
b[] = "____________________\n";
char *from, *to, *t;
initspace(space, SPACEDIM);
from = space; to = tspace;
for(i=0; i<GENERATION; i++)
{
show(from, SPACEDIM);
evolve(from, to, SPACEDIM);
t = from; from = to; to = t;
}
printf("\n");
initspace_random(space, SPACEDIM);
from = space; to = tspace;
for(i=0; i<GENERATION; i++)
{
show(from, SPACEDIM);
evolve(from, to, SPACEDIM);
t = from; from = to; to = t;
}
return 0;
}</lang>
 
The output is:
 
<pre style="height:10pc;overflow:scroll">_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________
__##________________
 
do { printf(c + 1); } while (evolve(c + 1, b + 1, sizeof(c) - 3));
#_###__#_#_#_#####_#
return 0;
_##_#___#_#_##___##_
_}</lang>output<lang>###_____#_##_#____#_#_#__#__
_#_#______##_#___#_#_#_#______
_##___##_#_#_______
__#_______###____##_
_##___###_#________
__________#_#____##_
_##___#_##_________
___________#_____##_
_##____###_________
_________________##_
_##____#_#_________
_________________##_
_##_____#__________
_________________##_
_##________________</lang>
</pre>
 
=={{header|C++}}==
Anonymous user