/* * C program for Conway's ``game of life''. * * Input is from command line and standard input: * * Initial board configuration is read from standard input: * N (size of board) and N*N values (each 0 or 1) * * Number of steps is read from command line. */ #include #include #define cell_index(r, c, n) ((r)*((n)+2) + (c)) #define DEBUG 0 /* * Data structure for board: * "size" is the size of the (square) board. * "cells" and "new_cells" are pointers to 1D arrays to hold two board * configurations (the current one and the one being computed for the next * time step). Each array is of size (size+2)*(size+2), to allow for a * one-cell-wide border of always-dead cells. (This simplifies the coding * a bit.) The 2D board configuration is stored in this 1D array in * row-major order. */ typedef struct { int size; int* cells; int* new_cells; } board_t; int read_board(FILE* infile, board_t *board); void update_board(board_t *board); void print_board(FILE* outfile, board_t *board); int main(int argc, char* argv[]) { int steps, i; board_t board; if (argc < 2) { fprintf(stderr, "usage: %s number_of_time_steps\n", argv[0]); return EXIT_FAILURE; } steps = atoi(argv[1]); if (read_board(stdin, &board) != 0) return EXIT_FAILURE; fprintf(stdout, "Initial board\n\n"); print_board(stdout, &board); fprintf(stdout, "\n\n"); for (i = 0; i < steps; ++i) { update_board(&board); fprintf(stdout, "Board after step %d\n\n", i); print_board(stdout, &board); fprintf(stdout, "\n\n"); } return EXIT_SUCCESS; } void clear_border(int board_size, int *cells) { int i, j; for (j = 0; j <= (board_size)+1; ++j) { cells[cell_index(0, j, board_size)] = 0; cells[cell_index((board_size)+1, j, board_size)] = 0; } for (i = 0; i <= (board_size)+1; ++i) { cells[cell_index(i, 0, board_size)] = 0; cells[cell_index(i, (board_size)+1, board_size)] = 0; } } int read_board(FILE* infile, board_t *board) { int i, j, temp; if (fscanf(infile, "%d", &(board->size)) != 1) { fprintf(stderr, "unable to read size of board\n"); return 1; } board->cells = malloc(((board->size)+2)*((board->size)+2)*sizeof(int)); board->new_cells = malloc(((board->size)+2)*((board->size)+2)*sizeof(int)); if ((board->cells == NULL) || (board->new_cells == NULL)) { fprintf(stderr, "unable to allocate space for board of size %d\n", (board->size)); return 2; } for (i = 1; i <= board->size; ++i) { for (j = 1; j <= board->size; ++j) { if (fscanf(infile, "%d", &temp) != 1) { fprintf(stderr, "unable to read values for board\n"); return 1; } if ((temp == 0) || (temp == 1)) board->cells[cell_index(i, j, board->size)] = temp; else { fprintf(stderr, "unable to read values for board\n"); return 1; } } } clear_border(board->size, board->cells); return 0; } void update_board(board_t *board) { /* YOUR CODE GOES HERE */ } void print_board(FILE* outfile, board_t *board) { int i, j; for (i = 1; i <= board->size; ++i) { for (j = 1; j <= board->size; ++j) { if (board->cells[cell_index(i, j, board->size)] == 0) fprintf(outfile, ". "); else fprintf(outfile, "1 "); } fprintf(outfile, "\n"); } }