// Count the compact walks using brute force. // Enumerate all walks and throw out the ones that not compact. int64_t count_walks_brute(int n) { // Initialize the grid. GRID grid; grid_init(&grid, n); // Define shortcuts for navigating the grid. int delta[4]; delta[LEFT] = -1; delta[RIGHT] = 1; delta[UP] = -grid.ncols; delta[DOWN] = grid.ncols; // Define the grid index corresponding to the anchor point. int grid_index = grid.origin_row * grid.nrows + grid.origin_col; // Index workspace for checking compactness; int index_ws[grid.area]; int direction_histogram[] = {0, 0, 0, 0}; // Count the number of walks. int64_t nwalks = count_continuations_brute(&grid, delta, direction_histogram, index_ws, grid_index, n); // Destroy the grid. grid_destroy(&grid); // Return the number of walks. return nwalks; }
int main(int argc, char *argv[]) { printf("Initializing game\n"); clock_t start, end; int remaining_time; display_init(); gamepad_init(); grid_init(grid); players_init(grid, players); display_fill_screen(0); g_running = 1; while(g_running) { start = clock(); update_pos(grid, players); end = clock(); remaining_time = TIME_PER_LOOP - (((end - start)/CLOCKS_PER_SEC)*1000); if(remaining_time > 0) { usleep(remaining_time*1000); } continue; } exit(EXIT_SUCCESS); }
GameState events_menu_load(GameVars *game_vars) { SDL_Event ev; while (SDL_PollEvent(&ev)) { GameEvent gameev = events_parse(&ev); switch (gameev) { case EV_EXIT: return STATE_EXIT; case EV_KEY_ESC: return STATE_MAIN_MENU; case EV_KEY_SPACE: if (game_vars->grid == NULL) { game_vars->grid = grid_new(-1, -1); grid_init(game_vars->grid); file_load_grid(SAVE_FILE_NAME, game_vars); } return STATE_SIM_PAUSED; case EV_RESIZE: Event_SetWindowSize(game_vars, ev.window.data1, ev.window.data2); break; default: break; } } return game_vars->state; }
int init() { int status = 0; // initialize MPI task numbers status = comm_init(); error_check(&status, "error in comm_init\n"); if(status) return status; // initialize grid status = grid_init(); error_check(&status, "error in grid_init\n"); if(status) return status; // initialize fourier status = fourier_init(); error_check(&status, "error in fourier_init\n"); if(status) return status; // initialize state status = state_init(); error_check(&status, "error in state_init\n"); if(status) return status; // initialize model status = model_init(); error_check(&status, "error in model_init\n"); if(status) return status; // initialize time step status = time_init(); error_check(&status, "error in time_init\n"); if(status) return status; return status; }
void lcs_serial(const char* s1, const char* s2, const int maxlen, char* res) { grid_t grid; grid_init( &grid, s1, s2 ); lcs_length_serial( &grid ); lcs_backtrack_serial(&grid, grid.n, grid.m, maxlen, res); }
void lcs_pthreads(const char* s1, const char* s2, int p, const int maxlen, char* res) { grid_t grid; grid_init( &grid, s1, s2 ); prodcon_queue_t q[p]; lcs_task_t task[p]; pthread_t thread[p]; pthread_barrier_t bar; pthread_barrier_init(&bar, NULL, p); // for each block on this row (m/p) int r; for(r = 0; r < p; r++) { task[r].p = p; task[r].r = r; task[r].q = q; task[r].grid = &grid; task[r].bar = &bar; task[r].res = res; pthread_create( &thread[r], NULL, lcs_thread_main, (void*)&task[r] ); } for(r = 0; r < p; r++) { pthread_join( thread[r], NULL ); } pthread_barrier_destroy(&bar); }
SetupMSW() { Opm::ParseContext parse_context; Opm::ParserPtr parser(new Opm::Parser()); Opm::DeckConstPtr deck = parser->parseFile("msw.data", parse_context); Opm::EclipseStateConstPtr ecl_state(new Opm::EclipseState(*deck , parse_context)); // Create grid. const std::vector<double>& porv = ecl_state->get3DProperties().getDoubleGridProperty("PORV").getData(); std::unique_ptr<GridInit> grid_init(new GridInit(ecl_state, porv)); const Grid& grid = grid_init->grid(); // Create material law manager. std::vector<int> compressed_to_cartesianIdx; Opm::createGlobalCellArray(grid, compressed_to_cartesianIdx); std::shared_ptr<MaterialLawManager> material_law_manager(new MaterialLawManager()); material_law_manager->initFromDeck(deck, ecl_state, compressed_to_cartesianIdx); std::unique_ptr<FluidProps> fluidprops(new FluidProps(deck, ecl_state, material_law_manager, grid)); const size_t current_timestep = 0; // dummy_dynamic_list_econ_lmited const Opm::DynamicListEconLimited dummy_dynamic_list; // Create wells. Opm::WellsManager wells_manager(ecl_state, current_timestep, Opm::UgGridHelpers::numCells(grid), Opm::UgGridHelpers::globalCell(grid), Opm::UgGridHelpers::cartDims(grid), Opm::UgGridHelpers::dimensions(grid), Opm::UgGridHelpers::cell2Faces(grid), Opm::UgGridHelpers::beginFaceCentroids(grid), fluidprops->permeability(), dummy_dynamic_list, false // We need to pass the optionaly arguments // as we get the following error otherwise // with c++ (Debian 4.9.2-10) 4.9.2 and -std=c++11 // converting to ‘const std::unordered_set<std::basic_string<char> >’ from initializer list would use explicit constructor , std::vector<double>(), // null well_potentials std::unordered_set<std::string>()); const Wells* wells = wells_manager.c_wells(); const auto wells_ecl = ecl_state->getSchedule()->getWells(current_timestep); ms_wells.reset(new Opm::MultisegmentWells(wells, wells_ecl, current_timestep)); };
int dl_decomp_init_global(char *basis_file, int q) { assert(basis_file != 0); if (read_basis(basis_file)) return 1; if (grid_init()) return 2; quiet = q; return 0; }
struct grid_t *init_grid_from_random() { struct grid_t *grid; int cols, rows, i, j; srandom(time(NULL)); /* TODO range */ cols = random() % 21 + 40; rows = random() % 21 + 40; grid = grid_init(rows, cols); grid_fill(grid, 0); for (i = 0;i < rows;i++) for (j = 0;j < cols;j++) grid_set(grid, i, j, random() % 2); return grid; }
int main(void) { ps.lose = 0; ps.points = 0; srand(time(NULL)); if (screen_init(&ps)) return 1; grid_init(&ps); while (!ps.lose) { if (!main_cycle(&ps)) break; } freelist(ps.s); screen_end(&ps); return 0; }
SetupMSW() { Opm::ParseContext parse_context; Opm::ParserPtr parser(new Opm::Parser()); Opm::DeckConstPtr deck = parser->parseFile("msw.data", parse_context); Opm::EclipseStateConstPtr ecl_state(new Opm::EclipseState(deck , parse_context)); // Create grid. const std::vector<double>& porv = ecl_state->get3DProperties().getDoubleGridProperty("PORV").getData(); std::unique_ptr<GridInit> grid_init(new GridInit(ecl_state, porv)); const Grid& grid = grid_init->grid(); // Create material law manager. std::vector<int> compressed_to_cartesianIdx; Opm::createGlobalCellArray(grid, compressed_to_cartesianIdx); std::shared_ptr<MaterialLawManager> material_law_manager(new MaterialLawManager()); material_law_manager->initFromDeck(deck, ecl_state, compressed_to_cartesianIdx); std::unique_ptr<FluidProps> fluidprops(new FluidProps(deck, ecl_state, material_law_manager, grid)); const size_t current_timestep = 0; // dummy_dynamic_list_econ_lmited const Opm::DynamicListEconLimited dummy_dynamic_list; // Create wells. Opm::WellsManager wells_manager(ecl_state, current_timestep, Opm::UgGridHelpers::numCells(grid), Opm::UgGridHelpers::globalCell(grid), Opm::UgGridHelpers::cartDims(grid), Opm::UgGridHelpers::dimensions(grid), Opm::UgGridHelpers::cell2Faces(grid), Opm::UgGridHelpers::beginFaceCentroids(grid), fluidprops->permeability(), dummy_dynamic_list, false); const Wells* wells = wells_manager.c_wells(); const auto wells_ecl = ecl_state->getSchedule()->getWells(current_timestep); ms_wells.reset(new Opm::MultisegmentWells(wells, wells_ecl, current_timestep)); };
void DBSCAN_Grid::hash_construct_grid(){ // do some initialization and detect the size of the grid unsigned int features_num = cl_d.size2(); grid_init(features_num); std::vector<float> min_vec(features_num, std::numeric_limits<float>::max()); std::vector<float> max_vec(features_num, std::numeric_limits<float>::min()); getMinMax_grid(min_vec, max_vec); m_min_val.resize(features_num); std::copy(min_vec.begin(), min_vec.end(), m_min_val.begin()); m_n_cnt.resize(features_num); for(unsigned int i=0; i<features_num; i++) m_n_cnt[i] = int((max_vec[i] - min_vec[i]) / m_cell_width) + 1; // for debug for(unsigned int i=0; i<features_num; i++) cout<<m_n_cnt[i]<<" "; cout<<endl; std::vector<int> temp(m_n_cnt.size()); for(unsigned int i=0; i<m_n_cnt.size(); i++) temp[i] = m_n_cnt[i] + 1; mi.set_dimension(features_num); mi.set_max(temp); int uf_counter = 0; int length = (int)cl_d.size1(); for(int i=0; i<length; i++){ for(unsigned int j=0; j<cl_d.size2(); j++) temp[j] = int((cl_d(i, j) - m_min_val[j]) / m_cell_width) + 1; HashType key = mi.hash(temp); std::unordered_map<HashType, Cell>::iterator got = m_hash_grid.find(key); if(got == m_hash_grid.end()){ Cell c; c.ufID = uf_counter++; c.data.push_back(i); m_hash_grid.insert(std::make_pair(key,c)); } else got->second.data.push_back(i); } }
void level_update(int dt, state_t *state) { int next = state->until_next; wave_t *wave = state->wave; level_t *level = state->level; next = next > dt ? next - dt : 0; /* do not advance wave until we are done spawning noobs */ if(next == 0 && level != NULL && state->level_noobs >= wave->noobs) { if(wave->next != NULL) { state->wave = wave = wave->next; next = WAVE_DELAY * 1000; state->level_noobs = 0; } /* only advance level if we have killed/leaked all noobs */ else if(state->kills + state->leaks >= state->total_noobs) { state->level = level->next; if(state->level != NULL) { state->wave = wave = state->level->waves; tower_init(); grid_init(); state->towers = 0; state->power_used = 0; path_load(state, &level->next->map); } next = WAVE_DELAY * 1000; state->level_noobs = 0; } } state->until_next = next; until_spawn = until_spawn > dt ? until_spawn - dt : 0; if(state->level_noobs == wave->noobs) return; if(until_spawn == 0) { noob_spawn(wave->speed, wave->hp, wave->shield, wave->armor_type, state); if(level != NULL) state->level_noobs++; until_spawn += wave->delay; } }
//---- Load balancing algorithms ------------------------------------ void balance_init() { int i; n_grid_units = 1<<((tm_wm.depth+1)/2); m_grid_units = 1<<((tm_wm.depth)/2); grid_unit_sz_x = (tm_wm.size.x % n_grid_units) == 0 ? (tm_wm.size.x / n_grid_units) : ((tm_wm.size.x / n_grid_units) + 1); grid_unit_sz_y = (tm_wm.size.y % m_grid_units) == 0 ? (tm_wm.size.y / m_grid_units) : ((tm_wm.size.y / m_grid_units) + 1); grid = (grid_unit_t**)malloc(n_grid_units * sizeof(grid_unit_t*)); assert( grid ); for(i=0; i<n_grid_units; i++) { grid[i] = (grid_unit_t*)malloc(m_grid_units * sizeof(grid_unit_t)); assert( grid[i] ); } thread_stats = (int*)malloc(sv.num_threads * sizeof(int)); assert( thread_stats ); grid_init(grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y ); }
GameState events_menu_new(GameVars *game_vars) { SDL_Event ev; while (SDL_PollEvent(&ev)) { GameEvent gameev = events_parse(&ev); switch (gameev) { case EV_EXIT: return STATE_EXIT; case EV_KEY_ESC: return STATE_MAIN_MENU; case EV_KEY_SPACE: if (game_vars->grid == NULL) { game_vars->grid = grid_new(game_vars->grid_size.x, game_vars->grid_size.y); grid_init(game_vars->grid); } return STATE_SIM_PAUSED; case EV_KEY_UP: game_vars->grid_size.y++; break; case EV_KEY_DOWN: if (game_vars->grid_size.y > 1) { game_vars->grid_size.y--; } break; case EV_KEY_RIGHT: game_vars->grid_size.x++; break; case EV_KEY_LEFT: if (game_vars->grid_size.x > 1) { game_vars->grid_size.x--; } break; case EV_RESIZE: Event_SetWindowSize(game_vars, ev.window.data1, ev.window.data2); break; default: break; } } return game_vars->state; }
int64_t count_walks(int n) { // Initialize the grid. GRID grid; grid_init(&grid, n); // Define shortcuts for navigating the grid. int delta[4]; delta[LEFT] = -1; delta[RIGHT] = 1; delta[UP] = -grid.ncols; delta[DOWN] = grid.ncols; // Define the grid index corresponding to the anchor point. int grid_index = grid.origin_row * grid.nrows + grid.origin_col; // Index workspace for checking fillability of void regions. int index_ws[grid.area]; // This flag is true if we have begun filling a void region. bool filling = false; // Initialize neighborhood lookup. int neighborhood_lookup[256]; init_empty_neighbor_group_lookup(neighborhood_lookup); int direction_histogram[] = {0, 0, 0, 0}; // Count the number of walks. int64_t nwalks = count_continuations(&grid, delta, neighborhood_lookup, direction_histogram, index_ws, grid_index, n, filling); // Destroy the grid. grid_destroy(&grid); // Return the number of walks. return nwalks; }
SetupTest () { Opm::Parser parser; auto deck = parser.parseFile("TESTWELLMODEL.DATA"); ecl_state.reset(new Opm::EclipseState(deck) ); { const Opm::TableManager table ( deck ); const Opm::Eclipse3DProperties eclipseProperties ( deck , table, ecl_state->getInputGrid()); const Opm::Runspec runspec (deck); schedule.reset( new Opm::Schedule(deck, ecl_state->getInputGrid(), eclipseProperties, runspec)); } // Create grid. const std::vector<double>& porv = ecl_state->get3DProperties().getDoubleGridProperty("PORV").getData(); std::unique_ptr<GridInit> grid_init(new GridInit(*ecl_state, porv)); const Grid& grid = grid_init->grid(); // Create material law manager. std::vector<int> compressed_to_cartesianIdx; Opm::createGlobalCellArray(grid, compressed_to_cartesianIdx); current_timestep = 0; // Create wells. wells_manager.reset(new Opm::WellsManager(*ecl_state, *schedule, current_timestep, Opm::UgGridHelpers::numCells(grid), Opm::UgGridHelpers::globalCell(grid), Opm::UgGridHelpers::cartDims(grid), Opm::UgGridHelpers::dimensions(grid), Opm::UgGridHelpers::cell2Faces(grid), Opm::UgGridHelpers::beginFaceCentroids(grid), false, std::unordered_set<std::string>() ) ); };
struct grid_t *init_grid_from_file(char *fname, int max_cols, \ char comment, char alive, char dead) { struct grid_t *grid; FILE *stream; int cols, rows, counter; char *line, *c; stream = fopen(fname, "r"); assert(stream != NULL); line = malloc(sizeof(char) * max_cols); cols = -1; rows = -1; counter = 0; for(;getline(&line, (size_t *) &max_cols, stream) != -1;) { if (line[0] != comment) { if (rows == -1) { rows = atoi(line); assert(rows != -1); } else if (cols == -1) { cols = atoi(line); assert(cols != -1); grid = grid_init(rows, cols); grid_fill(grid, 0); } else if (counter < rows * cols) { /* read the grid */ assert(rows != -1 && cols != -1); for (c = line;*c != '\n' && *c != EOF;c++, counter++) grid_set(grid, counter / cols, counter % cols,\ (*c == alive) ? (1) : (0)); } } } assert(rows != -1 && cols != -1 && grid != NULL); fclose(stream); return grid; }
int test_compactness_1() { printf("test 1\n"); // Initialize the test grid. int nrows = 7; int ncols = 7; int area = nrows * ncols; int original_grid[] = { -5, -5, -5, -5, -5, -5, -5, -5, -1, 10, 10, 10, 10, -5, -5, 10, 10, -1, -1, 10, -5, -5, 10, -1, -1, -1, 10, -5, -5, 10, -1, -1, 10, 10, -5, -5, 10, 20, 30, 10, -1, -5, -5, -5, -5, -5, -5, -5, -5, }; // Initialize the grid structure. GRID grid; grid_init(&grid, 2); // Compare the dimensions to the test grid. assert(grid.nrows == nrows); assert(grid.ncols == ncols); assert(grid.area == area); // Copy the test grid data into the grid structure. memcpy(grid.data, original_grid, sizeof original_grid); // Run some tests. int nfails = 0; // Declare variables. int query_row; int query_col; int void_row; int void_col; int query_index; int void_index; int nremaining; int expected_fillability; // Setup for vertex 20 stepping upwards. query_row = 5; query_col = 2; void_row = 4; void_col = 2; query_index = query_row*ncols + query_col; void_index = void_row*ncols + void_col; // This step should not work regardless of the number of vertices remaining, // because the parity is wrong. nremaining = 6; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 7; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 8; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // Setup for vertex 30 stepping in the up direction. query_row = 5; query_col = 3; void_row = 4; void_col = 3; query_index = query_row*ncols + query_col; void_index = void_row*ncols + void_col; // This step works when the number of remaining vertices is correct. nremaining = 6; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 7; expected_fillability = true; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 8; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // Destroy the grid. grid_destroy(&grid); return nfails; }
Grid *grid_create(unsigned rows, unsigned cols) { Grid *grid = malloc(grid_calc_size(rows, cols)); grid_init(grid, rows, cols); return grid; }
void state_init(struct state *s, int w, int h, enum stencil shape, unsigned int map_seed, int keep_random, int locations_num, int clients_num, int conditions, int inequality, enum config_speed speed, enum config_dif dif){ s->speed = speed; s->prev_speed = s->speed; s->dif = dif; s->map_seed = map_seed; s->conditions = conditions; s->inequality = inequality; s->time = (1850 + rand()%100) * 360 + rand()%360; /* player controlled from the keyboard */ s->controlled = 1; /* int players[] = {7, 2, 3, 5}; */ /* int players[] = {2, 3, 4, 5, 6, 7}; */ int all_players[] = {1, 2, 3, 4, 5, 6, 7}; int comp_players[MAX_PLAYER]; int comp_players_num = 7 - clients_num; s->kings_num = comp_players_num; int ui_players[MAX_PLAYER]; int ui_players_num = clients_num; int i; for (i=0; i<7; ++i) { if (i<clients_num) ui_players[i] = all_players[i]; else { int j = i - clients_num; /* computer player index / king's index */ comp_players[j] = all_players[i]; switch (i){ case 1: king_init (&s->king[j], i+1, opportunist, &s->grid, s->dif); break; case 2: king_init (&s->king[j], i+1, one_greedy, &s->grid, s->dif); break; case 3: king_init (&s->king[j], i+1, none, &s->grid, s->dif); break; case 4: king_init (&s->king[j], i+1, aggr_greedy, &s->grid, s->dif); break; case 5: king_init (&s->king[j], i+1, noble, &s->grid, s->dif); break; case 6: king_init (&s->king[j], i+1, persistent_greedy, &s->grid, s->dif); break; } } } /* Initialize map generation with the map_seed */ srand(s->map_seed); /* Map generation starts */ { int conflict_code = 0; do { grid_init(&s->grid, w, h); /* starting locations arrays */ struct loc loc_arr[MAX_AVLBL_LOC]; int available_loc_num = 0; int d = 2; apply_stencil(shape, &s->grid, d, loc_arr, &available_loc_num); /* find the leftmost visible tile */ /* int i, j; s->xskip = MAX_WIDTH * 2 + 1; for(i=0; i<s->grid.width; ++i) for(j=0; j<s->grid.height; ++j) if(is_visible(s->grid.tiles[i][j].cl)) { int x = i*2 + j; if (s->xskip > x) s->xskip = x; } s->xskip = s->xskip/2; */ /* conflict mode */ conflict_code = 0; if (!keep_random) conflict_code = conflict(&s->grid, loc_arr, available_loc_num, comp_players, comp_players_num, locations_num, ui_players, ui_players_num, s->conditions, s->inequality); } while(conflict_code != 0 || !is_connected(&s->grid)); } /* Map is ready */ int p; for(p = 0; p<MAX_PLAYER; ++p) { flag_grid_init(&s->fg[p], w, h); s->country[p].gold = 0; } /* cursor location */ /* s->cursor.i = s->grid.width/2; s->cursor.j = s->grid.height/2; int i, j; for (i=0; i<s->grid.width; ++i) { for (j=0; j<s->grid.height; ++j) { if (s->grid.tiles[i][j].units[s->controlled][citizen] > s->grid.tiles[s->cursor.i][s->cursor.j].units[s->controlled][citizen]) { s->cursor.i = i; s->cursor.j = j; } } } */ /* Kings */ /* s->kings_num = 5; */ /* king_init (&s->king[0], 2, opportunist, &s->grid, s->dif); */ /* king_init (&s->king[1], 3, one_greedy, &s->grid, s->dif); king_init (&s->king[2], 7, persistent_greedy, &s->grid, s->dif); king_init (&s->king[3], 5, aggr_greedy, &s->grid, s->dif); king_init (&s->king[4], 4, none, &s->grid, s->dif); king_init (&s->king[0], 6, noble, &s->grid, s->dif); */ }
void UPnP_Media_Server(IDirectFBEventBuffer *p_eventbuffer) { GridObj *grid=NULL, *text_grid=NULL; IconObj *icon=NULL; TextObj *text=NULL, *text_selected=NULL; UPnPNode *head=NULL, *unode_selected=NULL; DFBRectangle *page_space; int key = 0, grid_count = 1, serv_response = 0; int page_size=5, line_distance=50; serv_response = upnp_cmd_handler(p_eventbuffer, &head, NULL, SHOWMS); if(serv_response!=REQ_SUCCESS) return; icon_alert(PHOTO_LOADING_G, D_MOVE_CENTER); page_space = (DFBRectangle*)malloc(sizeof(DFBRectangle)); grid_init(page_space, screen_space->x+50, screen_space->y+80, screen_space->w-100, screen_space->h-160); grid=create_view_tree(grid_count, page_space); icon=create_icon_tree(grid, main_layer, win_dsc); text_grid = create_text_grid_tree(page_size, line_distance, page_space); text = create_text_node_tree(text_grid); upnp_text_node_map(head, text); draw_text_table(text, icon->window_surface); icon_opacity(icon, 255); icon_stackclass(icon, DWSC_MIDDLE); close_icon_alert(); text_selected=text; unode_selected=head; while(1) { key = GetInputDeviceEventByType(p_eventbuffer,PTYPE_NOSCREEN_API); if( key == DIKI_UP ) { upnp_cmd_handler(p_eventbuffer, NULL, NULL, CDUP); free(page_space); grid_free(grid); grid_free(text_grid); icons_free(icon); texts_free(text); delete_unode_tree(head); return; }else if( key == DIKI_LEFT ){ if(unode_selected->elder_brother_node!=NULL) { if(text_selected->left!=NULL&&text_selected->left->content!=NULL) { unode_selected=unode_selected->elder_brother_node; shift_text_left(&text_selected, icon->window_surface); }else { go_prev_page(page_size, &unode_selected, text, icon->window_surface); text_selected=text; } } }else if( key == DIKI_RIGHT ){ if(unode_selected->brother_node!=NULL) { unode_selected=unode_selected->brother_node; if(text_selected->right!=NULL&&text_selected->right->content!=NULL) { shift_text_right(&text_selected, icon->window_surface); }else{ go_next_page(unode_selected, text, icon->window_surface); text_selected=text; } } }else if( key == DIKI_DOWN ){ }else if( key == DIKI_ENTER ){ serv_response = upnp_cmd_handler(p_eventbuffer, NULL, unode_selected->guid, SETMS); icon_opacity(icon, 0); UPnP_OpenMediaServer(p_eventbuffer, NULL); draw_title_string("Select Media Server", font_title, rgba, title_space, title_window_surface); title_window_surface->Flip(title_window_surface, NULL, DSFLIP_NONE); icon_opacity(icon, 255); }else if( key == DIKI_A || key == DIKI_B ){ icon_opacity(icon, 0); Detect_USBMount(p_eventbuffer); icon_opacity(icon, 255); p_eventbuffer->Reset(p_eventbuffer); } p_eventbuffer->Reset(p_eventbuffer); } }
void UPnP_MediaController(IDirectFBEventBuffer *p_eventbuffer, char *media_device_guid) { GridObj *grid=NULL; IconObj *toolbar_icon=NULL; FcNode *toolbar_node=NULL; DFBRectangle *toolbar_space, *toolbar_icon_space; int key = 0, grid_count = 6, serv_response = 0, toolbar_style=H_STYLE; char *toolbar[]={"play","pause","stop","mute","voice up","voice down","0x"}; char *toolbar_path[]={MEDIA_PLAY_0_G, MEDIA_PLAY_1_G, MEDIA_PLAY_2_G, MEDIA_PLAY_3_G, MEDIA_PLAY_4_G, MEDIA_PLAY_5_G,"0x"}; serv_response = upnp_cmd_handler(p_eventbuffer, NULL, media_device_guid, OPEN); if(serv_response !=REQ_SUCCESS) return; toolbar_space = (DFBRectangle*)malloc(sizeof(DFBRectangle)); toolbar_icon_space = (DFBRectangle*)malloc(sizeof(DFBRectangle)); grid_init(toolbar_space, screen_space->x+100, screen_space->y+380, screen_space->w-100, screen_space->h-350); grid_init(toolbar_icon_space, 0, 0, 50, 50); grid=create_button_tree(grid_count, toolbar_space, toolbar_style); toolbar_icon=create_icon_tree(grid, main_layer, win_dsc); toolbar_node=create_menu_tree(toolbar, toolbar_path); draw_icon_tree(toolbar_node, toolbar_icon, toolbar_icon_space, gp_dfb); icon_opacity(toolbar_icon, 100); toolbar_icon->window->SetOpacity(toolbar_icon->window, 255); icon_stackclass(toolbar_icon, DWSC_MIDDLE); while(1) { key = GetInputDeviceEventByType(p_eventbuffer,PTYPE_NOSCREEN_API); if( key == DIKI_UP ) { free(toolbar_space); free(toolbar_icon_space); grid_free(grid); icons_free(toolbar_icon); delete_fcnode_tree(toolbar_node); return; }else if( key == DIKI_LEFT ){ if(toolbar_node->elder_brother_node!=NULL) { toolbar_node=toolbar_node->elder_brother_node; toolbar_icon->window->SetOpacity(toolbar_icon->window, 100); toolbar_icon=toolbar_icon->left; toolbar_icon->window->SetOpacity(toolbar_icon->window, 255); } }else if( key == DIKI_RIGHT ){ if(toolbar_node->brother_node!=NULL) { toolbar_node=toolbar_node->brother_node; toolbar_icon->window->SetOpacity(toolbar_icon->window, 100); toolbar_icon=toolbar_icon->right; toolbar_icon->window->SetOpacity(toolbar_icon->window, 255); } }else if( key == DIKI_DOWN ){ free(toolbar_space); free(toolbar_icon_space); grid_free(grid); icons_free(toolbar_icon); delete_fcnode_tree(toolbar_node); return; }else if( key == DIKI_ENTER ){ if(!strcmp(toolbar_node->title,toolbar[0])){ serv_response = upnp_cmd_handler(p_eventbuffer, NULL, NULL, PLAY); }else if(!strcmp(toolbar_node->title,toolbar[1])){ serv_response = upnp_cmd_handler(p_eventbuffer, NULL, NULL, STOP); }else if(!strcmp(toolbar_node->title,toolbar[2])){ serv_response = upnp_cmd_handler(p_eventbuffer, NULL, NULL, PAUSE); }else if(!strcmp(toolbar_node->title,toolbar[3])){ serv_response = upnp_cmd_handler(p_eventbuffer, NULL, NULL, MUTE); }else if(!strcmp(toolbar_node->title,toolbar[4])){ serv_response = upnp_cmd_handler(p_eventbuffer, NULL, NULL, VUP); }else if(!strcmp(toolbar_node->title,toolbar[5])){ serv_response = upnp_cmd_handler(p_eventbuffer, NULL, NULL, VDOWN); } }else if( key == DIKI_A || key == DIKI_B ){ icon_opacity(toolbar_icon, 0); Detect_USBMount(p_eventbuffer); icon_opacity(toolbar_icon, 255); p_eventbuffer->Reset(p_eventbuffer); } p_eventbuffer->Reset(p_eventbuffer); } }
int test_compactness_0() { printf("test 0\n"); // Initialize the test grid. int nrows = 7; int ncols = 7; int area = nrows * ncols; int original_grid[] = { -5, -5, -5, -5, -5, -5, -5, -5, 14, 13, 12, -1, -1, -5, -5, 15, -1, 11, -1, -1, -5, -5, 16, -1, 10, -1, -1, -5, -5, 17, -1, -1, 24, 23, -5, -5, 18, 19, 20, 21, 22, -5, -5, -5, -5, -5, -5, -5, -5, }; // Initialize the grid structure. GRID grid; grid_init(&grid, 2); // Compare the dimensions to the test grid. assert(grid.nrows == nrows); assert(grid.ncols == ncols); assert(grid.area == area); // Copy the test grid data into the grid structure. memcpy(grid.data, original_grid, sizeof original_grid); // Run some tests. int nfails = 0; // Declare variables. int query_row; int query_col; int void_row; int void_col; int query_index; int void_index; int nremaining; int expected_fillability; // Setup for vertex 24 stepping to the left. query_row = 4; query_col = 4; void_row = 4; void_col = 3; query_index = query_row*ncols + query_col; void_index = void_row*ncols + void_col; // This step should work only when four vertices remain. nremaining = 3; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 4; expected_fillability = true; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 5; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // Setup for vertex 24 stepping in the up direction. query_row = 4; query_col = 4; void_row = 3; void_col = 4; query_index = query_row*ncols + query_col; void_index = void_row*ncols + void_col; // This step should work regardless of how many vertices remain, // because this void region is connected to the border. nremaining = 3; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 4; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 5; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // Destroy the grid. grid_destroy(&grid); return nfails; }
void state_init(struct state *s, struct basic_options *op, struct multi_options *mop){ s->speed = op->speed; s->prev_speed = s->speed; s->dif = op->dif; s->map_seed = op->map_seed; s->conditions = op->conditions; s->inequality = op->inequality; s->winlosecondition = 0; s->time = (1850 + rand()%100) * 360 + rand()%360; /* player controlled from the keyboard */ s->controlled = 1; /* int players[] = {7, 2, 3, 5}; */ /* int players[] = {2, 3, 4, 5, 6, 7}; */ int all_players[] = {1, 2, 3, 4, 5, 6, 7}; int comp_players[MAX_PLAYER]; int comp_players_num = 7 - mop->clients_num; s->kings_num = comp_players_num; int ui_players[MAX_PLAYER]; int ui_players_num = mop->clients_num; int i; for (i=0; i<7; ++i) { if (i<mop->clients_num) ui_players[i] = all_players[i]; else { int j = i - mop->clients_num; /* computer player index / king's index */ comp_players[j] = all_players[i]; switch (i){ case 1: king_init (&s->king[j], i+1, opportunist, &s->grid, s->dif); break; case 2: king_init (&s->king[j], i+1, one_greedy, &s->grid, s->dif); break; case 3: king_init (&s->king[j], i+1, none, &s->grid, s->dif); break; case 4: king_init (&s->king[j], i+1, aggr_greedy, &s->grid, s->dif); break; case 5: king_init (&s->king[j], i+1, noble, &s->grid, s->dif); break; case 6: king_init (&s->king[j], i+1, persistent_greedy, &s->grid, s->dif); break; } } } /* Initialize map generation with the map_seed */ srand(s->map_seed); /* Map generation starts */ { int conflict_code = 0; do { grid_init(&s->grid, op->w, op->h); /* starting locations arrays */ struct loc loc_arr[MAX_AVLBL_LOC]; int available_loc_num = 0; int d = 2; apply_stencil(op->shape, &s->grid, d, loc_arr, &available_loc_num); /* conflict mode */ conflict_code = 0; if (!op->keep_random_flag) conflict_code = conflict(&s->grid, loc_arr, available_loc_num, comp_players, comp_players_num, op->loc_num, ui_players, ui_players_num, s->conditions, s->inequality); } while(conflict_code != 0 || !is_connected(&s->grid)); } /* Map is ready */ int p; for(p = 0; p<MAX_PLAYER; ++p) { flag_grid_init(&s->fg[p], op->w, op->h); s->country[p].gold = 0; } /* kings evaluate the map */ for(p = 0; p < s->kings_num; ++p) { king_evaluate_map(&s->king[p], &s->grid, s->dif); } /* Zero timeline */ s->show_timeline = op->timeline_flag; s->timeline.mark = -1; for(i=0; i<MAX_TIMELINE_MARK; ++i) { s->timeline.time[i] = s->time; for(p=0; p<MAX_PLAYER; ++p) { s->timeline.data[p][i] = 0.0; } } /* init turn by turn */ for(p=0; p<mop->clients_num; ++p) { s->turn_validated[p] = 0; } }
int main() { int i; char a[N][N]; enemy_ship s[M], old[M]; my_ship mine; srand(time(NULL)); if(screen_init() == 1) return 1; grid_init(a); print_grid(a); //initialize enemy ships + old shoots for(i = 0; i < M; i++) { s[i].ship_col = -1; old[i].ship_col = -1; } ships_generate(a, s); //initialize my ship for(i = 0; i < N; i++) mine.shoot_col[i] = -1; mine.col = 0; i--; a[i][mine.col] = '*'; bold_print(i, mine.col, a[i][mine.col]); do { move(row + i * vert_space, col + mine.col * horiz_space); change_refresh_time(); refresh(); switch(getch()) { case KEY_LEFT: if((mine.col > 0) && (a[i][mine.col - 1] != ':')) { a[i][mine.col] = ' '; printw("%c", a[i][mine.col]); mine.col--; a[i][mine.col] = '*'; mvprintw(row + i * vert_space, col + mine.col * horiz_space, "%c", a[i][mine.col]); } break; case KEY_RIGHT: if((mine.col < N - 1) && (a[i][mine.col + 1] != ':')) { a[i][mine.col] = ' '; printw("%c", a[i][mine.col]); mine.col++; a[i][mine.col] = '*'; mvprintw(row + i * vert_space, col + mine.col * horiz_space, "%c", a[i][mine.col]); } break; case 32: //space to shoot my_shoot(&mine); my_shoots_sort(&mine); my_shoots_num++; break; case KEY_F(2): //f2 to exit screen_end(); return 0; } if(points < 800) { if(points >= 100) { change_num_ships(a, s); if(points >= 150) change_height(a, s, &mine, old); } } if(my_shoots_num != 0) update_my_shoot(a, s, &mine, old); enemy_shoots(a, s, mine.col); old_shoots_recover(a, old, mine.col); } while(victory); screen_end(); return 0; }
int main(void) { int i, position_x, position_y; /* Declaration of the needed data structures. */ grid_t * certainty_grid; range_measure_t measure[MEASUREMENTS]; hist_t * polar_histogram; control_signal_t control_signal; /* ** Initialization of the grid and the histogram. */ certainty_grid = grid_init(50, 10); polar_histogram = hist_init(2, 20, 10, 5); /* Are the initializations ok? */ if (certainty_grid == NULL) return -1; if (certainty_grid->cells == NULL) return -1; if (polar_histogram == NULL) return -1; if (polar_histogram->densities == NULL) return -1; /* ** Fake measures. */ printf("Measures\n"); for (i = 0; i < MEASUREMENTS; ++i) { measure[i].direction = (int) ((360.0 * rand()) / RAND_MAX); /* [degrees] */ measure[i].distance = (int) (((130.0 * rand()) / RAND_MAX) + 20); /* [cm] */ if (i < 50) printf("\t%2d: %3d [cm], %3d [degrees]\n", i, (int) measure[i].distance, measure[i].direction); } if (i > 50) printf("\t...\n"); /* Let's assume the 'robot' is in the middle of the grid. */ position_x = position_y = (certainty_grid->dimension + 1) / 2; printf("\nPosition of the robot: (%d, %d)\n", position_x, position_y); /* Add the information from the measures to the grid. */ printf("\nUpdating the certainty grid...\n"); for (i = 0; i < MEASUREMENTS; ++i) { grid_update(certainty_grid, position_x, position_y, measure[i]); } /* ** Calculating the control signals. */ /* Generate the histogram for the current grid. */ printf("\nUpdating the polar histogram...\n"); hist_update(polar_histogram, certainty_grid); /* What's the next direction? */ control_signal.direction = calculate_direction(polar_histogram, OBJECTIVE_DIRECTION); printf("\nNext direction: %d [degrees]\n", control_signal.direction); free(certainty_grid); free(polar_histogram); certainty_grid = NULL; polar_histogram = NULL; return 0; }
int test_compactness_2() { printf("test 2\n"); printf("neither starting point is blocked by parity\n"); printf("but both are blocked by an excess of degree-1 vertices\n"); // Initialize the test grid. int nrows = 7; int ncols = 7; int area = nrows * ncols; int original_grid[] = { -5, -5, -5, -5, -5, -5, -5, -5, -1, 10, 10, 10, -1, -5, -5, 10, 10, -1, 10, -1, -5, -5, 10, -1, -1, 10, 10, -5, -5, 10, -1, -1, -1, 10, -5, -5, 10, 20, 30, 10, 10, -5, -5, -5, -5, -5, -5, -5, -5, }; // Initialize the grid structure. GRID grid; grid_init(&grid, 2); // Compare the dimensions to the test grid. assert(grid.nrows == nrows); assert(grid.ncols == ncols); assert(grid.area == area); // Copy the test grid data into the grid structure. memcpy(grid.data, original_grid, sizeof original_grid); // Run some tests. int nfails = 0; // Declare variables. int query_row; int query_col; int void_row; int void_col; int query_index; int void_index; int nremaining; int expected_fillability; // Setup for vertex 20 stepping upwards. query_row = 5; query_col = 2; void_row = 4; void_col = 2; query_index = query_row*ncols + query_col; void_index = void_row*ncols + void_col; // Blocked by too many degree-1 vertices. nremaining = 6; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 7; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 8; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // Setup for vertex 30 stepping in the up direction. query_row = 5; query_col = 3; void_row = 4; void_col = 3; query_index = query_row*ncols + query_col; void_index = void_row*ncols + void_col; // Blocked by too many degree-1 vertices. nremaining = 6; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 7; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // nremaining = 8; expected_fillability = false; nfails += _compactness_test_helper(&grid, original_grid, query_index, void_index, nremaining, expected_fillability); // Destroy the grid. grid_destroy(&grid); return nfails; }
void surfit() { writelog(LOG_MESSAGE,""); writelog(LOG_MESSAGE,""); time_t ltime_begin; time( <ime_begin ); if (map_name == NULL) map_name = strdup("noname"); if (strlen(map_name) == 0) { free(map_name); map_name = strdup("noname"); } if (functionals->size() == 0) { surfit_data_manager->auto_functionals(); } bool method_ok = false; grid_init(); grid_prepare(); if (surfit_grid == NULL) { writelog(LOG_ERROR,"grid not found : aborting"); return; } size_t i; while( !method_ok ) { grid_begin(); std::vector<functional *> * Functionals = new std::vector<functional *>(*functionals); size_t f_size = Functionals->size(); for (i = 0; i < f_size; i++) { if (stop_execution == 1) break; functional * fnc = (*Functionals)[i]; std::vector<functional *>::iterator fnc_it = Functionals->begin()+i; bool res = fnc->minimize(); if (res == false) { if (i+1 < f_size) { functional * next_fnc = (*Functionals)[i+1]; next_fnc->cond_add(fnc); next_fnc->cond_move(fnc); i--; Functionals->erase(fnc_it); f_size = Functionals->size(); } } else { fnc->mark_solved_and_undefined(method_mask_solved, method_mask_undefined, false); if (fnc->cond()) { int k; for (k = fnc->cond_size()-1; k >= 0; k--) { functional * fnc_cnd = fnc->cond_get(k); Functionals->insert(fnc_it+1, fnc_cnd); fnc->cond_erase(k); f_size = Functionals->size(); } } fnc->drop_private_data(); } }; for (i = 0; i < (int)Functionals->size(); i++) { functional * fnc = (*Functionals)[i]; if (fnc->cond()) fnc->cond_erase_all(); fnc->drop_private_data(); } delete Functionals; if (stop_execution != 1) grid_finish(method_ok); else break; }; size_t f_size = functionals->size(); for (i = 0; i < f_size; i++) { functional * f = (*functionals)[i]; f->cleanup(); } grid_release(); time_t ltime_end; time( <ime_end ); double sec = difftime(ltime_end,ltime_begin); int minutes = (int)(sec/REAL(60)); sec -= minutes*60; writelog(LOG_MESSAGE,"Elapsed time %d minutes %G seconds",minutes,sec); };
int main(int ac, char** av) { grid_t g; state_t state; bfs_t b; cmdline_t cmd; rulset_t rulset; const rule_t* ru; unsigned int i; grid_init(&g, 5); state_init(&state); rulset_init(&rulset); /* start with first rule */ state.cur_rule = rulset.rules; while (state.is_done == 0) { cmdline_get(&cmd); switch (cmd.op) { case CMDLINE_OP_PUT_BLOCK: if (state.items[state.cur_color]) { if (*grid_at(&g, cmd.x, cmd.y) == BLOCK_COLOR_INVALID) { *grid_at(&g, cmd.x, cmd.y) = state.cur_color; --state.items[state.cur_color]; } } break ; case CMDLINE_OP_GET_BLOCK: if (*grid_at(&g, cmd.x, cmd.y) != BLOCK_COLOR_INVALID) { ++state.items[*grid_at(&g, cmd.x, cmd.y)]; *grid_at(&g, cmd.x, cmd.y) = BLOCK_COLOR_INVALID; } break ; case CMDLINE_OP_SEL_COLOR: state.cur_color = cmd.color; break ; case CMDLINE_OP_SEL_RULE: ru = rulset.rules; for (i = 0; ru && (i < cmd.rule); ++i, ru = ru->next) ; state.cur_rule = (rule_t*)ru; if (ru == NULL) { printf("no such rule\n"); break ; } goto eval_rule_case; break ; case CMDLINE_OP_LIST_RULES: ru = rulset.rules; for (i = 0; ru; ++i, ru = ru->next) printf("[%u] %u\n", i, ru->outcome); break ; case CMDLINE_OP_LIST_ITEMS: for (i = 0; i < 3; ++i) printf(" %u", state.items[i]); printf("\n"); break ; case CMDLINE_OP_EVAL_RULE: eval_rule_case: if (state.cur_rule == NULL) { printf("no rule selected\n"); break ; } bfs_do(&b, &g, state.cur_rule); state.cur_dist = b.dist; printf("distance: %u\n", state.cur_dist); break ; case CMDLINE_OP_PRINT_GRID: grid_print(&g); break ; case CMDLINE_OP_QUIT: state.is_done = 1; break ; case CMDLINE_OP_INVALID: default: break ; } } grid_fini(&g); rulset_fini(&rulset); return 0; }