#include <stdio.h> #include <stdlib.h> #include <math.h> /* This example handles a 12 x 12 mesh, non-MPI version */ //#define maxn 12 #define maxn 8 int const ROWS=12; int const COLS=12; char const LIFE='o'; char const DEATH='.'; struct individual { char condition; int neighbors; } ; void readFile(char filename[], struct individual board[ROWS][COLS]) { int row, col; char eoln; FILE *infile; infile=fopen(filename, "r"); for(row=0; row<ROWS; row++) { for(col=0; col<COLS; col++) { fscanf(infile,"%c", &board[row][col].condition); board[row][col].neighbors=0; } fscanf(infile,"%c", &eoln); } } void printboard(struct individual board[ROWS][COLS], int rows, int cols) { int row, col; printf("\n"); for(row=0; row<rows; row++) { printf("%2d:", row); for(col=0; col<cols; col++) { printf("%c", board[row][col].condition); } printf("\n"); } } void printboardtoFile(FILE* outfile, struct individual board[ROWS][COLS], int rows, int cols) { int row, col; fprintf(outfile, "\n"); for(row=0; row<rows; row++) { for(col=0; col<cols; col++) { fprintf(outfile,"%c", board[row][col].condition); } fprintf(outfile,"\n"); } } void checkneighbors(struct individual board[ROWS][COLS], int rows, int cols) { int row, col; // Check the 1st and last rows, the 1st and last columns // and the internal cells of the matrix (those not on the borders) } void golife(struct individual board[ROWS][COLS], int rows, int cols) { int row, col; int count; for(row=0; row<rows; row++) { for(col=0; col<cols; col++) { checkneighbors(board, rows, cols); //Count the number of neighbors for each cell } } for(row=0; row<rows; row++) { for(col=0; col<cols; col++) { // A dead cell with exactly three live neighbors becomes a live cell (birth). // A live cell with two or three live neighbors stays alive (survival). // In all other cases, a cell dies or remains dead (overcrowding or loneliness). } } } int main(int argc, char **argv) { int rank, size, i, j; struct individual board[ROWS][COLS]; char* filename="board.txt"; char* resultfile="resultlife.txt"; int LIMIT; FILE *outfile; outfile=fopen(resultfile,"w"); printf("How many generations?\n"); scanf("%d", &LIMIT); readFile(filename, board); fprintf(outfile,"%d %d\n", ROWS,COLS); fprintf(outfile,"%d\n", LIMIT); printboard(board, ROWS, COLS); for(i=0;i<LIMIT;i++) { golife(board,ROWS,COLS); // printboardtoFile(outfile, board,ROWS, COLS); printboard(board, ROWS, COLS); } fclose(outfile); return 0; }