- 1D cellular automata Written using the very basics of C so it is easily readable.
- Generates all 256 possible rules(you can find specific rule code here)
- Creates a text file in the location C:\cellular_automata.txt of the size 15057KB
- Open that text file using any text viewer(notepad is good enough) and set to a lower font size and see the automata patterns
Of course the code looks too long. That's because i was using the very basics of the C language
/* (copyleft) no rights reserved*/
#include <stdio.h>
#include <stdlib.h>
#define COLUMNS 300
#define ITERATIONS 100
#define SEED 150
int main()
{
char F='O',B=' ';
int y[8]={0,0,0,0,0,0,0,0},x[COLUMNS],z[COLUMNS];
int r,j,k,t,i;
FILE *fp;fp=fopen("c:\\cellular_automata.txt","w");
fprintf(fp,"Set font size to 1 on your text viewer to see the larger picture\n");
fprintf(fp,"Hint: If you are using notepad click Format->font and change the font size\n\n\n");
for(i=1;i<=256;i++)
{
if(i%1==0) {if(y[7]==1)y[7]=0;else y[7]=1;}
if(i%2==0) {if(y[6]==1)y[6]=0;else y[6]=1;}
if(i%4==0) {if(y[5]==1)y[5]=0;else y[5]=1;}
if(i%8==0) {if(y[4]==1)y[4]=0;else y[4]=1;}
if(i%16==0) {if(y[3]==1)y[3]=0;else y[3]=1;}
if(i%32==0) {if(y[2]==1)y[2]=0;else y[2]=1;}
if(i%64==0) {if(y[1]==1)y[1]=0;else y[1]=1;}
if(i%128==0){if(y[0]==1)y[0]=0;else y[0]=1;}
fprintf(fp,"rule %d: %d%d%d%d%d%d%d%d\n ",i,y[0],y[1],y[2],y[3],y[4],y[5],y[6],y[7]);
for(j=0;j<COLUMNS;j++)
{
x[j]=0;
z[j]=0;
}
x[SEED]=1;
for(k=0;k<ITERATIONS;k++)
{
x[0]=x[COLUMNS-1]=0;
for(r=1;r<(COLUMNS-1);r++)
{
if( (x[r-1]==0)&&(x[r]==0)&&(x[r+1]==0) )
z[r]=y[7];
else if( (x[r-1]==0)&&(x[r]==0)&&(x[r+1]==1) )
z[r]=y[6];
else if( (x[r-1]==0)&&(x[r]==1)&&(x[r+1]==0) )
z[r]=y[5];
else if( (x[r-1]==0)&&(x[r]==1)&&(x[r+1]==1) )
z[r]=y[4];
else if( (x[r-1]==1)&&(x[r]==0)&&(x[r+1]==0) )
z[r]=y[3];
else if( (x[r-1]==1)&&(x[r]==0)&&(x[r+1]==1) )
z[r]=y[2];
else if( (x[r-1]==1)&&(x[r]==1)&&(x[r+1]==0) )
z[r]=y[1];
else if( (x[r-1]==1)&&(x[r]==1)&&(x[r+1]==1) )
z[r]=y[0];
if(z[r]==1)fprintf(fp,"%c",F);
else fprintf(fp,"%c",B);
}
fprintf(fp,"\n ");
z[0]=z[COLUMNS-1]=0;
for(r=1;r<(COLUMNS-1);r++)
{
if( (z[r-1]==0)&&(z[r]==0)&&(z[r+1]==0) )
x[r]=y[7];
else if( (z[r-1]==0)&&(z[r]==0)&&(z[r+1]==1) )
x[r]=y[6];
else if( (z[r-1]==0)&&(z[r]==1)&&(z[r+1]==0) )
x[r]=y[5];
else if( (z[r-1]==0)&&(z[r]==1)&&(z[r+1]==1) )
x[r]=y[4];
else if( (z[r-1]==1)&&(z[r]==0)&&(z[r+1]==0) )
x[r]=y[3];
else if( (z[r-1]==1)&&(z[r]==0)&&(z[r+1]==1) )
x[r]=y[2];
else if( (z[r-1]==1)&&(z[r]==1)&&(z[r+1]==0) )
x[r]=y[1];
else if( (z[r-1]==1)&&(z[r]==1)&&(z[r+1]==1) )
x[r]=y[0];
if(x[r]==1)fprintf(fp,"%c",F);
else fprintf(fp,"%c",B);
}
fprintf(fp,"\n ");
}
fprintf(fp,"\n\n\n");
}
printf("open c:\\cellular_automata.txt to see intrisique patterns of cellular automata..!!\n\n");
return 0;
}
Here are some examples
If you are not seeing patterns in Notepad set the font settings in notepad to following
Font: Lucida Console
Font style: Regular
Font size: 1
No comments:
Post a Comment