home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
275_01
/
lcau22.c
< prev
next >
Wrap
Text File
|
1980-01-01
|
20KB
|
675 lines
/* lcau22.c */
/* (2,2) Linear Cellular Automaton */
/* Reference: */
/* */
/* Kenneth E. Perry */
/* Abstract Mathematical Art */
/* BYTE */
/* December, 1986 */
/* pages 181-192 */
/* Copyright (C) 1987 */
/* Copyright (C) 1988 */
/* 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) [HVM] */
/* 20 May 1987 - modified for (2,3) [HVM] */
/* 21 May 1987 - Wolfram rule number [HVM] */
/* 8 June 1987 - general rule for (4,1) [HVM] */
/* 12 June 1987 - cartesian product of (2,1) rules [HVM] */
/* 12 June 1987 - (2,1) rule with memory [HVM] */
/* 14 June 1987 - individual cycles of evolution [HVM] */
/* 17 June 1987 - p-adic representation in plane [HVM] */
/* 22 June 1987 - 2 speed gliders via 16 transitions [HVM] */
/* 26 June 1987 - push, pop the rule [HVM] */
/* 26 June 1987 - conserve position of rule cursor [HVM] */
/* 27 June 1987 - incremental rule construction [HVM] */
/* 29 June 1987 - conserve position of cell pointer [HVM] */
/* 30 June 1987 - mark & unmark transitions, xchg x&X [HVM] */
/* 25 July 1987 - display and edit de Bruijn diagrams [HVM] */
/* 27 July 1987 - graph of transition probabilities [HVM] */
/* 4 September 1987 - PROB21.C for option 't' [HVM] */
/* 21 October 1987 - program disks disappeared */
/* 20 December 1987 - program reconstructed from listings */
/* 20 February 1988 - RIJN21.C for option 'd' [HVM] */
# include <bdos.h>
# define COLGRAF 4 /* graph resolution */
# define T80X25 3 /* text resolution */
# define WHCYMAG 1 /* color quad for normal screen */
# define YELREGR 2 /* alternative color palette */
# define CR 5 /* cursor reference - row editor */
# define CM 7 /* cursor reference - main menu */
# define CL 11 /* cursor reference - line editor */
# define AL 320 /* array length (screen width) */
# define SL 40 /* short array length */
# define KK 2 /* number of states per cell */
# define DS KK*KK*KK*KK*KK /* number of distinct neighborhoods */
# define TS 5*(KK-1)+1 /* distinct sums w/totalistic rule */
# define BD 25 /* max number Bernstein coeffs */
# define NX 7 /* number of sample rules */
char xrule[NX][KK][KK][KK][KK][KK];
char ixrule[NX][DS]=
"00010110011010010110100110010111", /* totalistic rule #52 */
"00010110011010010110100110010110", /* totalistic rule #20 */
"00010011101010011100110001100011", /* mottled */
"00101110000110101011001111000100", /* */
"01101000001100110011001100110011", /* */
"10010000101100110000000101111100", /* */
"11111101001100110011001100110011" /* */
;
char ascrule[KK][KK][KK][KK][KK];
char trule[TS]="000000";
int binrule[KK][KK][KK][KK][KK];
int arr1[AL], arr2[AL];
int ix0, iy0; /* origin for pen moves */
double bp[BD];
main()
{
int i, j, i0, i1, i2, i3, i4;
int more = 'r';
char a, b, c;
for (i=0; i<NX; i++) { /* copy to 3-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-1) {i4=0; i3++;};
if (i3>KK-1) {i3=0; i2++;};
if (i2>KK-1) {i2=0; i1++;};
if (i1>KK-1) {i1=0; i0++;};
if (i0>KK-1) {i0=0; };
};};
videopalette(WHCYMAG); /* white/cyan/magenta */
tuto();
while (!kbdst()) rand(); /* wait for keypress */
kbdin(); /* ignore it */
videomode(T80X25);
videoscroll(0,0,CR,71,0,3); /* menu on blue background */
videoscroll(CL+8,0,24,71,0,3);
xtoasc(rand()%NX);
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,CM,0);
break;
case '$': /* dozen totalistics */
j=numin(0);
for (i=0; i<12; i++) {
nutoto(j+i);
totoasc();
ranlin();
evolve();
};
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++) {
ascrule[i0][i1][i2][i3][i4]=trule[i0+i1+i2+i3+i4];
};};};};};
videocursor(0,CM,0);
rmenu();
break;
case 't': /* probability calculation */
edtri();
rmenu();
lmenu();
break;
case 'r': /* edit rule */
xblnk();
edrule();
videocursor(0,CM,0);
rmenu();
break;
case 'l': /* edit cell string */
xblnk();
edline();
videocursor(0,CM,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();
};
videomode(T80X25);
rmenu();
break;
case 'd': /* de Bruijn diagram */
edijn();
rmenu();
lmenu();
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 'w': /* Wolfram rulw # */
i=numin(0);
wmenu(i);
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++) {
ascrule[i0][i1][i2][i3][i4]='0'+i%KK;
i/=KK;
};};};};};
rmenu();
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++) {
if ((KK*(KK*i0+i1)+i2)%4 == 0) i=rand();
ascrule[i0][i1][i2][i3][i4]='0'+i%KK;
i/=KK;
};};};};};
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++) {
ascrule[i4][i3][i2][i1][i0]=ascrule[i0][i1][i2][i3][i4];
};};};};};
break;
case 'B': /* begin barrier */
a=kbdin();
b=kbdin();
ascrule[0][a-'0'][b-'0'][a-'0'][a-'0']=a;
ascrule[1][a-'0'][b-'0'][a-'0'][a-'0']=a;
ascrule[2][a-'0'][b-'0'][a-'0'][a-'0']=a;
rmenu();
break;
case 'E': /* end barrier */
a=kbdin();
b=kbdin();
ascrule[a-'0'][a-'0'][a-'0'][b-'0'][0]=b;
ascrule[a-'0'][a-'0'][a-'0'][b-'0'][1]=b;
ascrule[a-'0'][a-'0'][a-'0'][b-'0'][2]=b;
rmenu();
break;
case 'L': /* left glider link */
a=kbdin();
b=kbdin();
c=kbdin();
ascrule[a-'0'][a-'0'][a-'0'][b-'0'][c-'0']=c;
rmenu();
break;
case 'R': /* left glider link */
a=kbdin();
b=kbdin();
c=kbdin();
ascrule[a-'0'][a-'0'][a-'0'][b-'0'][c-'0']=a;
rmenu();
break;
case 'S': /* still life link */
a=