home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
275_01
/
lcau31.c
< prev
Wrap
Text File
|
1980-01-01
|
24KB
|
833 lines
/* lcau31.c */
/* (3,1) 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] */
/* 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 - PROB41.C for option 't' [HVM] */
/* 21 October 1987 - program disks disappeared */
/* 24 December 1987 - program reconstructed from listings */
/* 20 February 1988 - RIJN31.C for option 'd' [HVM] */
/* 10 April 1988 - include rule engineering from LCA41 [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 /* color quad for alternative screen */
# define TRR 16 /* row showing totalistic rule number */
# define TRC 56 /* column for totalistic rule number */
# define XRR 12 /* row displaying totalistic rule */
# define XRC 56 /* column for totalistic rule */
# define AL 320 /* array length (screen width) */
# define SL 40 /* short array length */
# define TS 7 /* distinct sums w/totalistic rule */
# define DS 27 /* (number of distinct neighborhoods) */
# define KK 3 /* number of states per cell */
# define NX 19 /* number of sample rules */
# define BD 11 /* maximum degree of b.p. */
char xrule[NX][KK][KK][KK];
char ixrule[NX][DS]=
"000111011022211211212212121", /* nice cross hatching */
"000111011022211211010101011", /* very complex glider */
"001011121112111102122010110", /* v. bars w/entanglement */
"001212111222012021211012110", /* cycles on dgl bgrnd */
"002010000121000021210121012", /* crosshatching */
"002010110011102200201210000", /* bin ctr */
"001212111222012021211012110", /* cycles on dgl bgrnd */
"010000022020011001000000012", /* shuttle squeeze */
"010002001022011100022000102", /* coo gldrs */
"012101100200011111020112002", /* crocodile skin */
"012101211210021101110111010", /* mixture of types */
"012101210121012101210221111", /* y/o on b/g */
"020100121221001000010020101", /* slow glider - copies bar */
"020101102122002001220120212", /* slow glider */
"020102221122012100201010221", /* slow & fast gliders */
"021201121221011200101210111",
"010010101111011120221112000", /* diagonal growth on mesh */
"101010202020202010101111111", /* skewed triangle */
"212112112111211112121211112" /* gliderettes & latice */
;
char tabl[20][SL]; /* workspace to construct table */
char ascrule[KK][KK][KK]; /* ASCII transition values */
char auxrule[KK][KK][KK]; /* auxiliary transition table */
char rulstk[10][KK][KK][KK]; /* pushdown for rules */
char prule1[8], prule2[8]; /* product rule specification */
char trule[TS]="0130232";
int binrule[KK][KK][KK];
int arr1[AL], arr2[AL]; /* line of cells */
int ru, ru0, ru1, ru2; /* rule cursor */
int li, lj; /* cell pointer */
int rulptr; /* rule pd ptr */
int ix0, iy0; /* origin for pen moves */
double wmul[KK], wmvl[KK]; /* left mass point */
double wmur[KK], wmvr[KK]; /* right mass point */
double bp[KK][KK][KK][KK]; /* bernstein polynomial */
main() {
int i, j, i0, i1, i2;
int more = 'r';
char a, b, c;
ru=6; ru0=0; ru1=0; ru2=0;
li=SL/2; lj=0;
rulptr=0;
for (i=0; i<NX; i++) { /* copy to 3-index array */
i0=0; i1=0; i2=0;
for (j=0; j<DS; j++) {
xrule[i][i0][i1][i2]=ixrule[i][j];
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);
ranlin(); /* random initial array */
auxblnk(); /* uncomitted transitions */
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();
};
videomode(T80X25);
rmenu();
lmenu();
break;
case '.': /* one cycle of evolution */
asctobin();
onegen(AL);
clmenu();
break;
case ',': /* one cycle of evolution */
videomode(COLGRAF);
pgrid();
for (i=0; i<200; i++) {pprob(); onegen(AL);};
videodot(190,195,3);
kbdin();
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++) {
ascrule[i0][i1][i2]=trule[i0+i1+i2];
};};};
videocursor(0,4,0);
rmenu();
xmenu(totonu(0));
break;
case 't': /* edit tri prob plots */
edtri();
rmenu();
lmenu();
break;
case 'r': /* edit rule */
xblnk();
edrule();
videocursor(0,4,0);
rmenu();
break;
case 'l': /* edit cell string */
xblnk();
edline(8,40);
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();
};
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 'X': /* random rule */
xblnk();
i=rand();
for (i0=0; i0<KK; i0++) {
for (i1=0; i1<KK; i1++) {
for (i2=0; i2<KK; i2++) {
if (i == 0) i=rand();
ascrule[i0][i1][i2]='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++) {
if (