void GCMRegridder_add_sheet(GCMRegridder *cself, std::string name, std::string const &gridI_fname, std::string const &gridI_vname, std::string const &exgrid_fname, std::string const &exgrid_vname, std::string const &sinterp_style, PyObject *elevI_py, PyObject *maskI_py) { NcIO ncio_I(gridI_fname, netCDF::NcFile::read); std::unique_ptr<Grid> gridI(new_grid(ncio_I, "grid")); gridI->ncio(ncio_I, "grid"); ncio_I.close(); NcIO ncio_exgrid(exgrid_fname, netCDF::NcFile::read); std::unique_ptr<Grid> exgrid(new_grid(ncio_exgrid, exgrid_vname)); exgrid->ncio(ncio_exgrid, exgrid_vname); ncio_exgrid.close(); auto interp_style(parse_enum<InterpStyle>(sinterp_style)); auto elevI(np_to_blitz<double,1>(elevI_py, "elevI", {gridI->ndata()})); auto maskI(np_to_blitz<int,1>(maskI_py, "maskI", {gridI->ndata()})); SparseVector elevI_sp({elevI.extent(0)}); for (int i=0; i<elevI.extent(0); ++i) if (!maskI(i)) elevI_sp.add({i}, elevI(i)); auto sheet(new_ice_regridder(gridI->parameterization)); sheet->init(name, std::move(gridI), std::move(exgrid), interp_style, std::move(elevI_sp)); cself->add_sheet(std::move(sheet)); }
Grid* load_file(char *path) { FILE *fp; int width, height; char buf[MAX]; Grid *grid; int i, j; char tile; if (!(fp = fopen(path, "r"))) { print_error_file(path); return NULL; } fgets(buf, MAX, fp); sscanf(buf, "%d %d", &width, &height); grid = new_grid(width, height); for (i=0; fgets(buf, MAX, fp) && i < height; i++) { for (j=0; j < width && ((tile=buf[j]) != '\n'); j++) grid->tiles[i][j] = tile; } fclose(fp); return grid; }
void GCMRegridder_init(GCMRegridder *cself, std::string const &gridA_fname, std::string const &gridA_vname, std::vector<double> &hpdefs, bool _correctA) { // Read gridA NcIO ncio(gridA_fname, netCDF::NcFile::read); std::unique_ptr<Grid> gridA = new_grid(ncio, gridA_vname); gridA->ncio(ncio, gridA_vname); ncio.close(); // Construct a domain to be the full extent of indexing. int rank = gridA->indexing.base.size(); std::vector<int> high(rank); for (int k=0; k<rank; ++k) high[k] = gridA->indexing.base[k] + gridA->indexing.extent[k]; // Put it together long nhp = hpdefs.size(); cself->init( std::move(gridA), std::move(hpdefs), Indexing<long,long>({0,0}, {gridA->ndata(), nhp}, {1,0}), _correctA); }
Grid* load_grid_from_ppm(char *path, unsigned int max_width, unsigned int max_height) { unsigned int i, j; Image *img = load_ppm(path); Grid *grid; if (img == NULL) { print_error("Échec de chargement de la grille."); return NULL; } grid = new_grid(min(max_width, img->width), min(max_height, img->height)); for (i=0; i < grid->width; i++) for (j=0; j <grid->height; j++) { if (img->pixels[i][j].grey < 255) // le pixel n'est pas blanc grid->tiles[i][j] = '+'; else grid->tiles[i][j] = '.'; } delete_image(img); // On libère la mémoire return grid; }
bool Sudoku::bruteforce_node(Grid& cur_grid, std::size_t cur_x, std::size_t cur_y) { std::size_t unknown_x, unknown_y; //check if we can keep coloring nodes, or if we need to stop and assess the generated board if (find_unknown(cur_grid, cur_x, cur_y, unknown_x, unknown_y)) { //clone the existing game board Grid new_grid(cur_grid); for (int i = 1; i <= (int)cur_grid.n(); i++) { //color the cell value new_grid.set(unknown_x, unknown_y, i); //if the coloring was successful, then return the colored graph indicate success if (bruteforce_node(new_grid, unknown_x, unknown_y)) { cur_grid = new_grid; return true; } } //we couldn't find a solution :( return false; } else { //the board is completely colored, so we can't color any more nodes, but is it a valid coloring? return Validator::is_good_board(cur_grid); } }
void circuit() { int r, c; int do_cut; int radius; int centerx, centery; clear_invert_map(); grid = (char **) new_grid(map.num_row, map.num_col, sizeof(char)); count = (int *) new_array(map.num_row, sizeof(int)); radius = (map.sec_width > map.sec_height ? map.sec_width : map.sec_height)/4.0; radius = (radius == 0 ? 1 : radius ); line_size = (map.sec_width > map.sec_height ? map.sec_width : map.sec_height)/4.0; line_size = (line_size == 0 ? 1 : line_size); for( r = 0; r < map.num_row; r++ ) { count[r] = 0; for( c = 0; c < map.num_col; c++ ) { if( (r == 0 || r == map.num_row-1) && (c == 0 || c == map.num_col/2 || c == map.num_col-1) ) do_cut = 1; else do_cut = rand()%4; if( do_cut != 1 ) { grid[r][c] = 0; continue; } section_center(¢erx, ¢ery, r, c); circlefill(map.map, centerx, centery, radius, 255); grid[r][c] = 1; count[r]++; } } connect_rows(); connect_front(); connect_back(); connect_mid(); delete_grid((void **)grid, map.num_row); delete_array(count); /* redraw outline */ /* TODO: once in a while if cuts off the edge.. */ rect(map.map, 0, 0, map.width-1, map.height-1, 0); return; }
int Sudoku::singular_decider(Grid& cur_grid, bool found_one, std::size_t cur_x, std::size_t cur_y) { std::size_t unknown_x, unknown_y; //check if we can keep coloring nodes, or if we need to stop and assess the generated board if (find_unknown(cur_grid, cur_x, cur_y, unknown_x, unknown_y)) { std::uint_fast64_t colors = Validator::good_colors(cur_grid, unknown_x, unknown_y); //clone the existing game board Grid new_grid(cur_grid); for (int i = 1; i <= (int)cur_grid.n(); i++) { //can we use this color here? if ((colors & (1 << (i - 1))) != 0) { //color the node new_grid.set(unknown_x, unknown_y, i); //if the coloring was successful, then return the colored graph indicate success switch (singular_decider(new_grid, found_one, unknown_x, unknown_y)) { case 0: { break; } //that branch did not yield a solution, so keep going case 1: { found_one = true; break; } //found one solution, so keep going case 2: { return 2; } //confirmed multiple solutions, so stop } } } //we're done with this branch return found_one ? 1 : 0; } else { //the board is completely colored, so we can't color any more nodes, but is it a valid coloring? if (Validator::is_good_board(cur_grid)) { if (found_one) { //oh no, we've got multiple solutions! return 2; } else { //this is just the first solution return 1; } } else { //this branch did not yield a solution return 0; } } }
int main() { grid testG = new_grid(); printf("grid created\n"); delete_grid(testG); printf("grid deleted\n"); return 0; }
/*---------------------------------------------------------------------- * init_grid - initialize grid coordinate system from file * * input : filename - grid parameter definitions file name * file must have following fields: * Grid Width: number_of_columns * Grid Height: number_of_rows * Grid Map Origin Column: col0 * Grid Map Origin Row: row0 * and either one of: * Grid Cells per Map Unit: columns_and_rows * Grid Columns per Map Unit: columns * Grid Rows per Map Unit: rows * or: * Grid Map Units per Cell: map_units * Grid Map Units per Column: map_units * Grid Map Units per Row: map_units * plus: * Grid MPP File: mpp_filename * or map projection parameters embedded in the same label * * old fixed format was as follows: * mpp_filename * number_of_columns number_of_rows * columns_per_map_unit rows_per_map_unit * map_origin_column map_origin_row * * result: pointer to new grid_class instance * or NULL if an error occurs during initialization * * note: for some parameters, if no value is specified in * the .gpd file the parameter is set to a default * value without warning * also, if init_grid fails to open the gpd_file on its * first attempt it will then search the colon separated * list of directories in the map parameters search path * envornment variable * *----------------------------------------------------------------------*/ grid_class *init_grid(char *filename) { char *gpd_filename, *label=NULL; FILE *gpd_file=NULL; grid_class *this=NULL; /* * open .gpd file and read label */ gpd_filename = (char *)malloc(FILENAME_MAX); if (gpd_filename == NULL) { perror("init_grid"); return NULL; } strncpy(gpd_filename, filename, FILENAME_MAX); gpd_file = search_path_fopen(gpd_filename, mapx_PATH, "r"); if (!gpd_file) { fprintf(stderr,"init_grid: error opening parameters file.\n"); perror(filename); return NULL; } label = get_label_keyval(gpd_filename, gpd_file, 0); if (NULL == label) return NULL; /* * initialize projection parameters */ this = new_grid(label); if (NULL == this) goto error_return; free(label); label = NULL; /* * fill in file and filename fields */ this->gpd_filename = gpd_filename; this->gpd_file = gpd_file; if (!this->mapx->mpp_filename) this->mapx->mpp_filename = strdup(gpd_filename); return this; error_return: fprintf(stderr,"init_grid: error reading grid parameters definition file\n"); if (label) free(label); if (gpd_filename) free(gpd_filename); if (gpd_file) fclose(gpd_file); close_grid(this); return NULL; }
static void new_game(t_context *gamestate) { WINDOW **windows; windows = gamestate->windows; if (ft_bitscan(WIN_VALUE) != 1 || WIN_VALUE == 1) { mvwaddstr(windows[SCORE], CENTER(WINC_Y, 1), 2, "I AIN'T RUNNIN' THAT"); wrefresh(windows[SCORE]); gamestate->is_running = 0; return ; } gamestate->is_running = 1; mvwhline(windows[HIGHSCORES], WINA_Y - 2, 1, ' ', WINA_X - 2); wrefresh(windows[HIGHSCORES]); update_score(gamestate); gamestate->grid = new_grid(); add_number(gamestate); add_number(gamestate); draw_grid(gamestate); }
int main() { grid testG = new_grid(); printf("grid created\n"); add_tile(testG); bool isGameOver = game_over(testG); if(isGameOver) // check if an empty grid is a gameover { return EXIT_FAILURE; } else printf("game not over with an empty grid\n"); // FILLING THE GRID WITH UNIQUE VALUES int k = 1; for(int i = 0; i < GRID_SIDE; i++) { for(int j = 0; j < GRID_SIDE; j++) { set_tile(testG, i, j, (tile)k); printf("[%d][%d]:%d\n", i, j, get_tile(testG, i, j)); k++; } } isGameOver = game_over(testG); if(!isGameOver) return EXIT_FAILURE; // check if it's game over with a grid filled with unique values else printf("gameover with a grid filled with unique values\n"); delete_grid(testG); printf("grid deleted\n"); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { gtk_init(&argc, &argv); GtkWidget *window = new_window(); GtkWidget *grid = new_grid(); GtkWidget *notebook = new_notebook(); gtk_container_add(GTK_CONTAINER(window), grid); g_signal_connect(window, "focus", G_CALLBACK(notebook_tab_shortcut), (gpointer)notebook); g_signal_connect(window, "key-press-event", G_CALLBACK(control_key_checker), (gpointer)true); g_signal_connect(window, "key-release-event", G_CALLBACK(control_key_checker), (gpointer)false); gtk_grid_attach(GTK_GRID(grid), notebook, 0, 0, 1, 1); gtk_grid_attach(GTK_GRID(grid), gtk_button_new_with_label("Button"), 0, 1, 1, 1); gtk_widget_show_all(window); gtk_main(); return 0; }
bool Sudoku::color_node(Grid& cur_grid, std::size_t cur_x, std::size_t cur_y) { std::size_t unknown_x, unknown_y; //check if we can keep coloring nodes, or if we need to stop and assess the generated board if (find_unknown(cur_grid, cur_x, cur_y, unknown_x, unknown_y)) { std::uint_fast64_t colors = Validator::good_colors(cur_grid, unknown_x, unknown_y); //clone the existing game board Grid new_grid(cur_grid); for (int i = 1; i <= (int)cur_grid.n(); i++) { //can we use this color here? if ((colors & (1 << (i - 1))) != 0) { //color the node new_grid.set(unknown_x, unknown_y, i); //if the coloring was successful, then return the colored graph indicate success if (color_node(new_grid, unknown_x, unknown_y)) { cur_grid = new_grid; return true; } } } //we couldn't find a coloring :( return false; } else { //the board is completely colored, so we can't color any more nodes, but is it a valid coloring? return Validator::is_good_board(cur_grid); } }
thing_th *Primordial_Grid(int shallReg) { grid_th *grid=calloc(1, sizeof(grid_th)); grid->kind=grid_k; grid->data=new_grid(); return shallReg ? reg_thing((thing_th *)grid) : (thing_th *)grid; }
// Initialize the two grids. void gol_init(int height, int width) { grids[0] = new_grid(height, width); grids[1] = new_grid(height, width); cur_grid = 0; }
// Print the global grid to stdout from rank 0. void gol_print(int height, int width) { // TODO This function only works sequentially (one process). Need // to communicate remote cells to rank 0. int rank = 0, num_procs = 0; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); int *grid_sizes, *offset; if(rank == 0) { grid_sizes = calloc(num_procs, sizeof(int)); offset = calloc(num_procs, sizeof(int)); } int grid_length = grids[0]->height * grids[0]->width; MPI_Gather(&grid_length, 1, MPI_INT, grid_sizes, 1, MPI_INT, 0, MPI_COMM_WORLD); char grid_cells[grids[0]->height * grids[0]->width]; char old_cells[grids[1]->height * grids[1]->width]; int index = 0; for(int row = 0; row < grids[0]->height; row++) { for(int col = 0; col < grids[0]->width; col++) { grid_cells[index] = grid_get(grids[0], row, col); old_cells[index] = grid_get(grids[1], row, col); index++; } } char *whole_grids_cells[2]; struct grid *whole_grids[2]; if(rank == 0) { whole_grids_cells[cur_grid] = calloc(height*width, sizeof(char)); whole_grids_cells[1-cur_grid] = calloc(height*width, sizeof(char)); whole_grids[0] = new_grid(height, width); whole_grids[1] = new_grid(height, width); offset[0] = 0; for(int i = 1; i < num_procs; i++) { offset[i] = grid_sizes[i-1] + offset[i-1]; } } MPI_Gatherv(grid_cells, grid_length, MPI_CHAR, whole_grids_cells[cur_grid], grid_sizes, offset, MPI_CHAR, 0, MPI_COMM_WORLD); MPI_Gatherv(old_cells, grid_length, MPI_CHAR, whole_grids_cells[1-cur_grid], grid_sizes, offset, MPI_CHAR, 0, MPI_COMM_WORLD); if(rank == 0) { index = 0; for(int row = 0; row < whole_grids[0]->height; row++) { for(int col = 0; col < whole_grids[1]->width; col++) { grid_set(whole_grids[0], row, col, whole_grids_cells[cur_grid][index]); grid_set(whole_grids[1], row, col, whole_grids_cells[1-cur_grid][index]); index++; } } struct grid *grid = whole_grids[cur_grid]; struct grid *old = whole_grids[1-cur_grid]; for (int row = 0; row < grid->height; ++row) { for (int col = 0; col < grid->width; ++col) { if (ALIVE == grid_get(grid, row, col)) { // Live cell. printf("o "); } else if (ALIVE == grid_get(old, row, col)) { // Dying cell (empty now but alive in the previous generation). printf("+ "); } else { // Dead cell. printf(". "); } } printf("\n"); } free(grid_sizes); free(offset); free(whole_grids_cells[0]); free(whole_grids_cells[1]); } }
void maze() { char start_dir, dir; int **maze_map; stack_node_t *top, *n; int r, c, tor, toc; int mark; maze_map = (int **)new_grid(map.num_row , map.num_col , sizeof(int)); top = NULL; /* mark all cells as unmarked */ for( r = 0; r < map.num_row; r++ ) for( c = 0; c < map.num_col; c++ ) maze_map[r][c] = UNMARKED; /* pick a random starting point */ r = rand() % map.num_row; c = rand() % map.num_col; printf("start: %d, %d\n", r, c); top = push_stack_node(top, r, c); while( top != NULL ) { n = top; r = n->x; c = n->y; /* pick random direction that leads to an unmarked cell */ mark = UNMARKED; dir = rand()%4; start_dir = dir; get_cell(&tor, &toc, r, c, dir); /* the -1 checks is to skip dirs that lead off the map */ while( (tor == -1 || toc == -1) || maze_map[tor][toc] != UNMARKED ) { dir++; if( dir >= 4 ) dir = 0; if( dir == start_dir ) { /* we've tried every dir so quit */ mark = DEAD_END; break; } get_cell(&tor, &toc, r, c, dir); } /* if we can't go any where */ if( mark == DEAD_END ) { maze_map[r][c] |= DEAD_END; top = pop_stack_node(top, &n); free(n); } else { /* mark current cell as going that direction */ mark = get_mark(dir); maze_map[r][c] |= mark; /* push new cell */ top = push_stack_node(top, tor, toc); } } /* while stack not empty */ /* draw the maze */ for( r = 0; r < map.num_row; r++ ) { for( c = 0; c < map.num_col; c++ ) { /* if maze path went RIGHT then remove the right wall */ /* or if the cell to EAST went LEFT then remove the right wall */ /* so if not A and not B then draw LEFT wall */ if( !(maze_map[r][c] & RIGHT) && !(c < map.num_col-1 && maze_map[r][c+1] & LEFT) ) { draw_right_wall(r, c); } if( !(maze_map[r][c] & DOWN) && !(r < map.num_row-1 && maze_map[r+1][c] & UP) ) { draw_lower_wall(r, c); } } } return; }