Example #1
0
int		fill_grid(struct s_grid *grid, int x, int y)
{
	int value;
	int next_x;
	int next_y;

	if (y == 9)
		return (1);
	next_x = x + 1;
	next_y = y;
	if (next_x == 9)
	{
		next_x = 0;
		next_y++;
	}
	if (*(grid->lines[y][x]) != '0')
		return (fill_grid(grid, next_x, next_y));
	value = '0';
	while (++value <= '9')
	{
		*(grid->lines[y][x]) = value;
		if (ft_check_case(x, y, grid) == 1)
			if (fill_grid(grid, next_x, next_y) == 1)
				return (1);
	}
	*(grid->lines[y][x]) = '0';
	return (0);
}
Example #2
0
/*
Ouverture d'un fichier
@params	p_widget	Elément ayant déclencher la fonction
@params	user_data	Données transmis au callback
*/
void cb_open_file(GtkWidget *p_widget, gpointer user_data){
    GtkWidget *p_dialog_open;
    p_dialog_open = gtk_file_chooser_dialog_new ("Ouvrir un fichier",
                                            GTK_WINDOW(widgetCallback.entry1),
                                            GTK_FILE_CHOOSER_ACTION_OPEN,
                                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                            NULL);

    if (gtk_dialog_run(GTK_DIALOG(p_dialog_open)) == GTK_RESPONSE_ACCEPT){ //Le fichier est choisit
        char *filename;
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(p_dialog_open));

        int grid[9][9] = {{0}};
        int grid_fixes[9][9] = {{0}};

        //On récupère et on affiche la grille
        get_grid_from_file(grid, grid_fixes, filename);
        fill_grid(grid, grid_fixes);
    }

    gtk_widget_destroy(p_dialog_open);

    //Parametres inutilises
    (void)p_widget;
}
Example #3
0
int forest_fire(int x, int y, double density)
{
    Forest *forest;
    printf(COLOR_YELLOW"\nForest Fire Simulation will be executed. "
        COLOR_RESET "Details:\n");
    printf("Gridsize: %d by %d\n", x, y);
    printf("Vegetation density: %g\n", density);
    forest = init_grid(x, y);
    fill_grid(forest, density);
    //print_grid(forest);
    //printf("\n");
    while(forest_fire_sim(forest))
    {
        //print_grid(forest);
        //printf("\n");
        //sleep(1);
    }
    if(forest->crossed > 0)
    {
        printf(COLOR_GREEN "Opposite side of the forest reached in %d "
            "steps.\n", forest->crossed);
        printf(COLOR_RESET);
        return forest->crossed;
    }
    printf(COLOR_RED "Couldn't reach the other side of the forest. "
        "Burning stopped after %d steps.\n", abs(forest->crossed));
    printf(COLOR_RESET);
    cleanup_grid(forest);
    free(forest);
    return 0;
}
Example #4
0
bool init_grid(struct grid *grid, int row, int col)
{
  int i;
  int j;
  int x;
  int y;

  if (!fill_grid(grid, row, col))
    return false;
  for (i = 0; i < grid->x; i++)
    for (j = 0; j < grid->y; j++) {
        grid->cell[i][j].visible = false;
        grid->cell[i][j].flagged = false;
        grid->cell[i][j].value = 0;
    }
  for (i = 0; i < grid->mines; i++) {
    do {
      x = (int)rand() % grid->x;
      y = (int)rand() % grid->y;
    }
    while (grid->cell[x][y].value == -1);
    add_mine(grid, x, y);
  }
  return true;
}
Example #5
0
int			main(int argc, char **argv)
{
	int			nbr_tetris;
	char		**grid;
	t_tetrimino	*tetris;
	t_coord		co;
	t_path		path;

	path.square_size = -1;
	path.path = ft_strnew(27);
	co.x = 0;
	co.y = 0;
	nbr_tetris = -1;
	if (argc != 2 || !(is_valid_file(argv[1])))
		write(1, "error\n", 6);
	else if (!(nbr_tetris = process_file(argv[1], 0, &tetris)))
		write(1, "error\n", 6);
	else
	{
		grid = create_grid(nbr_tetris);
		path = fill_grid(grid, tetris, co, nbr_tetris);
		ft_strrev(path.path);
		grid_from_str(path.path, grid, tetris, co);
		display_grid(grid);
		free_grid(grid);
	}
	return (0);
}
Example #6
0
void show_transfer_window (void)
{

    GtkBuilder *gbuilder = gtk_builder_new();
    ex_builder_load_file (gbuilder, "gridform.ui");
    gtk_builder_connect_signals (gbuilder, NULL);

    window = GTK_WIDGET(gtk_builder_get_object (gbuilder, "window"));

    GtkContainer *scrolledwindow_grid = GTK_CONTAINER(gtk_builder_get_object (gbuilder, "scrolledwindow_grid"));

    button_del = GTK_BUTTON(gtk_builder_get_object (gbuilder, "button_del"));

    GtkWidget *button_add = GTK_WIDGET(gtk_builder_get_object (gbuilder, "button_add"));
    GtkWidget *button_edit = GTK_WIDGET(gtk_builder_get_object (gbuilder, "button_edit"));

    gtk_widget_set_visible (button_add, FALSE);
    gtk_widget_set_visible (button_edit, FALSE);

    grid = create_grid (scrolledwindow_grid);

    g_signal_connect (G_OBJECT(grid), "selection_changed", G_CALLBACK (on_grid_selection_changed), NULL);
    g_signal_connect (G_OBJECT(button_del), "clicked", G_CALLBACK (on_button_del_clicked), NULL);


    fill_grid();

    gtk_widget_show(window);
}
Example #7
0
void agrid2_fill (agrid2 pnt, float min1, float max1, float min2, float max2,
		  void (*fill)(float x, void* dat, float* f))
