home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
245_01
/
lca42.c
< prev
next >
Wrap
Text File
|
1987-10-28
|
14KB
|
435 lines
/* (4,2) 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 - Adapted for second neighbors [HVM] */
/* 25 April 1987 - Collection of sample rules [HVM] */
/* 26 April 1987 - Multiple menus [HVM] */
/* 28 April 1987 - version for XVI Feria de Puebla [HVM] */
# 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 DS 16 /* number of distinct sums */
# define NX 91 /* number of sample rules */
char xrule[] =
"0000012233331111" /* interesting background */
"0000021232311000" /* nice gliders & 2-sided cycles */
"0000112232211000" /* class iv stills */
"0000121232310000" /* cycles */
"0000122313221000" /* small black, big green blotches */
"0001310000012100" /* class iv w/zigzag */
"0010321000321010" /* glider class iv dies */
"0010032000132011" /* binary fission */
"0010102010332233" /* slow red glider */
"0010132000231011" /* */
"0010230200100001" /* small class iv periodic */
"0012030200100220" /* localized */
"0010132000132011" /* interesting binary fission */
"0013331303033023" /* very delicate - blue background */
"0023111031322011" /* dies quickly class iv */
"0023323310033133" /* more regular but delicate */
"0100002323111111" /* red automaton various barriers */
"0100002232311111" /* barriers and red (2,1) */
"0100010323212121" /* interesting mixed */
"0100021121101101" /* natural barriers */
"0100021123301101" /* strong & fragile barriers */
"0100320333033113" /* good vs evil */
"0102220002123003" /* noteworthy */
"0110001323211113" /* cycles */
"0110013230203233" /* blue vs red - striking */
"0110031111222333" /* blue-green split in red foreg. */
"0120330111101331" /* also delicate */
"0010321000321010" /* long period reflector - glider */
"0020013111222033" /* glider on red background */
"0020221113030110" /* a new style */
"0100002323111001" /* red systeb between barriers */
"0100002323211121" /* more colorful */
"0100012222233300" /* green and red */
"0100121233320000" /* blue triangles w/red */
"0100122233331111" /* crazy stripes */
"0100211001022220" /* stills & gliders, no blue */
"0100311022223330" /* neat, try variations */
"0110012222233331" /* green and red compete */
"0110122033301111" /* even crazier stripes */
"0111003221022222" /* rec w/other colors struggling */
"0111003222102333" /* variant w/blue cores /*
"0133333112111220" /* bears looking at again */
"0200010323003133" /* conflict of blue vs black */
"0200012303223303" /* black vs blue */
"0200001323211112" /* gliders and cycles */
"0200122102233233" /* unstable */
"0200110123123010" /* class iv w/red background */
"0201212113312303" /* delicate */
"0210012222223331" /* big green and red compete */
"0220010323203233" /* blue background w/cycl & glidr */
"0230013022200303" /* class iv on green */
"0230013022200313" /* less prolific & dies out */
"0232020000100012" /* green dominated by blue */
"0200211301023220" /* black spots */
"0310020330100001" /* interesting zigzag */
"0310020101103102" /* */
"0310020101103122" /* jungle pyramids */
"0310102020230000" /* still on green */
"0313320330100001" /* */
"0310020101103122" /* jungle pyramids */
"0100211001022220" /* stills and gliders */
"1002033112212122" /* cycles on green background; slow glider */
"1200012222233000" /* dying gliders & glider generator */
"1202000213121123" /* gliders on striped background */
"1210012222233000" /* also interesting - dying gliders */
"1211133030313323" /* glider on blue */
"1222330131213111" /* another delicate */
"1222330131213113" /* similar but jitters */
"2000013111213333" /* blue glider on black */
"2013300233223300" /* so-so cycles */
"2110013112033331" /* wedge diminishes to glider */
"2120320203223021" /* glider & local */
"2120320203223023" /* glider and local variant */
"2200013112033331" /* variant */
"2213331303033023" /* delicate but more prolific */
"2222003031000213" /* green and black strips & stills */
"2222003031000210" /* has a bouncing glider */
"2300013111213330" /* macroscopic glider */
"2313113000001333" /* vivid */
"2322022110002201" /* stripey class iv */
"3003003300123120" /* class iv blue/black monochrome */
"3030111032310222" /* class iv cycles - dies quickly */
"3031300300330012" /* some triangle conflicts */
"3110220211131302" /* chinese lantern */
"3100002022011123" /* striking reiangles in matrix */
"3120202022011123" /* blue triangles in green matrix */
"3122320211331223" /* pleasant */
"3223212330100010" /* be patient - it regularizes */
"3312020131133323" /* interesting barrier conflict */
"3320001132030011" /* 3 color cycle w/glider */
"2332212222200001" /* triangles */
;
char xx[4], rule[DS+1];
int arule[DS], arr1[AL], arr2[AL];
main()
{
int c, i, jj, n;
int more = 'r';
videopalette(WHCYMAG); /* white/cyan/magenta */
tuto();
while (!kbdst()) jj=rand(); /* wait for keypress */
c=kbdin(); /* ignore it */
jj=rand()%NX;
for (i=0; i<DS; i++) rule[i] = xrule[DS*jj+i]; /* random sample rule */
rule[DS]=0;
for (i=0; i<AL; i++) { /* random initial array */
if (i%4 == 0) c=rand();
arr1[i]=c&3; c>>=2;};
videomode(T80X25);
videoscroll(3,0,4,71,0,3); /* menu on blue background */
videoscroll(16,0,21,71,0,3);
xmenu(jj+1); /* show initial rule */
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 'r': /* edit rule */
xblnk();
edrule();
videocursor(0,3,0);
rmenu();
break;
case 'l': /* edit cell string */
xblnk();
edline();
videocursor(0,3,0);
lmenu();
break;
case '#': /* read stored rule */
xmenu(NX);
n=DS*((i=lim(1,numin(0),NX))-1);
xmenu(i);
for (i=0; i<DS; i++) rule[i] = xrule[n+i];
rmenu();
break;
case 'u':
xblnk();
for (i=0; i<AL; i++) arr1[i]=0;
arr1[AL/4]=1;
arr1[AL/2]=2;
arr1[(3*AL)/4]=3;
lmenu();
break;
case 'x': /* random rule */
xblnk();
for (i=0; i<DS; i++) {
if (i%4 == 0) c = rand();
rule[i] = '0'+(c & 3);
c >>= 2;
};
rmenu();
break;
case 'y': /* random line */
xblnk();
for (i=0; i<AL; i++) {
if (i%4 == 0) c = rand();
arr1[i] = c & 3;
c >>= 2;
};
lmenu();
break;
default: break;
};
};
if (more=='n') break;
do {
evolve(rule);
videocursor(0,0,0);
scrstr("More?");
videocursor(0,0,30);
scrstr("y/n/cr");
more=kbdin();
} while (more=='\015');
videomode(T80X25); /* reset the screen */
if (more=='n') break;
};
}
edrule() /* edit the rule */
{
char c;
int i;
videocursor(0,1,6); /* get the rule */
i=0;
while (i<DS) {
videoputc(rule[i],2);
videoputc('\010',1);
c = kbdin();
if (c == '\015') break;
switch (c) {
case '0': case '1': case '2': case '3': /* state */
rule[i++] = c;
videoputc(c,1);
break;
c