Game of Life
-
Yes, here it is: /* * * CS2123 - C Programming - 10:30 MWF * David John Davis * Chapter 5 - Exercise 18 * * Start Date: October 15, 1996 * Last Changed: October 16, 1996 * * John H. Conway (Scientific American, October 1970, p. 120) invented a game called * Life to model the process of birth, survival, and death. The idea is that * organisms require others in order to survive and procreate but that overcrowding * results in death. This program simulates the game Life. It ask the user for an * integer which it uses to seed a random number generator. This random number * generator is used to generate a random first generation in the game. After this * the game continues for the number of generations defined by N. The games follows * these rules: * * A) Birth Rule: An organism is born into any empty cell that has exactly three * living neighbors. * B) Survival Rule: An organism with either two or three neighbors survives from * one generation to the next. * C) Death Rule: An organism with four or more neighbors dies from overcrowding. * An organism with fewer than two neightbors dies from loneliness. * * This program uses a 10x10 array to represent the environment, and each cell can * have a * to represent a life, or be blank. * */ #include #include #define LIFE_GRID 10 /* Defines the width x height of the arrays used. */ #define N 20 /* Defines the number of generations the program should simulate. */ void load_init(char [LIFE_GRID][LIFE_GRID]); /* Function to setup initial generation. */ void step_generation(char [LIFE_GRID][LIFE_GRID]); /* Function to move forward one generation. */ void print_grid(char [LIFE_GRID][LIFE_GRID]); /* Function to print the array. */ int neighbors(char [LIFE_GRID][LIFE_GRID],int,int); /* Function to figure the number of neighbors for a given cell. */ void copy_array(char [LIFE_GRID][LIFE_GRID],char [LIFE_GRID][LIFE_GRID]); /* Function to copy the contents of one array to another. */ void main(void) { char life[LIFE_GRID][LIFE_GRID]; /* Array used for the simulation. */ int i; unsigned int seed; /* Holds the user entered seed for the random number generater. */ /* Get a unsigned int from the user and use it to seed the random number generator. */ printf("\nPlease enter a postive integer: "); scanf("%u",&seed); srand(seed); /* Print a message to the screen and run load_init to setup the initial generation. */ printf("\n\nGenerating inital grid...\n
-
Yes, here it is: /* * * CS2123 - C Programming - 10:30 MWF * David John Davis * Chapter 5 - Exercise 18 * * Start Date: October 15, 1996 * Last Changed: October 16, 1996 * * John H. Conway (Scientific American, October 1970, p. 120) invented a game called * Life to model the process of birth, survival, and death. The idea is that * organisms require others in order to survive and procreate but that overcrowding * results in death. This program simulates the game Life. It ask the user for an * integer which it uses to seed a random number generator. This random number * generator is used to generate a random first generation in the game. After this * the game continues for the number of generations defined by N. The games follows * these rules: * * A) Birth Rule: An organism is born into any empty cell that has exactly three * living neighbors. * B) Survival Rule: An organism with either two or three neighbors survives from * one generation to the next. * C) Death Rule: An organism with four or more neighbors dies from overcrowding. * An organism with fewer than two neightbors dies from loneliness. * * This program uses a 10x10 array to represent the environment, and each cell can * have a * to represent a life, or be blank. * */ #include #include #define LIFE_GRID 10 /* Defines the width x height of the arrays used. */ #define N 20 /* Defines the number of generations the program should simulate. */ void load_init(char [LIFE_GRID][LIFE_GRID]); /* Function to setup initial generation. */ void step_generation(char [LIFE_GRID][LIFE_GRID]); /* Function to move forward one generation. */ void print_grid(char [LIFE_GRID][LIFE_GRID]); /* Function to print the array. */ int neighbors(char [LIFE_GRID][LIFE_GRID],int,int); /* Function to figure the number of neighbors for a given cell. */ void copy_array(char [LIFE_GRID][LIFE_GRID],char [LIFE_GRID][LIFE_GRID]); /* Function to copy the contents of one array to another. */ void main(void) { char life[LIFE_GRID][LIFE_GRID]; /* Array used for the simulation. */ int i; unsigned int seed; /* Holds the user entered seed for the random number generater. */ /* Get a unsigned int from the user and use it to seed the random number generator. */ printf("\nPlease enter a postive integer: "); scanf("%u",&seed); srand(seed); /* Print a message to the screen and run load_init to setup the initial generation. */ printf("\n\nGenerating inital grid...\n
-
Although it's C not C++, the best life implementation I've seen is still XLife: http://www.cs.jhu.edu/~callahan/lifepage.html Which uses a much better algorithym than just making an n by x array, making it run 100s of times faster for big patterns like "breeder". I've got a Windows version (sort of) working based on a half-finished X11 implementation I wrote some time back - email me if you're interested
-
Unsure but maybe this helps http://www.geocities.com/simesgreen/gllife/index.html regardz Colin Davie