-
Notifications
You must be signed in to change notification settings - Fork 0
/
gand.c
85 lines (75 loc) · 2.56 KB
/
gand.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#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,1,0,0,1,0}; /*rule is set to popular "rule 18"*/
// int y[8]= {0,0,0,1,0,0,1,1}; /*rule is set to popular "rule 18"*/
int x[COLUMNS],z[COLUMNS];
int r,j,k;
FILE *fp;
fp=fopen("gand.txt","w");
fprintf(fp,"Set font size to 1 on your text viewer to see the larger picture\n");
fprintf(fp,"If you are using notepad click Format->font and change the font size\n\n\n");
fprintf(fp,"rule %d%d%d%d%d%d%d%d\n ",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,"%s","gand ");
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,"%s","gand");
else fprintf(fp,"%c",B);
}
fprintf(fp,"\n ");
}
printf("open gand.txt to see cellular automata pattern..!!\n\n");
return 0;
}