Exemplo n.º 1
0
void usersolve() {
    x=5;
    y=6;
    ox=x;
    oy=y;
    steps=0;
    r=1;
    m=0;
    while((x!=width-3) && (button==0)) {
        expose();
        XQueryPointer(display,main_win,&root,&win,&root_x,&root_y,&win_x,&win_y,&mask);
        while(ox!=x || oy!=y) {
            steps++;
            pset(ox,oy,7);
            px[m]=ox;
            py[m]=oy;
            if(m<maxdepth) m++;
            sprintf(status," Steps: %d",steps);
            showstatus(0);
            if(ox!=x) ox=x;
            else oy=y;
        }
        pset(ox,oy,7);
        ox=x;
        oy=y;
        pset(x,y,(fg+1)&31);
        if((x>win_x/mag) && (ppoint(x-1,y)!=bg)) x--;
        if((x<win_x/mag) && (ppoint(x+1,y)!=bg)) x++;
        if((y>win_y/mag) && (ppoint(x,y-1)!=bg)) y--;
        if((y<win_y/mag) && (ppoint(x,y+1)!=bg)) y++;
        nap(50);
    }
    if(x==width-3) {
        code=rand();
        sprintf(status," %d Steps! Code:%04X ",steps,code);
        showstatus(0);
        i=0;
        c=1;
        button=0;
        while(button==0) {
            pset(ox,oy,c);
            ox=px[i];
            oy=py[i];
            pset(ox,oy,0);
            i++;
            if(i>=m) {
                i=0;
                c=(c+1)&31;
                if(c==bg) c=(c+1)&31;
            }
            expose();
            nap(20);
        }
    }
    waitnobutton();
}
Exemplo n.º 2
0
int solvefrom(int x, int y) {
    int i;
    static int solved=0;
    static int solvedepth=0;
    if(solved) return(1);
    solvedepth++;
    if(solvedepth>h) h=solvedepth;
    pset(x,y,fg);
    sprintf(status," %d,%d Depth:%d  ",x,y,solvedepth);
    showstatus(1);
    if(x>=(width-4)) {
        solved=1;
        sprintf(status,"Solved! Depth:%d ",solvedepth);
        showstatus(0);
        return(1);
    }
    for(i=0; i<4; i++) {
        if(ppoint(x+sx[i]/2,y+sy[i]/2)==(fg^31)) {
            if(solvefrom(x+sx[i]/2,y+sy[i]/2)) {
                if(button==0 && solved==0) nap(delay);
                solvedepth--;
                return(1);
            }
        }
    }
    pset(x,y,fg^31);
    if(button==0) nap(delay);
    solvedepth--;
    return(0);
}
Exemplo n.º 3
0
void cpusolve(void) {
    h=0;
    sprintf(status,"Clearing maze...");
    showstatus(0);
    for(x=4; x<width-3; x++) {
        for(y=4; y<height-3; y++) {
            if(ppoint(x,y)!=bg) pset(x,y,fg^31);
        }
    }
    x=5;
    y=6;
    r=solvefrom(x,y);
    sprintf(status,"Solution depth: %d",h);
    showstatus(0);
}
Exemplo n.º 4
0
static void set_rgn(double *msc, char *name, char *name1, char *name2)
{
    char reg_name[20];
    int x0, y0, xp, yp, *x, *y, xstart, ystart, btn, d, method, meth;
    static int pts, rgn_cnt = 0;
    double dtmp, etmp;
    FILE *tmp;
    char *tempfile;

    /* get the name of the regions map */

    if (!G_ask_cell_new("    ENTER THE NEW REGION MAP NAME:", reg_name))
	return;

    /* allocate memory for storing the
       points along the boundary of each
       region */

    x = (int *)G_malloc(100 * sizeof(int));
    y = (int *)G_malloc(100 * sizeof(int));

    tempfile = G_tempfile();
    tmp = fopen(tempfile, "w");

  back2:
    G_system("clear");
    fprintf(stderr, "\n\n    CHOOSE AN OPTION:\n\n");
    fprintf(stderr, "       Draw a region                     1\n");
    fprintf(stderr, "       Quit drawing regions and return");
    fprintf(stderr, "\n          to setup options menu          2\n");
    fprintf(stderr, "       Change the color for drawing      3\n\n");

    do {
	fprintf(stderr, "                             Which Number?   ");
	numtrap(1, &etmp);
	if ((meth = fabs(etmp)) > 3 || meth < 1) {
	    fprintf(stderr, "\n    Choice must between 1-3; try again");
	}
    }
    while (meth > 3 || meth < 1);

    if (meth == 2)
	return;
    if (meth == 3) {
	R_open_driver();
	change_draw();
    }
    if (meth == 1) {
	R_open_driver();
	rgn_cnt = 0;
    }

    /* ask the user to outline a region */

  back:
    G_system("clear");
    ppoint(NULL, 0, 0, -1);
    fprintf(stderr, "\n    PLEASE OUTLINE REGION # %d\n", (++rgn_cnt));
    pbutton(0);
    pts = 0;
    x0 = 0;
    y0 = 0;

    /* get the points along the boundary
       of the region as they are drawn */

    do {
	btn = 0;
	R_get_location_with_line(x0, y0, &xp, &yp, &btn);
	if (btn == 1)
	    ppoint(msc, xp, yp, 0);
	else if (btn == 2) {
	    if (!pts) {
		pbutton(1);
		R_move_abs(xp, yp);
		xstart = xp;
		ystart = yp;
	    }
	    x[pts] = xp * msc[0];
	    y[pts] = yp * msc[1];
	    ppoint(msc, xp, yp, (++pts));
	    x0 = xp;
	    y0 = yp;
	    R_cont_abs(x0, y0);
	}
	else if (btn == 3 && pts < 3) {
	    fprintf(stderr,
		    "\n\n    Please digitize more than 2 boundary points\n\n");
	}
    }
    while (btn != 3 || pts < 3);

    R_cont_abs(xstart, ystart);
    R_close_driver();
    R_open_driver();
    x[pts] = x[0];
    y[pts] = y[0];
    pts++;

    /* redisplay the menu and find out what
       to do next */
  back1:
    G_system("clear");
    fprintf(stderr, "\n\n    CHOOSE AN OPTION:\n\n");
    fprintf(stderr,
	    "       Draw another region                          1\n");
    fprintf(stderr,
	    "       Start over drawing regions                   2\n");
    fprintf(stderr,
	    "       Quit drawing and save the region map         3\n");
    fprintf(stderr,
	    "       Quit drawing and don't save the region map   4\n");
    fprintf(stderr,
	    "       Change the color for drawing                 5\n\n");
    do {
	fprintf(stderr,
		"                                        Which Number?  ");
	numtrap(1, &dtmp);
	if ((method = fabs(dtmp)) > 5 || method < 1) {
	    fprintf(stderr, "\n    Choice must between 1-5; try again");
	}
    }
    while (method > 5 || method < 1);


    /* save the region and draw another */

    if (method == 1) {
	save_rgn(reg_name, tempfile, tmp, x, y, pts, rgn_cnt, 1);
	goto back;
    }

    /* start over */

    else if (method == 2) {
	fclose(tmp);
	if (!(tmp = fopen(tempfile, "w")))
	    G_fatal_error
		("Can't open temporary file for storing region info\n");
	rgn_cnt = 0;
	R_close_driver();
	paint_map(name, name1, name2);
	goto back2;
    }

    /* save the region and exit */

    else if (method == 3)
	save_rgn(reg_name, tempfile, tmp, x, y, pts, rgn_cnt, 2);


    /* change the color for drawing */

    else if (method == 5) {
	change_draw();
	goto back1;
    }

    R_close_driver();
    G_free(x);
    G_free(y);
    unlink(tempfile);
    return;

}
Exemplo n.º 5
0
void makemaze(void) {
    segments=0;
    h=0;
    x=6+((rand()%(width-10))&32766);
    y=6+((rand()%(height-10))&32766);
    ox=x;
    oy=y;
    t=flatness;
    d=1;
    m=0;
    px[m]=x;
    py[m]=y;
    m++;
    start=time(0);
    for(i=0; i<chldmax; i++) {
        chldd[i]=-1;
    }
    while(m>=0) {
        for(i=0; i<chldmax; i++) {
            if(chldd[i]!=-1) {
                xx1=chldx[i]+sx[chldd[i]];
                yy1=chldy[i]+sy[chldd[i]];
                if(ppoint(xx1,yy1)==bg) {
                    px[m]=chldx[i];
                    py[m]=chldy[i];
                    m++;
                    line(chldx[i],chldy[i],xx1,yy1,fg);
                    segments++;
                    chldx[i]=xx1;
                    chldy[i]=yy1;
                    if((rand()%(flatness+1))==0) chldd[i]=rand()%4;
                } else {
                    c=0;
                    for(j=0; j<4; j++) {
                        if(ppoint(chldx[i]+sx[j],chldy[i]+sy[j])==bg) c++;
                    }
                    if(c==0) {
                        chldd[i]=-1;
                    } else {
                        chldd[i]=rand()%4;
                    }
                }
            }
        }
        if(statusline) {
            sprintf(status," %d %%%d  ",segments,segments*100/targetsegments);
            showstatus(1);
        }
        c=0;
        for(i=0; i<4; i++) {
            r=ppoint(x+sx[i],y+sy[i]);
            if(r==bg) {
                n[c]=i;
                c++;
            }
        }
        if(c==0) {
            x=px[m-1];
            y=py[m-1];
            m--;
            ox=x;
            oy=y;
        } else {
            if(delay) if (button==0) nap(delay);
            if(ppoint(x+sx[d],y+sy[d])!=bg) {
                d=n[rand()%c];
                while(c) {
                    for(i=0; i<chldmax; i++) {
                        if(chldd[i]== -1) {
                            chldx[i]=x;
                            chldy[i]=y;
                            chldd[i]=rand()%4;
                            break;
                        }
                    }
                    c--;
                }
            } else {
                xx1=x;
                yy1=y;
                x=x+sx[d];
                y=y+sy[d];
                line(x,y,xx1,yy1,fg);
                px[m]=x;
                py[m]=y;
                m++;
                if(m>h) h=m;
                segments++;
                t--;
                if(t<0) {
                    t=(rand()%(flatness+1));
                    d=rand()%4;
                }
            }
        }
    }
    end=time(0);
    for(i=1; i<4; i++) {
        box(i,i,width-i-1,height-i-1,fg);
    }
    box(4,4,width-4,height-4,bg);
    pset(5,6,fg);
    line(width-2,height-6,width-6,height-6,fg);
    sprintf(status," T:%d D:%d S:%d",(int)(end-start),h,segments);
    showstatus(1);
}