/*< Populate the grid. x is relative to the start of the grid >*/
{
    int i2;

    for (i2 = 0; i2 < pnt->n2; i2++) {
	fill_grid (pnt->ent[i2],min1,max1,min2,max2,&i2,fill);
    }
}
Example #8
0
/*
Affichage d'une grille générée
@params	p_widget	Elément ayant déclencher la fonction
@params	user_data	Données transmis au callback
*/
void cb_new_generate(GtkWidget *p_widget, gpointer user_data){
    //Génération
    int grid[9][9] = {{0}};
    generate(grid, (int)user_data);

    int grid_fixes[9][9] = {{0}};
    fill_grid(grid, grid_fixes); //Affichage

    gtk_widget_destroy(p_widget); //Fermeture dialog
}
Example #9
0
static void tick(int *len, char *buf, struct per_session_data *data) {
    int path_length = 0;
    path_t path;

    mask_t random_dots = EMPTY_MASK;

    json_object *result;
    const char *s;

    /* If it's a new game, only send the grid and don't compute a move. */
    if (data->new_game) {
        data->new_game = 0;
    } else {
        struct timeval tv;
        long ms;
        mask_t move;
        int no_moves;

        gettimeofday(&tv, NULL);
        ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
        if ((ms - data->last_updated) < MIN_UPDATE_INTERVAL) {
            return;
        }
        data->last_updated = ms;

        move = choose_move(data->grid, 0, 100, &no_moves);
        apply_move(data->grid, move);
        fill_grid(data->grid, GET_CYCLE_COLOR(move));
        if (no_moves) {
            random_dots = move;
        } else {
            mask_to_path(move, &path_length, path);
        }
    }

    result = json_object_new_object();
    json_object_object_add(result, "grid", json_grid(data->grid));
    if (path_length == 0 && random_dots == EMPTY_MASK) {
        json_object_object_add(result, "newGrid", json_object_new_boolean(TRUE));
    }
    if (random_dots) {
        json_object_object_add(result, "shrinkRandom", json_shrink_random(random_dots));
    }
    if (path_length) {
        json_object_object_add(result, "path", json_path(path_length, path));
    }

    s = json_object_to_json_string(result);
    *len = strlen(s);
    memcpy(buf, s, *len);
    buf[*len] = 0;

    json_object_put(result);
}
Example #10
0
void Community::sense_noisy_velocities(double* vel_sensed) {
    /*
     * If using grid, this fills the grid from scratch
     * at every iteration.
     */
    int num_neis ;
    Agent* neis  ;
    if (use_grid) {
        fill_grid() ;
        for(int i=0; i<num_agents; i++) {
            neis = grid->get_neighborhood(agents+i , &num_neis ) ;
            agents[i].sense_noisy_velocity(num_neis , neis , vel_sensed + i*DIM) ;
        }
    } else {
        for(int i=0; i<num_agents; i++)
            agents[i].sense_noisy_velocity(num_agents , agents , vel_sensed + i*DIM) ;
    }
}
Example #11
0
int		main(int argc, char *argv[])
{
	struct s_grid	*grid;

	if (ft_precheck(argc, argv) == 0)
	{
		ft_error();
		return (0);
	}
	grid = ft_create_grid(argv);
	if (fill_grid(grid, 0, 0) == 0)
	{
		ft_error();
		return (0);
	}
	ft_print_grid(grid);
	return (0);
}
Example #12
0
int surfw_res(PROT prot, int ir, float probe_rad)
{ int   i, j, k;

  PROBE_RAD  = probe_rad;       /* get parameters */
  grid_interval = PROBE_RAD + ATOM_RAD;
  num_pts = 122;

  set_vdw_rad(prot, PROBE_RAD);

  /* make the surface for each atom */
  for (j = 0; j < prot.res[ir].n_conf; j++) {
     for (i=0; i<prot.res[ir].n_conf; i++) {
        if (i==j) prot.res[ir].conf[i].on = 1;
        else prot.res[ir].conf[i].on = 0;
     }

     get_pdb_size(prot);
     extend_grid();
     calc_gsize();
     gindex.x = gsize.x - 1;
     gindex.y = gsize.y - 1;
     gindex.z = gsize.z - 1;
     alloc_3d_array(gsize.x, gsize.y, gsize.z);
     fill_grid(prot);

     for (k = 0; k < prot.res[ir].conf[j].n_atom; k++) {
        if (!(prot.res[ir].conf[j].atom[k].on)) continue;
        mkacc(&(prot.res[ir].conf[j].atom[k]));
     }
     free_3d_array(gsize.x, gsize.y, gsize.z);

  }

  /* turn off conformers in this residue */
  for (i=2; i<prot.res[ir].n_conf; i++) prot.res[ir].conf[i].on = 0;
  if (prot.res[ir].n_conf > 1) prot.res[ir].conf[1].on = 1;
  prot.res[ir].conf[0].on = 1;


  reset_atom_rad(prot, PROBE_RAD);

  return 0;

}
Example #13
0
/*
Affichage de la solution de la grille
@params	p_widget	Elément ayant déclencher la fonction
@params	user_data	Données transmis au callback
*/
void cb_resolve(GtkWidget *p_widget, gpointer user_data){
    int i,row,col;

    //On récupère la grille
    int grid[9][9] = {{0}};
    get_grid(grid);
    int grid_fixes[9][9] = {{0}};
    get_fixed_grid(grid_fixes);

    //On vérifit si grid_fixes est vide
    int emptyGridFixes = 1;
    for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        if(grid_fixes[col][row] != 0){emptyGridFixes = 0;break;}
    }

    if(!emptyGridFixes){
        //On récupère la base de la grille
        for(i=0;i<=80;i++){
            row = floor(i/9);
            col = i%9;
            if(grid_fixes[col][row] == 0){grid[col][row] = 0;}
        }
    }
    else{
        //On remplit grid_fixes(grille saisi par user)
        for(i=0;i<=80;i++){
            row = floor(i/9);
            col = i%9;
            if(grid[col][row] != 0){grid_fixes[col][row] = 1;}
        }
    }

    //On résout et on affiche
    if(is_grid_valid(grid)){
        resolve(grid, 0, 0);
        fill_grid(grid, grid_fixes);
    }

    (void)p_widget;
    (void)user_data;
}
Example #14
0
static void
fill_pattern(testpattern_t *p, unsigned char *data, size_t width,
	     size_t s_count, size_t image_depth, size_t byte_depth)
{
  memset(data, 0, global_printer_width * image_depth * byte_depth);
  switch (p->type)
    {
    case E_PATTERN:
      fill_colors(data, width, s_count, p, byte_depth);
      break;
    case E_XPATTERN:
      fill_colors_extended(data, width, s_count, p, byte_depth);
      break;
    case E_GRID:
      fill_grid(data, width, s_count, p, byte_depth);
      break;
    default:
      break;
    }
}
int main( int argc, char **argv )
{
  fftw_complex *in, *out;
  double *grid;
  fftw_plan p;
  FILE *fp = fopen("fftw.dat", "w");
  if( !fp )
    EXIT_WITH_PERROR("file open failed in main: ")

  grid = calloc( sizeof(double), N );
  in = fftw_malloc( sizeof(fftw_complex) * N );
  out = fftw_malloc( sizeof(fftw_complex) * N );

  if( !grid || !in || !out )
    EXIT_WITH_PERROR("malloc failed in main")


  fill_grid( grid, N, L );
  fftw_complex_fill_cosine( in, M );
  int i;
  for( i = M; i < N; i++ )
    in[i] = 0;

  //p = fftw_create_plan( N, FFTW_FORWARD, FFTW_ESTIMATE );
  p = fftw_plan_dft_1d( N, in, out, FFTW_FORWARD, FFTW_ESTIMATE );

  //fftw_one( p, in, out );
  fftw_execute( p );

  fftw_write_data( fp, out, grid, N, W_REAL );

  fftw_destroy_plan( p );
  fftw_free( in );
  fftw_free( out );
  free( grid );

  fclose( fp );

  return 0;
}
Example #16
0
int main(int argc,const char * argv[]) {
  //Robin's monitor.
  #ifdef ROOKS_MONITOR
  pthread_t t;
  #endif
  int max = 0;
  //DO NOT PUT 0 HERE ;)
  int len = 8;
  grid g;
  fill_grid(&g,len);
  #ifdef ROOKS_MONITOR
  pthread_create(&t, NULL, monitor, &g);
  #endif
  backtrack_corner(&g,&max);
  #ifdef ROOKS_MONITOR
  pthread_cancel(t);
  pthread_join(t,NULL);
  #endif
  printf("Result: %d\n",max);
  printf("Explored confs: %" PRIu64 "\n",conf_counter);
  free_grid(&g);
  return(0);
}
Example #17
0
/*
Nouvelle grille vide ou affichage difficulté
@params	p_widget	Elément ayant déclencher la fonction
@params	user_data	Données transmis au callback
*/
void cb_new(GtkWidget *p_widget, gpointer user_data){
    if((int)user_data == GTK_RESPONSE_ACCEPT){ //Grille vide
        int grid[9][9] = {{0}};
        int grid_fixes[9][9] = {{0}};
        fill_grid(grid, grid_fixes);
    }
    else if((int)user_data == GTK_RESPONSE_REJECT){ //Demande de difficulté
        GtkWidget *p_dialog = gtk_dialog_new_with_buttons(g_locale_to_utf8("Générer grille", -1, NULL, NULL, NULL),
                                                        GTK_WINDOW(widgetCallback.entry1),
                                                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                                        "Facile",
                                                        EASY_GRID,
                                                        "Moyen",
                                                        MEDIUM_GRID,
                                                        "Difficile",
                                                        HARD_GRID,
                                                        NULL);
        gtk_widget_show(p_dialog);

        g_signal_connect_swapped(p_dialog, "response", G_CALLBACK(cb_new_generate), p_dialog);
    }
    gtk_widget_destroy(p_widget);
}
Example #18
0
/* ---------------------------------------------------------------------- */
void
mexFunction(int nlhs,       mxArray *plhs[],
            int nrhs, const mxArray *prhs[])
