I always thought it was kind of cool, way back when. It's still cool, actually.
Want one for your Altair or your Altair Clone? A faster one than the BASIC version?
So grab a copy of BDS C, copy the source file below, save it as "clife.c" on your BDS disk and compile it using these commands:
cc clife
clink clife
It'll make the CLIFE.COM executable.
Hope it's fun for you! Happy 4th of July weekend!
- Code: Select all
/* cLife.c */
/* Conway's Life Simulation */
/* Wayne Parham */
#include <stdio.h>
#ifndef XMAX
#define XMAX 80
#endif
#ifndef YMAX
#define YMAX 22
#endif
#ifndef LINES
#define LINES 24
#endif
#ifndef IN_MAX
#define IN_MAX 3
#endif
int i,j;
int life;
int cycle;
int density;
int iteration;
int generations;
char array1[XMAX][YMAX];
char array2[XMAX][YMAX];
void cls() {
for( j=0; j<LINES; j++ ) {
putchar( '\n' );
}
}
int getnum() {
char c;
int i;
char input[IN_MAX];
int value;
for( i=0; i < IN_MAX && c != '\n' && c != '\r'; i++ ) {
c = getchar();
input[i] = c;
}
value = atoi( input );
return( value );
}
void seed() {
int n;
srand(n);
for( j=0; j<YMAX; j++ ) {
for( i=0; i<XMAX; i++ ) {
life = ( rand() % 100 );
if( life < density )
array1[i][j]='o';
else
array1[i][j]=' ';
}
}
}
void applyRule( world, alive, x, y ) {
int neighbors;
neighbors = 0;
if( world == 1 ) {
if( array1[x-1][y] =='o') neighbors++;
if( array1[x+1][y] =='o') neighbors++;
if( array1[x-1][y+1] =='o') neighbors++;
if( array1[x][y+1] =='o') neighbors++;
if( array1[x+1][y+1] =='o') neighbors++;
if( array1[x-1][y-1] =='o') neighbors++;
if( array1[x][y-1] =='o') neighbors++;
if( array1[x+1][y-1] =='o') neighbors++;
if( (alive==1) && (neighbors<2) ) array2[x][y]=' ';
else if( (alive==1) && (neighbors>3) ) array2[x][y]=' ';
else if( (alive==1) && ((neighbors==2) || (neighbors==3)) ) array2[x][y]='o';
else if( (alive==0) && (neighbors==3) ) array2[x][y]='o';
else array2[x][y]=' ';
}
else if( world == 2 ) {
if( array2[x-1][y] =='o') neighbors++;
if( array2[x+1][y] =='o') neighbors++;
if( array2[x-1][y+1] =='o') neighbors++;
if( array2[x][y+1] =='o') neighbors++;
if( array2[x+1][y+1] =='o') neighbors++;
if( array2[x-1][y-1] =='o') neighbors++;
if( array2[x][y-1] =='o') neighbors++;
if( array2[x+1][y-1] =='o') neighbors++;
if( (alive==1) && (neighbors<2) ) array1[x][y]=' ';
else if( (alive==1) && (neighbors>3) ) array1[x][y]=' ';
else if( (alive==1) && ((neighbors==2) || (neighbors==3)) ) array1[x][y]='o';
else if( (alive==0) && (neighbors==3) ) array1[x][y]='o';
else array1[x][y]=' ';
}
}
void generate( world ){
int alive;
if( world == 1 ) {
for( j=0; j<YMAX; j++ ) {
for( i=0; i<XMAX; i++ ) {
if( array2[i][j]=='o' ) {
alive = 1;
} else {
alive = 0;
}
applyRule(2,alive,i,j);
}
}
}
else if( world == 2 ) {
for( j=0; j<YMAX; j++ ) {
for( i=0; i<XMAX; i++ ) {
if( array1[i][j]=='o' ) {
alive = 1;
} else {
alive = 0;
}
applyRule(1,alive,i,j);
}
}
}
}
void display( array ) {
if( array == 1 ) {
for( j=0; j<YMAX; j++ ) {
for( i=0; i<XMAX; i++ ) {
printf("%c", array1[i][j]);
if( i==XMAX-1 )
printf( "\n" );
}
}
}
else if( array == 2 ) {
for( j=0; j<YMAX; j++ ) {
for( i=0; i<XMAX; i++ ) {
printf("%c", array2[i][j]);
if( i==XMAX-1 )
printf( "\n" );
}
}
}
printf( " Generation %d\n", iteration );
}
int main() {
printf( "\n Life\n" );
printf( "\nThis is a cellular automaton devised by the British mathematician John Conway" );
printf( "\nin 1970. Its evolution is determined by its initial state, requiring no" );
printf( "\nfurther input. You will only be asked for initial population density, which is" );
printf( "\nthen used to create a random distribution of cells.\n" );
printf( "\nThis simulation is Turing complete and can produce and contain a universal" );
printf( "\nconstructor or any other Turing machine.\n" );
printf( "\nThe universe is a two-dimensional orthogonal grid, with each position in one of" );
printf( "\ntwo possible states, live or dead, or populated and unpopulated, respectively.\n" );
printf( "\nEvery cell interacts with its eight neighbours, which are the cells that are" );
printf( "\nhorizontally, vertically, or diagonally adjacent. The following transitions" );
printf( "\noccur at each iteration, definining a generation:\n" );
printf( "\n- A live cell with fewer than two live neighbours dies from underpopulation." );
printf( "\n- A live cell with two or three live neighbours lives to the next generation." );
printf( "\n- A live cell with more than three live neighbours dies from overpopulation." );
printf( "\n- A space surrounded by three cells becomes a live cell from reproduction.\n" );
printf( "\nStarting population density as a percentage? " );
density = getnum();
if( (density < 1) || (density > 100) ) {
puts( "\nWhat? Density must be between 1% and 99%. Setting to 33%." );
density = 33;
}
printf( "\nNumber of generations (0 to loop forever) ? " );
generations = getnum();
if( generations < 0 )
generations = 0;
seed();
while( (generations == 0) || (generations > iteration) ) {
cls();
iteration++;
if( cycle ) {
display(2);
generate(1);
cycle = 0;
} else {
display(1);
generate(2);
cycle = 1;
}
}
return(0);
}