//Function that initializes grid. Takes # of rows, # of columns and int array as arguments. void initGrid(int rows, int cols, int g[rows][cols]) { int i, j, k; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { /* This is a common grid because it allows you to ignore accounting for corners, or going out of array bounds. [-1] [-1] [-1] [-1] [-1] [-1] [ ] [ ] [ ] [-1] [-1] [ ] [ ] [ ] [-1] [-1] [-1] [-1] [-1] [-1] */ if (i == 0 || i == (rows - 1) || j == 0 || j == (cols - 1)) //Sets first and last rows/cols to -1 to create container. g[i][j] = -1; else //Else will target inner cells (playable area). { k = rand() % 3; //One in three chance that the cell will be valued. Random number from 0 to 2. if(k == 0) { g[i][j] = 1; //Fill cell with 1, which represents a person. population++; //Add one to our population count } else g[i][j] = 0; } } } processGeneration(rows, cols, g); }
int main() { srand((unsigned int) time(NULL)); int grid[ROWS][COLS]; initGrid(ROWS, COLS, grid); printGrid(ROWS, COLS, grid); int i, g; g = getUserInput(); for(i = 0; i < g; i++) { generation++; processGeneration(ROWS, COLS, grid); printGrid(ROWS, COLS, grid); sleep(100); } return 0; }
int main(int argc, char const *argv[]) { srand((unsigned int) time(NULL)); //Takes the current time and uses it as a seed for the random number generator int grid[ROWS][COLS]; //Make grid initGrid(ROWS, COLS, grid); //Initialize grid printGrid(ROWS, COLS, grid); //Print grid to screen int i, g; g = getUserInput(); for (i = 0; i < g; i++) { generation++; processGeneration(ROWS, COLS, grid); printGrid(ROWS, COLS, grid); sleep(100); } return 0; }
// *************************************** void initGrid(int rows, int cols, int g[rows][cols]) { int i, j, k; for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { if(i == 0 || i == (rows - 1) || j == 0 || j == (cols - 1)) g[i][j] = -1; else { k = rand() % 3; if(k == 0) { g[i][j] = 1; population++; } else g[i][j] = 0; } } } processGeneration(rows, cols, g); }