Question: I need help creating a Game Of Life program in C. I wrote the following but for some reason, the glide option gets stuck at
I need help creating a Game Of Life program in C. I wrote the following but for some reason, the glide option gets stuck at some point and the random option seems to always end with the same cells rotating at the end. I need help fixing the functions rather than re-writing the entire code. Feel free to edit the contents of the functions in order for them to serve the desired purpose which is to (for the glide option) glide across and (for the random option) to be more "random". However, the functions Field, Glider, Semaphore and Random must not be changed. If you spot any other errors that I should be aware of, I would appreciate you pointing it out and explaining why it's an error.
#include
// Constants, representation of states #define ALIVE 'X' #define DEAD '.' #define FIELDROW 20 #define FIELDCOL 20
// Declaration of data structure typedef struct{ char current; char next; } cell;
void Field(const int rows, const int cols, cell field[rows][cols]); void Glider(const int rows, const int cols, cell field[rows][cols]); void Semaphore(const int rows, const int cols, cell field[rows][cols]); void Random(const int rows, const int cols, cell field[rows][cols]); void loadCustom(const int rows, const int cols, cell field[rows][cols]);
void PrintArea(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]); void SurroundingCells(const int rows, const int cols,int array[rows][cols], cell field[rows][cols]); int getState(int rows, int cols, cell field[rows][cols], int row, int col); void CellStatus(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]); void update(const int rows, const int cols, cell field [rows][cols]); int NumberOfCells(int rows, int cols, cell field[rows][cols], int row, int col);
// Start and run games, interact with the user.* Input: what initial structure and whether to step or exit. Output: Information to the user, and the game field in each step.
int main(void) { cell world[FIELDROW][FIELDCOL]; int neighborhood [FIELDROW][FIELDCOL] = {{0}}; initField(FIELDROW,FIELDCOL, world); PrintArea (FIELDROW, FIELDCOL, neighborhood, world);
printf(" ");
return 0; }
// Initialize all the cells to dead, then asks the user about which structure to load, and finally load the structure. Input: The field array and its size. * Output: The field array is updated.
void initField(const int rows, const int cols, cell field[rows][cols]) {
for (int r = 0 ; r < rows ; r++) { for (int c = 0 ; c < cols ; c++) { field[r][c].current = DEAD; } }
printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom "); printf("or [C]ustom): ");
int ch = getchar(); switch (ch) { case 'g': case 'G': loadGlider(rows, cols, field); break; case 's': case 'S': loadSemaphore(rows, cols, field); break; case 'r': case 'R': loadRandom(rows, cols, field); break; case 'c': case 'C': default: loadCustom(rows, cols, field); break; } if (ch != ' ') { getchar(); } }
// Inserts a glider into the field. input: The field array and its size. output: The field array is updated.
void loadGlider(const int rows, const int cols, cell field[rows][cols]) {
field[0][1].current = ALIVE; field[1][2].current = ALIVE; field[2][0].current = ALIVE; field[2][1].current = ALIVE; field[2][2].current = ALIVE; }
// Inserts a semaphore into the field. input: The field array and its size. output: The field array is updated.
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) {
field[8][1].current = ALIVE; field[8][2].current = ALIVE; field[8][3].current = ALIVE; }
// Inserts a random structure into the field. input: The field array and its size. output: The field array is updated. There is a 50 % chance that a cell is alive.
void loadRandom(const int rows, const int cols, cell field[rows][cols]) { for (int r = 0; r < FIELDROW; r++) { for (int c = 0; c < FIELDCOL; c++) { int temp = rand( ) % 2; if (temp == 0) { field[r][c].current = DEAD; } else { field[r][c].current = ALIVE; } }
} }
// Lets the user specify a structure that then is inserted into the field. input:The field array and its size. output: The field array is updated.
void loadCustom(const int rows, const int cols, cell field[rows][cols]) {
printf("Give custom format string: "); do { int r, c; scanf("%d,%d", &r, &c); field[r][c].current = ALIVE; } while (getchar() != ' '); }
// Prints the field to the screen. input: The field array and its size.
void PrintArea(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]) { char choice; do { for (int rows = 0; rows < FIELDROW; rows++) { for (int cols = 0; cols < FIELDCOL; cols++) { printf("%c ", field[rows][cols].current); } printf(" "); } CellStatus (rows, cols, array, field); printf("Select one of the following options: "); printf(" (enter) Step "); printf(" (any) Exit "); scanf("%c", &choice);
} while (choice == ' '); }
// Counts the number of live cells one cell is surrounded by. input: The field array and its size. output: The neighborCount array. void SurroundingCells(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]) { /*Search for neighbors begins at locCoordj and locCoordj (Local Coordinate) * represents rows and columns where the cell is located.*/ for (int r = 0; r <= rows; r++) { for (int c = 0; c <= cols; c++) { array[r][c] = NumberOfCells(rows, cols, field, r, c); } } }
int NumberOfCells(int rows, int cols, cell field[rows][cols], int row, int col) { int cnt = 0;
for (int r = row - 1; r <= row + 1; r++) { for (int c = col - 1; c <= col + 1; c++) { if ((row != r || col != c) && getState (rows, cols, field, r, c) == ALIVE) { cnt++; } } }
return cnt; }
int getState(int rows, int cols, cell field[rows][cols], int row, int col) { if (row >= 0 && row < FIELDROW && col >= 0 && col < FIELDCOL) { return field[row][col].current; } else { return DEAD; }
}
void CellStatus(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]) { SurroundingCells(FIELDROW, FIELDCOL, array, field); for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (field[r][c].current == ALIVE && array[r][c] == 2) { field[r][c].next = ALIVE; } else if (array[r][c] == 3) { field[r][c].next = ALIVE; } else { field[r][c].next = DEAD; } }
} for (int i = 0; i < FIELDROW; i++) { for (int j = 0; j < FIELDCOL; j++) { printf("%d ", array[i][j]); } printf(" "); } update (rows, cols, field); }
void update(const int rows, const int cols, cell field [rows][cols]) { for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { field[r][c].current = field[r][c].next; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
