/* Fills the structure with the flow in iterative way (keep propagating the flow using 4 diagonal-direction sweeps until no cell has been updated updated on the previous step) */ void flow_iterative(grid *gd) { int i, j; int update_flag = 1; while (update_flag) { update_flag = 0; /* top left -> bottom right */ for (i = 0; i < gd->height; i++) for (j = 0; j < gd->width; j++) if (gd->cells[i][j] == SITE_OPEN) update_flag += check_neighbors(gd, i, j); /* top right -> bottom left */ for (i = 0; i < gd->height; i++) for (j = gd->width - 1; j >= 0; j--) if (gd->cells[i][j] == SITE_OPEN) update_flag += check_neighbors(gd, i, j); /* bottom left -> top right */ for (i = gd->height - 1; i >= 0; i--) for (j = 0; j < gd->width; j++) if (gd->cells[i][j] == SITE_OPEN) update_flag += check_neighbors(gd, i, j); /* bottom right -> top left */ for (i = gd->height - 1; i >= 0; i--) for (j = gd->width - 1; j >= 0; j--) if (gd->cells[i][j] == SITE_OPEN) update_flag += check_neighbors(gd, i, j); } }
int main (int argc, char** argv) { char buf[20]; int m = 0; int n = 0; fgets(buf, 20, stdin); printf("%s\n", buf); m = strlen(buf) - 1; int sets = 2; int i = 0; n = m; char** array = (char**) calloc( n , sizeof(char*)); char* array_row; while(strlen(buf) - 1 == m){ array_row = (char*) calloc(m, sizeof(char)); strncpy(array_row, buf, n); array[i++] = array_row; fgets(buf, 20, stdin); } int j = 0; for(i =0; i< n; i++) { for(j =0; j<n; j++) { check_neighbors(array, &sets, j, i, n, n); } } printf("Number of disjoint blobs of zeroes %d\n", sets-2); }
void player_choice(char current_board[BOARD_SIZE][BOARD_SIZE], char future_board[BOARD_SIZE][BOARD_SIZE], char player) //function used to interpret the player's choice { int row = 0; int column = 0; int i = 0; int j = 0; switch(player) { case 'a': //adding a cell case 'A': printf("Inserting a cell.\n"); printf("Enter a row (any positive int < 40): "); scanf("%d", &row); if((row < 0) || (row > 40)) { printf("Invalid input.\n"); return; } printf("Enter a column (any positive int < 40): "); scanf("%d", &column); if((column < 0) || (column > 40)) { printf("Invalid input.\n"); return; } add_cell(future_board, row, column); //all edits are made to the future board change_board(current_board, future_board); //the boards a switched (current board is updated) printf("\033[2J\033[H"); print_board(current_board); //current board is printed break; case 'r': //removing a cell case 'R': printf("Removing a cell.\n"); printf("Enter a row (any positive int < 40): "); scanf("%d", &row); if((row < 0) || (row > 40)) { printf("Invalid input.\n"); return; } printf("Enter a column (any positive int < 40): "); scanf("%d", &column); if((column < 0) || (column > 40)) { printf("Invalid input.\n"); return; } delete_cell(future_board, row, column); change_board(current_board, future_board); printf("\033[2J\033[H"); print_board(current_board); break; case 'n': //applying the rules of the game case 'N': for(i = 0; i < BOARD_SIZE; i++) //the functions will read the current board but will apply the changes to the future board { for(j = 0; j < BOARD_SIZE; j++) { life_or_death(current_board, future_board, i, j, check_neighbors(current_board, i, j)); } } change_board(current_board, future_board); //boards are switched printf("\033[2J\033[H"); print_board(current_board); break; default: return; break; } return; }