Conway stuff
unknown
c_cpp
4 years ago
8.6 kB
7
Indexable
// File: good-life.c // Description: A program that runs Conway's Game of Life based on user input // // @author Robert Tetreault (rrt2850) // // // // // // // // // // // // // // // // // // // // // // // // // // // /*** Includes and Definitions ***/ #include <stdio.h> #include <stdlib.h> #define SIZE 20 /*** Helpful Functions ***/ /** * getNeighbors(): Copies one 2d array into another * * @param arr1: The first 2d array * @param arr2: The second 2d array * **/ void copyArr( char arr1[][SIZE], char arr2[][SIZE]){ int i, j; for(i = 0; i < SIZE; i++){ for(j = 0; j < SIZE; j++){ arr2[i][j] = arr1[i][j]; } } } /** * getNeighbors(): Gets the total number of neighbors for a point on the 2d array * * @param life: A 2d array representing the current game of life * @param row: The row the point is on * @param col: The column the point is on * @returns the total number of neighbors for the point * **/ int getNeighbors(char life[][SIZE], int row, int col){ int neighbors = 0; if(life[row - 1][col - 1] == '*')++neighbors; if(life[row - 1][col] == '*') ++neighbors; if(life[row - 1][col + 1] == '*') ++neighbors; if(life[row][col - 1] == '*') ++neighbors; if(life[row][col + 1] == '*') ++neighbors; if(life[row + 1][col - 1] == '*') ++neighbors; if(life[row + 1][col] == '*') ++neighbors; if(life[row + 1][col + 1] == '*') ++neighbors; return neighbors; } /** * getNeighborsWeird(): Gets the total number of neighbors for a point on the 2d array * if that point is on one of the edges of the board * * @param life: A 2d array representing the current game of life * @param row: The row the point is on * @param col: The column the point is on * @param num_row: The total number of rows in the grid * @param num_col: The total number of columns in the grid * @returns the total number of neighbors for the point * **/ int getNeighborsWeird(char life[][SIZE], int row, int col, int num_row, int num_col){ int neighbors = 0; if(row == 0 && col == 0){ if(life[row][col + 1] == '*') neighbors++; if(life[row + 1][col + 1] == '*') neighbors++; if(life[row + 1][col] == '*') neighbors++; return neighbors; } if(row == 0 && col < num_col - 1){ if(life[row][col - 1] == '*') neighbors++; if(life[row][col + 1] == '*') neighbors++; if(life[row + 1][col] == '*') neighbors++; if(life[row + 1][col - 1] == '*') neighbors++; if(life[row + 1][col + 1] == '*') neighbors++; return neighbors; } if(row == 0 && col == num_col - 1){ if(life[row][col - 1] == '*') neighbors++; if(life[row + 1][col - 1] == '*') neighbors++; if(life[row + 1][col] == '*') neighbors++; return neighbors; } if(row < num_row - 1 && col == num_col - 1){ if(life[row - 1][col] == '*') neighbors++; if(life[row + 1][col] == '*') neighbors++; if(life[row + 1][col - 1] == '*') neighbors++; if(life[row - 1][col - 1] == '*') neighbors++; if(life[row][col - 1] == '*') neighbors++; return neighbors; } if(row == num_row - 1 && col == num_col - 1){ if(life[row - 1][col] == '*') neighbors++; if(life[row - 1][col - 1] == '*') neighbors++; if(life[row][col - 1] == '*') neighbors++; return neighbors; } if(row == num_row - 1 && col < num_col - 1){ if(life[row - 1][col - 1] == '*') neighbors++; if(life[row - 1][col] == '*') neighbors++; if(life[row - 1][col + 1] == '*') neighbors++; if(life[row][col - 1] == '*') neighbors++; if(life[row][col + 1] == '*') neighbors++; return neighbors; } if(row == num_row - 1 && col == 0){ if(life[row - 1][col] == '*') neighbors++; if(life[row - 1][col + 1] == '*') neighbors++; if(life[row][col + 1] == '*') neighbors++; return neighbors; } if(row < num_row - 1 && col == 0){ if(life[row - 1][col] == '*') neighbors++; if(life[row + 1][col] == '*') neighbors++; if(life[row + 1][col + 1] == '*') neighbors++; if(life[row - 1][col + 1] == '*') neighbors++; if(life[row][col + 1] == '*') neighbors++; return neighbors; } return 0; } /*** Game Modifiers ***/ /** * survivalRule(): Determines whether or not an organism survives the generation * * @param life: A 2d array representing the current game of life * @param num_row: The total number of rows on the board * @param num_col: The total number of columns on the board * **/ void survivalRule(char life[][SIZE], int num_col, int num_row){ int row, col, neighbors; char temp[SIZE][SIZE]; copyArr(life, temp); for(row = 0; row < num_row; row++){ for(col = 0; col < num_col; col++){ if(temp[row][col] == '*'){ if(row == 0 || row == num_row - 1 || col == 0 || col == num_col - 1){ neighbors = getNeighborsWeird(life, row, col, num_row, num_col); } else{ neighbors = getNeighbors(life, row, col); } if(!(neighbors == 2 || neighbors == 3)){ life[row][col] = ' '; } } } } return; } /** * birthRule(): Determines whether or not an organism can be born on the board * * @param life: A 2d array representing the current game of life * @param num_row: The total number of rows on the board * @param num_col: The total number of columns on the board * **/ void birthRule(char life[][SIZE], int num_col, int num_row){ int row, col, neighbors; char temp[SIZE][SIZE]; copyArr(life, temp); for(row = 0; row < num_row; row++){ for(col = 0; col < num_col; col++){ if(temp[row][col] == ' '){ if(row == 0 || row == num_row - 1 || col == 0 || col == num_col - 1){ neighbors = getNeighborsWeird(life, row, col, num_row, num_col); } else{ neighbors = getNeighbors(life, row, col); } if(neighbors == 3){ life[row][col] = '*'; } } } } return; } /*** Game ***/ /** * runGame(): Runs the game based on user input * * @param life: A 2d array representing the current game of life * @param num_gens: The number of generations for the loop to run through * **/ void runGame(char life[][SIZE],int num_gens){ int row, col; int num_col = SIZE - 1; int num_row = SIZE - 1; int curr_gen = 0; while (curr_gen < num_gens) { birthRule(life, num_col, num_row); survivalRule(life, num_col, num_row); for(row = 0; row < SIZE; row++) { for(col = 0; col < SIZE; col++){ printf("%c", life[row][col]); } puts(" "); } printf("\ngeneration: %d\n", curr_gen); curr_gen++; } } /*** Initialization ***/ /** * addOrgs(): Adds organisms to the board randomly * * @param life: A 2d array representing the current game of life * @param num_orgs: The number of organisms to add * **/ void addOrgs(char life[][SIZE], int num_orgs){ int row, col, i; srand( 31 ); for(i = 0; i < num_orgs; i++){ row = rand(); row %= SIZE; col = rand(); col %= SIZE; if(life[row][col] != '*'){ life[row][col] = '*'; } else{ i--; } } } /** * fillSpace(): Fills all empty slots in the array with ' ' * * @param life: A 2d array representing the current game of life * **/ void fillSpace(char life[][SIZE]){ int row, col; for(row = 0; row < SIZE; row++){ for(col = 0; col < SIZE; col++){ if(life[row][col] != '*') life[row][col] = ' '; } } } /** * main(): Initializes everything and prompts for user input * * @returns 0 on success * **/ int main(void){ char life[SIZE][SIZE]; int num_orgs; int num_gens = 100; printf("\n\t..Welcome to the Game of life..\n"); printf("\nPlease enter the initial number of organisms: "); scanf("%i", &num_orgs); //printf("\nPlease enter the number of generations: "); //scanf("%i", &num_gens); addOrgs(life, num_orgs); fillSpace(life); runGame(life, num_gens); return 0; }
Editor is loading...