#include iolib.h
#include float.h	/* Floating point library with random numbers */
#include printf1.h	/* PRINTF library */

#define take 35		/* Cell ASCII value */
#define clear 32	/* Space ASCII value */
#define xmax 80		/* Max x cols */
#define ymax 20		/* Max y rows */
#define part 3		/* Number of neighbours to survive */ 

int cell[2000];
int x,y,count,gen;

/***********************************************/
/* LIFE this verison  by KEVIN GROVES (C) 1989 */
/*                                             */
/* The compiler used it Small-C.               */
/* for other compilers, remove functions       */
/* that are include with it.                   */
/***********************************************/ 

main()
{
blank();	/* Blank cell array first */
firstgen();	/* create first generation */
cls();		/* Clear screen */
life();		/* Create life..... */
}

life()
{
gen=1;

while(1)	/* Loop for ever */
	{
	home();
	x=1; y=1;	/* Check each cell */
	while((y++)<ymax)
		{
		x=1;
		while((x++)<xmax)
			{
			count=0;	/* Count neighbours */
			if(cell[at(x,y)]==take)
				count++;
			if(cell[at(x-1,y)]==take)
				count++;
			if(cell[at(x-1,y+1)]==take)
				count++;
			if(cell[at(x,y-1)]==take)
				count++;
			if(cell[at(x,y+1)]==take)
				count++;
			if(cell[at(x+1,y-1)]==take)
				count++;
			if(cell[at(x+1,y)]==take)
				count++;
			if(cell[at(x+1,y+1)]==take)
				count++;
			if(count==part)
				cell[at(x,y)]=take;
			else
				cell[at(x,y)]=clear;

			putchar(cell[at(x,y)]);	/* Display cell */
			}
		putchar(13);
		}
	printf("Generation %d with survial of %d partners",gen,part);
	gen++;	/* Next generation */
	}
}


home()
{
putchar(27);
puts("H");
}

cls()
{
putchar(27);
puts("E");
}

at(x1,y1)
int x1,y1;
{
return(((y1-1)*xmax)+x1);	/* Calc cell pos in single array */
}

blank()
{
count=0;
while((count++)<(xmax*ymax)+2)
	cell[count]=clear;	/* Clear array */
}

firstgen()
{
count=0;	/* Random cell position */
while((count++)<(xmax*ymax)+2)
	if(rand()>.45) cell[count]=take; else cell[count]=clear;
}
