/** @file life.c
@brief implements John H. Conway's Game of Life
This C program implements Conway's Game of Life on a torus
@author berndt
@version 1.0
@date 2020-06-04
@copyright 2020 Berndt E. Schwerdtfeger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// -------------------------------------------------------------------
/** @mainpage Implementation details
@section sec_outl Program outline
@subsection subs_init Initialisation
- open the configuration file
- read the configuration into the state matrix
- close the file
- write the initial configuration
@subsection subs_main Main program
Wait for input: number g of generation to process.
- If g=0 terminate
- otherwise: for g generations
- for each cell calculate the number n of neighbours
- for each cell:
- if n==3 set state s=1
- if n!=2 set state s=0
- Write out the map for the user;
@section sec_impl Program implementation
@subsection subs_loop Main loop
@code{.c}
int main(void) {
int s[MI][MK]; // state matrix
int n[MI][MK]; // number of neighbours
FILE *fp = fopen("life.txt","r");
if (fp == NULL) { error handling }
read(fp,s);
fclose(fp);
write(s,t);
while (scanf("%u",&g)) { // read the number of generations to process
if (g == 0) break; // terminate the program
for (h=0; h
#include
#define MI 50 /**< number of rows */
#define MK 80 /**< number of columns */
void read(FILE *, int [MI][MK]);
void write(int [MI][MK], int);
/** @brief main logic of the life program
The main routine takes no parameters and returns EXIT_SUCCESS on normal exit
or EXIT_FAILURE on failure to read the input file (named "life.txt").
The life map is initialized and written to the screen.
The program waits for the number of generations to be processed (integer g).
These number of generations is then calculated and the result displayed.
If g=0 the program ends.
The program keeps a generation counter (variable t for time).
The basic play ground is a grid (map) that keeps the state of each cell
in the area of a torus determined by two coordinates i,k that run from
0 to MI, resp. MK (maximum i, resp. maximum k). The MI resp. MK are the
fundamental periods of the torus.
The variable s (for state) is kept for each cell (i,k) in the array s[i][k].
The number of neighbours is kept in a similar array n[i][k].
The number of neighbours is calculated by
n[i][k] = s[i-1][k-1] + s[i-1][k] + s[i-1][k+1] + s[i][k-1] + s[i][k+1] + s[i+1][k-1] + s[i+1][k] + s[i+1][k+1]
and in dependence of this number the next generation states are
s[i][k] = 1 if ( n[i][k] == 3 )
s[i][k] = 0 if ( n[i][k] != 2 )
Recall that i is taken mod MI and k is taken mod MK.
*/
int main(void) {
int i,k; // row, column
int t = 0; // generation t
int g; // number of generations to calculate
int h; // loop number for generations
int s[MI][MK]; // state matrix
int n[MI][MK]; // number of neighbours
FILE *fp = fopen("life.txt","r");
if (fp == NULL) {
printf("Cannot open %s\n", "life.txt");
exit(EXIT_FAILURE);
}
read(fp,s);
fclose(fp);
write(s,t);
while (scanf("%u",&g)) {
if (g == 0) break;
for (h=0; h