#include #include #include #include const int naptime = 100000; // How long the program will wait before clearing // the screen and printing a new board. int i,j,di,dj; // The indices. int step=0; // The nth step. int seed; // A seed for the random-number generator. int points=0; const int xsiz=30,ysiz=50; // The size of the board. const int nsteps = 335; // Maximum number of iterations. typedef int gridfunc[xsiz*ysiz]; // A grid is represented by a 1D array. gridfunc b, nb; // The current and next boards. const int INDEX2D(int i,int j) { // For indices i and j, returns the proper index return i*ysiz+j; // of the grid element within the 1D array. } void randb(gridfunc &b) { // Produces a random board. seed = getpid(); // The seed is equal seed = 4570; // to the process ID. srand(seed); // The RNG is seeded. int r; // A random number. for (i=1; i 0 && s==2) // If the element is alive and the sum is 2, nb[cc] = 1; // stay alive. else if (b[cc] >= 0 && s==3) // If the sum is 3, nb[cc] = 1; // remain alive or be born. else if (b[cc] == 1 && s> 3) // If the element is alive and the sum is greater than 3, nb[cc] = 0; // the element dies from overcrowding. else // Otherwise nb[cc] = 0; // the element dies or remains dead. } } for (int i=0;i