/* ---------------------------------------------------------------------- */
{
    double                tolerance;
    char                  errmsg[1023 + 1];
    struct grdecl         grdecl;
    struct processed_grid g;

    if (args_ok(nlhs, nrhs, prhs)) {
        mx_init_grdecl(&grdecl, prhs[0]);
        tolerance = define_tolerance(nrhs, prhs);

        process_grdecl(&grdecl, tolerance, &g);

        plhs[0] = allocate_grid(&g, mexFunctionName());

        if (plhs[0] != NULL) {
            fill_grid(plhs[0], &g);
        } else {
            /* Failed to create grid structure.  Return empty. */
            plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
        }

        free_processed_grid(&g);
    } else {
        sprintf(errmsg,
                "Calling sequence is\n\t"
                "G = %s(grdecl)\t%%or\n\t"
                "G = %s(grdecl, tolerance)\n"
                "The 'grdecl' must be a valid structure with fields\n"
                "\t'cartDims', 'COORD', 'ZCORN'",
                mexFunctionName(), mexFunctionName());
        mexErrMsgTxt(errmsg);
    }
}
Example #19
0
int main(int argc, char* argv[])
{
    int nx, na, na2, ia, nz, order, maxsplit, ix, iz, *siz;
    float **place, *slow, **out, dx,dz, x0,z0, x[2];
    float max1, min1, max2, min2;
    bool isvel, lsint;
    agrid grd;
    sf_file vel, outp, size, grid;
        
    sf_init (argc,argv);

    /* get 2-D grid parameters */
    vel = sf_input("in");
    if (!sf_histint(vel,"n1",&nz)) sf_error("No n1= in input");
    if (!sf_histint(vel,"n2",&nx)) sf_error("No n2= in input");
    if (!sf_histfloat(vel,"d1",&dz)) sf_error("No d1= in input");
    if (!sf_histfloat(vel,"d2",&dx)) sf_error("No d2= in input");
    if (!sf_histfloat(vel,"o1",&z0)) z0=0.;
    if (!sf_histfloat(vel,"o2",&x0)) x0=0.;

    outp = sf_output("out");
    
    sf_putint(outp,"n4",nz);
    sf_putfloat(outp,"d4",dz);
    sf_putfloat(outp,"o4",z0);

    sf_putint(outp,"n3",nx);
    sf_putfloat(outp,"d3",dx);
    sf_putfloat(outp,"o3",x0);

    if (!sf_getint("na",&na)) na=60;
    /* number of angles */
    if (!sf_getfloat("da",&da)) da=3.1;
    /* angle increment (in degrees) */
    if (!sf_getfloat("a0",&a0)) a0=-90.;
    /* initial angle (in degrees) */

    if (!sf_getint("maxsplit",&maxsplit)) maxsplit=10;
    /* maximum splitting for adaptive grid */

    if (!sf_getfloat("minx",&min1)) min1=0.5*dx;
    /* parameters for adaptive grid */
    if (!sf_getfloat("maxx",&max1)) max1=2.*dx;
    if (!sf_getfloat("mina",&min2)) min2=0.5*da;
    if (!sf_getfloat("maxa",&max2)) max2=2.*da;

    sf_putint(outp,"n2",na);
    sf_putfloat(outp,"d2",da);
    sf_putfloat(outp,"o2",a0);

    da *= (SF_PI/180.);
    a0 *= (SF_PI/180.);

    sf_putint(outp,"n1",5);

    size = sf_output("size");
    sf_putint(size,"n1",nx);
    sf_putint(size,"n2",nz);
    sf_settype(size,SF_INT);

    grid = sf_output("grid");

    /* additional parameters */
    if(!sf_getbool("vel",&isvel)) isvel=true;
    /* y: velocity, n: slowness */
    if(!sf_getint("order",&order)) order=3;
    /* velocity interpolation order */
    if (!sf_getbool("lsint",&lsint)) lsint=false;
    /* if use least-squares interpolation */

    slow  = sf_floatalloc(nz*nx);
    place = sf_floatalloc2(5,na);
    siz  = sf_intalloc(nx);

    sf_floatread(slow,nx*nz,vel);

    if (isvel) {
      for(ix = 0; ix < nx*nz; ix++){
	slow[ix] = 1./slow[ix];
      }
    }

    ct = sf_celltrace_init (lsint, order, nz*nx, nz, nx, dz, dx, z0, x0, slow);
    free (slow);

    grd = agrid_init (na, 5, maxsplit);
    agrid_set (grd,place);

    for (iz = 0; iz < nz; iz++) {
	x[0] = z0+iz*dz;

	sf_warning("depth %d of %d;",iz+1, nz);
	for (ix = 0; ix < nx; ix++) {
	    x[1] = x0+ix*dx;

	    fill_grid(grd,min1,max1,min2,max2,(void*) x, raytrace);
	
	    na2 = grid_size (grd);
	    out = write_grid (grd);

	    siz[ix] = na2;
	
	    for (ia=0; ia < na2; ia++) {
	      if (ia < na)
		  sf_floatwrite (place[ia], 5, outp);

	      sf_floatwrite(out[ia], 5, grid);
	    }
	    free (out);
	}
	
	sf_intwrite (siz,nx,size);
    }
    sf_warning(".");

    exit (0);
}
Example #20
0
static void init_session_data(struct per_session_data *data) {
    data->new_game = 1;
    memset(data->grid, 0, sizeof(data->grid));
    fill_grid(data->grid, EMPTY);
}
Example #21
0
// Need to read, the total number of agents, line of chars, number of this agent, map dims, map
clientinfo_t* setup() {
    char line[80];
    uint agcount;
    uint agnum;
    struct sigaction sa;
    sa.sa_handler=pipehandler;
    sa.sa_flags=SA_RESTART;
    sigaction(SIGPIPE, 0, &sa);     
    if (!fgets(line, 80, stdin) || (line[strlen(line)-1]!='\n')) {
        return 0;
    } 
    if (!sscanf(line, "%u", &agcount)) {
        return 0;
    }
        // we can't use a fixed size buffer here because we don't know how many agents
    char* cs=malloc(sizeof(char)*(agcount+1));
    for (int i=0;i<agcount;++i) {
        cs[i]=fgetc(stdin);
	if (feof(stdin) || (cs[i]=='\n')) {
	    free(cs);
	    return 0;
	}
    }
    (void)getchar();	// to clear end of line char
    cs[agcount]='\0';	// to make it easier for debug printing
    if (!fgets(line, 80, stdin) || (line[strlen(line)-1]!='\n')) {
        free(cs);
        return 0;
    }

    if (!sscanf(line, "%u", &agnum)) {
        free(cs);
        return 0;
    }
    agnum-=1;	// to make array lookups easier
    if ((agnum<0) || (agnum>=agcount)) {
        free(cs);
	return 0;
    }
    if (!fgets(line, 80, stdin) || (line[strlen(line)-1]!='\n')) {
        free(cs);
        return 0;
    }
    uint rows, cols;
    if (sscanf(line,"%u %u", &rows, &cols)!=2) {
        free(cs);
        return 0;
    }
    map_t* map=alloc_map(rows, cols);
    if (fill_grid(map, stdin, ' ')) {
        free_map(map);
	free(cs);
        return 0;
    }
    // now we have all the parts
    clientinfo_t* client=malloc(sizeof(clientinfo_t));
    client->agcount=agcount;
    client->agnum=agnum;
    client->chars=cs;
    client->map=map;
    client->pos=malloc(sizeof(uint)*2*agcount);
    return client;
}