home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 8 Other
/
08-Other.zip
/
TEXTCA.ZIP
/
TEXTCA2.DOC
next >
Wrap
Text File
|
1991-02-02
|
2KB
|
41 lines
The code below is the C source code fragment for an artificial life program that
has been floating around recently. It is of interest primarily to those (like myself)
who see a connection between the way both natural life and digital processing
can bring apparant order from randomness.
Textca2.exe is the OS/2 binary. It is a mutli-threaded program which uses
this code fragment as the main routine, and sets up a thread that polls
the keyboard in the background. Pressing the ESC or "q" keys exits the
program; any other ASCII key resets the rule table. The output shows how
the cells reproduce themselves in a life-like way.
main()
{
int ruletable[8] = {0,1,1,1,1,0,0,0}; /* This is changeable. */
int i, nabecode, key, exitflag = 0;
int oldbuff[80], newbuff[80];
for (i=0; i < 80; i++) /* Empty out the buffers. */
{
oldbuff[i] = 0;
newbuff[i] = 0;
}
oldbuff[40] = 1; /* Turn one cell on */
while (!exitflag)
{
for (i=1; i<79; i++) /* Each cell looks to left and right */
{ /* Don't update the first or last cell. */
nabecode = 4 * oldbuff[i-1] + 2 * oldbuff[i] + oldbuff[i+1];
newbuff[i] = ruletable[nabecode]; /* 0 <= nabecode <= 8 */
}
for (i=0; i<80; i++) /* We show all 80 cells, though we only */
{ /* update the center 78 cells. */
if (newbuff[i] == 1)
printf("%c",219); /* 219 is ASCII number of a block */
else printf(".");
oldbuff[i] = newbuff[i];
}
}
}