home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
245_01
/
lca23.c
< prev
next >
Wrap
Text File
|
1987-10-28
|
29KB
|
917 lines
/* (2,3) Linear Cellular Automaton */
/* Reference: */
/* */
/* Kenneth E. Perry */
/* Abstract Mathematical Art */
/* BYTE */
/* December, 1986 */
/* pages 181-192 */
/* Copyright (C) 1987 */
/* Harold V. McIntosh */
/* Gerardo Cisneros S. */
/* G. Cisneros, 4.3.87 */
/* 10 April 1987 - modified for (4,2) [HVM] */
/* 26 April 1987 - Multiple menus [HVM] */
/* 28 April 1987 - back modified to (4,1) [HVM] */
/* 28 April 1987 - version for XVI Feria de Puebla [HVM] */
/* 14 May 1987 - modified for (3,1) and general rule [HVM] */
/* 19 May 1987 - modified for (2,2) */
/* 20 May 1987 - modified for (2,3) */
# include <bdos.h>
# define COLGRAF 4 /* graph resolution */
# define T80X25 3 /* text resolution */
# define WHCYMAG 1 /* color quad for normal screen */
# define AL 320 /* array length (screen width) */
# define TS 8 /* distinct sums w/totalistic rule */
# define DS 32 /* (number of distinct neighborhoods)/4 */
# define KK 2 /* number of states per cell */
# define NX 14 /* number of sample rules */
char xrule[NX][KK][KK][KK][KK][KK];
char ixrule[NX][DS]=
"00AC324A8D30329A99A108A46C9FDCB7", /* cycles on dgl bgrnd */
"088686698669337E8669699E699E9EE7", /* class iv - try vnts */
"088E8CC78EAEE5888CC7E67FE7717118", /* very complex gliders */
"088E8EE78EE7E7798EE7E779E7797996", /* Extrmly good varnts */
"088071C431F94830C78A435E015198DC",
"1008088608868669088686698669699E", /* dominant triangles */
"11000100000010110111001011001100", /* flotsam & jetsam */
"28CA858DC0500D0512AAA0BACBBB94B3",
"381E3AFA19D7CECBCE5867F7206F7B6E", /* mottled */
"4A1ADAD80AAC3E8E8AEE4D138308E0FB", /* crossed gliders */
"88EF636569ABE6994C073A1FF3AC5EFE", /* */
"8A0FFA23E1FF8558675AFF0B53AD9FCB", /* dislocatns & regns */
"A3F73FA3A81F3F4AE4280DED0E317F1B",
"D2BF4202000000005CEDB7C000000000" /* dislocation */
;
char xx[4], rule[DS+1], ascrule[KK][KK][KK][KK][KK][KK][KK];
int binrule[KK][KK][KK][KK][KK][KK][KK], arule[DS], arr1[AL], arr2[AL];
char trule[TS]="00000000";
main()
{
int i, j, i0, i1, i2, i3, i4, i5, i6;
int more = 'r';
char a, b, c;
for (i=0; i<NX; i++) { /* copy to 5-index array */
i0=0; i1=0; i2=0; i3=0; i4=0;
for (j=0; j<DS; j++) {
xrule[i][i0][i1][i2][i3][i4]=ixrule[i][j];
i4++;
if (i4==KK) {i4=0; i3++;};
if (i3==KK) {i3=0; i2++;};
if (i2==KK) {i2=0; i1++;};
if (i1==KK) {i1=0; i0++;};
if (i0==KK) {i0=0; };
};};
videopalette(WHCYMAG); /* white/cyan/magenta */
tuto();
while (!kbdst()) rand(); /* wait for keypress */
kbdin(); /* ignore it */
videomode(T80X25);
videoscroll(3,0,5,71,0,3); /* menu on blue background */
videoscroll(19,0,24,71,0,3);
xtoasc(rand()%NX);
rule[DS]=0;
ranlin(); /* random initial array */
while (more!='n') { /* execute multiple runs */
rmenu();
lmenu();
while (0<1) { /* set up one run */
c=kbdin();
if (c=='g') break; /* go draw graph */
if (c=='q') more='n'; /* quit for good */
if (more=='n') break;
switch (c) {
case '@': /* numbered tot rule */
nutoto(numin(0));
totoasc();
rmenu();
videocursor(0,4,0);
break;
case '$': /* dozen totalistics */
j=numin(0);
for (i=0; i<12; i++) {
nutoto(j+i);
totoasc();
ranlin();
evolve(rule);
};
videomode(T80X25);
rmenu();
lmenu();
break;
case 't': /* totalistic rule */
xblnk();
tmenu();
edtrule();
totoasc();
for (i0=0; i0<KK; i0++) {
for (i1=0; i1<KK; i1++) {
for (i2=0; i2<KK; i2++) {
for (i3=0; i3<KK; i3++) {
for (i4=0; i4<KK; i4++) {
for (i5=0; i5<KK; i5++) {
for (i6=0; i6<KK; i6++) {
ascrule[i0][i1][i2][i3][i4][i5][i6]=trule[i0+i1+i2+i3+i4+i5+i6];
};};};};};};};
videocursor(0,4,0);
rmenu();
xmenu(totonu(0));
break;
case 'r': /* edit rule */
xblnk();
edrule();
videocursor(0,4,0);
rmenu();
break;
case 'l': /* edit cell string */
xblnk();
edline();
videocursor(0,3,0);
lmenu();
break;
case '#': /* read stored rule */
xmenu(NX);
xtoasc(lim(1,numin(0),NX)-1);
rmenu();
break;
case 'D': /* run through samples */
for (i=0; i<NX; i++) {
xmenu(i);
xtoasc(i);
ranlin();
evolve(rule);
};
videomode(T80X25);
rmenu();
break;
case 'u': /* sparse init arry */
xblnk();
for (i=0; i<AL; i++) arr1[i]=0;
arr1[AL/4]=1;
arr1[AL/2]=1;
arr1[(3*AL)/4]=1;
arr1[(3*AL)/4+2]=1;
lmenu();
break;
case 'x': /* random rule */
xblnk();
for (i0=0; i0<KK; i0++) {
for (i1=0; i1<KK; i1++) {
for (i2=0; i2<KK; i2++) {
for (i3=0; i3<KK; i3++) {
for (i4=0; i4<KK; i4++) {
for (i5=0; i5<KK; i5++) {
for (i6=0; i6<KK; i6++) {
if (i4 == 0) i=rand();
ascrule[i0][i1][i2][i3][i4][i5][i6]='0'+i%2;
i/=2;
};};};};};};};
rmenu();
break;
case 'y': /* random line */
xblnk();
ranlin();
lmenu();
break;
case 'Y': /* symmetrize rule */
for (i0=0; i0<KK; i0++) {
for (i1=0; i1<KK; i1++) {
for (i2=0; i2<KK; i2++) {
for (i3=0; i3<KK; i3++) {
for (i4=0; i4<KK; i4++) {
for (i5=0; i5<KK; i5++) {
for (i6=0; i6<KK; i6++) {
ascrule[i6][i5][i4][i3][i2][i1][i0]=ascrule[i0][i1][i2][i3][i4][i5][i6];
};};};};};};};
break;
case 'B': /* begin barrier */
a=kbdin();
b=kbdin();
ascrule[0][0][0][0][0][a-'0'][b-'0']=a;
ascrule[0][0][1][0][0][a-'0'][b-'0']=a;
ascrule[0][0][2][0][0][a-'0'][b-'0']=a;
rmenu();
break;
case 'E': /* end barrier */
a=kbdin();
b=kbdin();
ascrule[0][0][0][0][a-'0'][b-'0'][0]=b;
ascrule[0][0][0][0][a-'0'][b-'0'][1]=b;
ascrule[0][0][0][0][a-'0'][b-'0'][2]=b;
rmenu();
break;
case 'L': /* left glider link */
a=kbdin();
b=kbdin();
c=kbdin();
ascrule[0][0][0][0][a-'0'][b-'0'][c-'0']=c;
rmenu();
break;
case 'R': /* left glider link */
a=kbdin();
b=kbdin();
c=kbdin();
ascrule[0][0][0][0][a-'0'][b-'0'][c-'0']=a;
rmenu();
break;
case 'S': /* still life link */
a=kbdin();
b=kbdin();
c=kbdin();
ascrule[0][0][0][0][a-'0'][b-'0'][c-'0']=b;
rmenu();
break;
case '=':
for (i=1; i<8; i++) {
for (j=0; j<40; j++) arr1[40*i+j]=arr1[j];};
lmenu();
break;
case '~':
for (i=1; i<16; i++) {
for (j=0; j<20; j++) arr1[20*i+j]=arr1[j];};
lmenu();
break;
default: break;
};
};
if (more=='n') break;
do {
evolve(rule);
videocursor(0,0,0);
scrstr("?");
videocursor(0,0,34);
scrstr("y/n/cr");
more=kbdin();
} while (more=='\015');
videomode(T80X25); /* reset the screen */
if (more=='n') break;
};
videomode(T80X25);}
/* edit the rule */
edrule() {
char c;
int i, i0, i1, i2, i3, i4;
i=6; i0=0; i1=0; i2=0; i3=0; i4=0;
while (0<1) {
videocursor(0,3,i);
c = kbdin();
if (c == '\015') break; /* carriage return exits */
switch (c) {
case '0': /* state */
ascrule[i0][i1][i2][i3][i4][0][0]='0';
ascrule[i0][i1][i2][i3][i4][0][1]='0';
ascrule[i0][i1][i2][i3][i4][1][0]='0';
ascrule[i0][i1][i2][i3][i4][1][1]='0';
i4++;
if (i4==KK) {i4=0; i3++;};
if (i3==KK) {i3=0; i2++;};
if (i2==KK) {i2=0; i1++;};
if (i1==KK) {i1=0; i0++;};
if (i0==KK) {i0=0; };
videocattr(0,c,3,1);
if (i<6+DS) i++;