| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 559 人关注过本帖
标题:C语言迷宫问题,紧急,谢谢进来帮忙的大侠
只看楼主 加入收藏
红色火焰
Rank: 2
等 级:论坛游民
帖 子:4
专家分:20
注 册:2010-5-9
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
C语言迷宫问题,紧急,谢谢进来帮忙的大侠
/*MAZE*/
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <bios.h>
#define N 22
#define M 22

int maze[M][N];
void drawmat(char *,int,int,int,int);
void welcome();
void operationInstruction();
void quit();
void autoRun();
void finish();
void select();
void game();
void makeMaze(int,int);
void drawMaze(int[][],int,int,int,int,int);
void drawCircle(int,int,int);
void rect(int,int,int,int);
void endFace();

char li16S[]={
  0x01,0x00,0x01,0x00,0x7F,0xFC,0x03,0x80,
  0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,
  0x0F,0xE0,0x00,0x40,0x00,0x80,0xFF,0xFE,
  0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,
};

char jin16S[]={
  0x01,0x00,0x01,0x00,0x02,0x80,0x04,0x40,
  0x08,0x20,0x10,0x10,0x2F,0xE8,0xC1,0x06,
  0x01,0x00,0x3F,0xF8,0x01,0x00,0x11,0x10,
  0x09,0x10,0x09,0x20,0xFF,0xFE,0x00,0x00,
};

char song16S[]={
  0x02,0x08,0x21,0x08,0x11,0x10,0x17,0xFC,
  0x00,0x40,0x00,0x40,0xF0,0x40,0x17,0xFE,
  0x10,0x40,0x10,0xA0,0x10,0x90,0x11,0x08,
  0x12,0x08,0x28,0x00,0x47,0xFE,0x00,0x00,
};

char xu16S[]={
  0x00,0x80,0x20,0x80,0x10,0x80,0x11,0xFC,
  0x01,0x20,0x02,0x20,0xF0,0x20,0x10,0x20,
  0x13,0xFE,0x10,0x20,0x10,0x20,0x10,0x20,
  0x14,0x20,0x18,0x20,0x10,0x20,0x00,0x20,
};

char xiao16S[]={
  0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
  0x01,0x00,0x11,0x10,0x11,0x08,0x11,0x04,
  0x21,0x04,0x21,0x02,0x41,0x02,0x81,0x02,
  0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,
};

char ming16S[]={
  0x00,0x00,0x00,0xFC,0x7C,0x84,0x44,0x84,
  0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,
  0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,
  0x01,0x04,0x01,0x04,0x02,0x14,0x04,0x08,
};

char liu16S[]={
  0x10,0x04,0x08,0x04,0x08,0x04,0xFF,0xA4,
  0x02,0x24,0x42,0x24,0x22,0x24,0x14,0x24,
  0x14,0x24,0x08,0x24,0x08,0x24,0x14,0x24,
  0x24,0x04,0x42,0x04,0x82,0x14,0x00,0x08,
};

char wen16S[]={
  0x02,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,
  0x10,0x10,0x10,0x10,0x08,0x20,0x08,0x20,
  0x04,0x40,0x02,0x80,0x01,0x00,0x02,0x80,
  0x04,0x40,0x08,0x20,0x30,0x18,0xC0,0x06,
};

char jie16S[]={
  0x20,0x40,0x20,0x40,0x27,0xFE,0x20,0x40,
  0xFB,0xF8,0x20,0x48,0x27,0xFE,0x28,0x48,
  0x33,0xF8,0xE0,0x40,0x22,0x40,0x22,0x7C,
  0x22,0x40,0x25,0x40,0xA4,0xFE,0x48,0x00,
};

void main()
{
    int gdriver=DETECT,gmode;
    registerbgidriver(EGAVGA_driver);
    initgraph(&gdriver,&gmode,"");
    welcome();
    game();  /*start the game*/
    getch();
    return;
}

void drawmat(char *mat,int matsize,int x,int y,int color)
{
    int i, j, k, n;
    n = (matsize - 1) / 8 + 1;
    for(j = 0; j < matsize; j++)
        for(i = 0; i < n; i++)
            for(k = 0;k < 8; k++)
                if(mat[j * n + i] & (0x80 >> k))
                    putpixel(x + i * 8 + k, y + j, color);
    return;
}

void welcome()
{
    int i;
    char str1[]="MAZE";
    setbkcolor(13);
    setcolor(BLUE);
    settextstyle(0,0,2);
    outtextxy(100+50,100,"This");
    delay(100);
    outtextxy(100+130,100,"is");
    delay(100);
    outtextxy(100+190,100,"our");
    delay(100);
    outtextxy(100+260,100,"game!");
    delay(100);                          /*delay 100 milliseconds*/

    settextstyle(0,0,2);
    outtextxy(250,150,str1);
    delay(100);

    setcolor(WHITE);
    settextstyle(0,0,2);
    outtextxy(150,250,"Our");
    delay(100);
    outtextxy(220,250,"group");
    delay(100);
    outtextxy(320,250,"members:");
    delay(100);

    drawmat(li16S,16,150,300,WHITE);
    delay(100);
    drawmat(jin16S,16,150,350,WHITE);
    delay(100);
    drawmat(song16S,16,150,400,WHITE);
    delay(100);

    drawmat(xu16S,16,275,300,WHITE);
    delay(100);
    drawmat(xiao16S,16,275,350,WHITE);
    delay(100);
    drawmat(ming16S,16,275,400,WHITE);
    delay(100);

    drawmat(liu16S,16,400,300,WHITE);
    delay(100);
    drawmat(wen16S,16,400,350,WHITE);
    delay(100);
    drawmat(jie16S,16,400,400,WHITE);
    delay(100);
    for(i=0;i<=45;i+=5)
    {
        setcolor(i); /*set diffent color*/
        rectangle(0+i,0+i,630-i,479-i); /*draw many rectangle of different radius*/
        delay(100);  /*delay 100 milliseconds to show every rectangle*/
    }
    setcolor(1);
    settextstyle(0,0,1);
    outtextxy(350,430,"Press any key to continue.....");
    getch();
    cleardevice();
    return;

}

void game()
{

    int direc;
    char ch,ch2,ch3;
    int step=20,len=10,x,y,row,col; /*each moved length*/
    Startgame:
        x=0,y=0,row=0,col=0;  /*x,y are circle's center coordinates*/
        makeMaze(M,N); /*make maze map by random*/
        cleardevice();
        setbkcolor(LIGHTBLUE);
        setwritemode(XOR_PUT); /*exclusive or*/
        settextstyle(1,0,1);
        setcolor(YELLOW);  /*set maze's color*/
        setfillstyle(XHATCH_FILL,YELLOW);
        drawMaze(maze,M,N,step,0,0);
        setcolor(WHITE); /*set circle's color*/
        x+=len;y+=len;
        drawCircle(x,y,len);
        select();
        while(1)
        {
            ch=getch();
            if(ch=='M'||ch=='m')
            {
            /* user control*/
                setfillstyle(SOLID_FILL,LIGHTBLUE);
                bar(getmaxx()-180,0,getmaxx(),getmaxy());

                operationInstruction();
                while(1)
                {
                    ch=getch();
                    setfillstyle(SOLID_FILL,LIGHTBLUE);
                    bar(x+len-3,y-len+3,x-len+3,y+len-3);
                    if(ch=='Q'||ch=='q')
                    {
                    /*if you press 'q',the game will end*/
                        quit();
                        while(1)
                        {
                            ch2=getch();
                            if(ch2=='Y'||ch2=='y')
                            {
                                endFace();
                                closegraph();/*if you press 'y',the game will end*/
                                clrscr();
                                exit(0);  /*exit mormally*/
                            }
                            else if(ch2=='N'||ch2=='n')  /*if you press 'n',the game will restart*/
                                goto Startgame;

                        }


                    }
                    switch(ch)
                    {
                        case 'a':
                        case 'A': /*press 'a',walk one step towards left*/
                            if(col>0&&maze[row][col-1]==0)
                            {
                                x-=step;
                                col--;
                            }
                            break;
                        case 's':
                        case 'S':  /*if you press 's',you will walk a step towards down*/
                            if(row<M-1&&maze[row+1][col]==0)
                            {
                                y+=step;
                                row++;
                            }
                            break;
                        case 'd':   /*if you press 'd',you will walk a step towards right*/
                        case 'D':
                            if(col<N-1&&maze[row][col+1]==0)
                            {
                                x+=step;
                                col++;
                            }
                            break;
                        case 'w':
                        case 'W':  /*if you press 'w',you will walk a step towards up*/
                            if(row>0&&maze[row-1][col]==0)
                            {
                                y-=step;
                                row--;
                            }
                            break;
                        case 'R':
                        case 'r':
                            goto Startgame;
                        default :   /*if you press another key, the man can't move*/
                            break;
                    }
                    drawCircle(x,y,len);
                    delay(50);
                    if(row==M-1&&col==N-1)
                    {
                        cleardevice();
                        setfillstyle(SOLID_FILL,LIGHTBLUE);
                        floodfill(480,370,LIGHTBLUE);
                        settextstyle(0,0,3);
                        setcolor(WHITE);
                        outtextxy(getmaxx()/2-200,getmaxy()/2-100,"You won the game!");
                        outtextxy(getmaxx()/2-200,getmaxy()/2,"Press Q to quit...");
                        outtextxy(getmaxx()/2-200,getmaxy()/2+100,"Press R to restart..");
                        while(1)
                        {
                            ch2=getch();
                            if(ch2=='Q'||ch2=='q') /*press 'q' to quit*/
                                break;
                            if(ch2=='R'||ch2=='r') /*press 'r' to restart game*/
                                goto Startgame;
                        }
                        getch();
                        closegraph();
                        clrscr();
                        exit(0);
                    }
                }
            
            }/* end of user control*/
        
            else if(ch=='C'||ch=='c')
            {
            /* computer control */
            /*direct means the direction of last step and next step*/
            /* 0 to 3 mean west,north,east,south*/
                autoRun();
                direc=2; /*initialize direction*/
                row=col=0;
                while(row<M-1||col<N-1)
                {
                    delay(250);
                    switch(direc)
                    {
                        case 0: /*west*/
                            /*take the order of 3,0,1 to try,if find a way,you can run,regardless can cross or not*/
                            if(row<M-1&&maze[row+1][col]==0)
                            {
                                y+=step;
                                row++; /*down*/
                                direc=3;
                                drawCircle(x,y,len);
                            }
                            else if(col>0&&maze[row][col-1]==0)
                            {
                                x-=step;
                                col--; /*left*/
                                direc=0;
                                drawCircle(x,y,len);

                            }
                            else if(row>0&&maze[row-1][col]==0)
                            {
                                y-=step;
                                row--; /*up*/
                                direc=1;
                                drawCircle(x,y,len);

                            }
                            else
                            {
                                maze[row][col]=1;
                                setfillstyle(SOLID_FILL,LIGHTBLUE);
                                bar(x+len-3,y-len+3,x-len+3,y+len-3);
                                x+=step;
                                col++; /*right*/
                                direc=2;
                                drawCircle(x,y,len);
                            }
                            break;
                        case 1: /*north*/
                            if(col>0&&maze[row][col-1]==0)
                            {
                                x-=step;/*left*/
                                col--;
                                direc=0;
                                drawCircle(x,y,len);

                            }
                            else if(row>0&&maze[row-1][col]==0)
                            {
                                y-=step;/*up*/
                                row--;
                                direc=1;
                                drawCircle(x,y,len);

                            }
                            else if(col<N-1&&maze[row][col+1]==0)
                            {
                                x+=step;/*right*/
                                col++;
                                direc=2;
                                drawCircle(x,y,len);

                            }
                            else
                            {
                                maze[row][col]=1;
                                setfillstyle(SOLID_FILL,LIGHTBLUE);
                                bar(x+len-3,y-len+3,x-len+3,y+len-3);
                                y+=step; /*down*/
                                row++;
                                direc=3;
                                drawCircle(x,y,len);
                            }
                            break;
                        case 2: /*east*/
                            if(row>0&&maze[row-1][col]==0)
                            {
                                y-=step;
                                row--; /*up*/
                                direc=1;
                                drawCircle(x,y,len);

                            }
                            else if(col<N-1&&maze[row][col+1]==0)
                            {
                                x+=step;
                                col++; /*right*/
                                direc=2;
                                drawCircle(x,y,len);

                            }
                            else if(row<M-1&&maze[row+1][col]==0)
                            {
                                y+=step;
                                row++; /*down*/
                                direc=3;
                                drawCircle(x,y,len);

                            }
                            else
                            {
                                maze[row][col]=1;
                                setfillstyle(SOLID_FILL,LIGHTBLUE);
                                bar(x+len-3,y-len+3,x-len+3,y+len-3);
                                x-=step;
                                col--; /*left*/
                                direc=0;
                                drawCircle(x,y,len);
                            }
                            break;
                        case 3: /*south*/
                            if(col<N-1&&maze[row][col+1]==0)
                            {
                                x+=step;/*right*/
                                col++;
                                direc=2;
                                drawCircle(x,y,len);

                            }
                            else if(row<M-1&&maze[row+1][col]==0)
                            {
                                y+=step;/*down*/
                                row++;
                                direc=3;
                                drawCircle(x,y,len);

                            }
                            else if(col>0&&maze[row][col-1]==0)
                            {
                                x-=step;/*left*/
                                col--;
                                direc=0;
                                drawCircle(x,y,len);

                            }
                            else
                            {
                                maze[row][col]=1;
                                setfillstyle(SOLID_FILL,LIGHTBLUE);
                                bar(x+len-3,y-len+3,x-len+3,y+len-3);
                                y-=step;/*up*/
                                row--;
                                direc=1;
                                drawCircle(x,y,len);
                            }
                            break;
                        default :
                            break;
                    }
                }
                setfillstyle(SOLID_FILL,LIGHTBLUE);
                bar(getmaxx()-180,0,getmaxx(),getmaxy());
                finish();
                ch3=getch();
                if(ch3=='q'||ch3=='Q')
                {
                    endFace();
                    closegraph();/*close graphics mode*/
                    clrscr();
                    exit(0);
                }
                else if(ch3=='R'||ch3=='r')
                    goto Startgame;

            }/*The end of computer controller*/
            else if(ch=='R'||ch=='r')
                goto Startgame;
    }
    return;
}

void drawCircle(int x,int y,int len)/*draw s circle remard to a man*/
{
    int r=len/2;
    setcolor(WHITE);
    circle(x,y,r);
    circle(x,y,r-2);
    return;
}

/* draw maze map*/
void drawMaze(int maze[][N],int a,int b,int step,int x,int y)
{
    int startx=x;
    int row,col;
    for(row=0;row<a;row++)
    {
        for(col=0;col<b;col++)
        {
            if(maze[row][col]==1)
                rect(x,y,x+step-1,y+step-1);/*draw every hinder*/
            x+=step;
        }
        x=startx;
        y+=step;
    }
    rectangle(0,0,step*b,step*a); /*draw maze's frame*/
    line(0,0,step,0);line(0,0,0,step);
    line(step*b,step*(a-1),step*b,step*a);
    line(step*(b-1),step*a,step*b,step*a);
    return;
}

void rect(int x0,int y0,int x1,int y1) /*draw one of hinder*/
{
    rectangle(x0,y0,x1,y1);
    floodfill((x0+x1)/2,(y0+y1)/2,YELLOW);
    return;

}

void makeMaze(int a,int b)/*create a array by random*/
{
    int row,col;
    int ran;
    int direc;
/* initialize maze map*/
    for(row=0;row<a;row++)
        for(col=0;col<b;col++)
            maze[row][col]=1;
    randomize();
    row=col=0;direc=2;
    while(1)
    {
        maze[row][col]=0;
        if(row>=M-1&&col>=N-1)
            break;
        ran=(int)rand()*4;
        if(ran<1)
        {
            if(direc!=1&&row<a-1)
            {
                row++;
                direc=3;
            }
        }   
        else if(ran<2)
        {
            if(direc!=2&&col>0)
            {
                col--;
                direc=0;
            }
        }
        else if(ran<3)
        {
            if(direc!=3&&row>0)
            {
                row--;
                direc=1;
            }
        }
        else
        {
            if(direc!=0&&col<b-1)
            {
                col++;
                direc=2;            
            }
        }
     }
    /*creat road that can cross by random*/
    for(row=0;row<a;row++)
        for(col=0;col<b;col++)
            if(maze[row][col]==1)
            {
                ran=(int)rand()*10;
                if(ran<7)
                    maze[row][col]=0;
            }
    return;
}
void operationInstruction() /*let user know how to control by keybord*/
{

    setlinestyle(0,0,3);
    setcolor(RED);
    line(490,100,495,105);
    line(540,40,545,45);
    line(540,140,535,135);
    line(540,140,545,135);
    line(540,40,535,45);
    line(490,100,495,95);
    line(590,100,585,95);
    line(590,100,585,105);
    line(540,40,540,140);
    line(490,100,590,100);
    settextstyle(0,0,2);
    setcolor(WHITE);
    outtextxy(470,95,"A");
    outtextxy(600,95,"D");
    outtextxy(533,20,"W");
    outtextxy(533,150,"S");
    gotoxy(60,16);
    printf("Q-Quit");
    gotoxy(60,18);
    printf("R-Restart");
    return;
}
void quit()
{
    gotoxy(60,18);
    printf("You have press");
    gotoxy(60,19);
    printf("Q key! Do you");
    gotoxy(60,20);
    printf("Want to quit?");
    gotoxy(60,21);
    printf("OK?[Y/N]");
    return;
}
void autoRun()/*let user know now is computer control*/
{
     gotoxy(60,3);
     printf("Computer now");
     gotoxy(60,6);
     printf("Run the maze");
     gotoxy(60,9);
     printf("Automatically...");
     return;
}
void finish() /* the game end ,you can press Q to quit*/
{
     gotoxy(60,12);
     printf("Finished!");
     gotoxy(60,15);
     printf("Press R to restart");
     gotoxy(60,18);
     printf("Press Q to quit game");
     return;
}
void select() /*if you press 'c',game start by computer */
{             /*if you press 'm',game start by user*/
    settextstyle(1,0,0);
    gotoxy(60,3);
    printf("M-by You");
    gotoxy(60,6);
    printf("C-by Computer");
    gotoxy(60,9);
    printf("R-Restart");
    return;
}

void endFace()
{   cleardevice();
    setbkcolor(LIGHTBLUE);
    setcolor(WHITE);
    settextstyle(0,0,3);
    outtextxy(getmaxx()/2-100,getmaxy()/2-50,"Thank you!");
    outtextxy(getmaxx()/2-100,getmaxy()/2,"The end!");
    sleep(3);
    return;
}

我想1.把全局变量给改掉,2.并且game()函数给分成两部分,一部分是manControl()另一部分是computerControl(),还有3.能不能给我讲解一下makeMaze()当中具体是如何生成一定有通路的迷宫数组的,如果嫌麻烦的话就只帮忙第一点和第三点就可以了,同样万分感谢!
搜索更多相关主题的帖子: 迷宫 C语言 紧急 进来 
2010-06-07 13:22
a392271275
Rank: 2
等 级:论坛游民
帖 子:12
专家分:32
注 册:2010-5-17
收藏
得分:10 
- - 看着这么多的程序我就晕了··
2010-06-07 14:47
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:10 
全局变量可以适当的用传参的方式解决,把函数分开也一样,不用全局变量,就只能用参数传递,这个有时会感觉很麻烦,必要时全局变量也是可以小用的,
2010-06-07 20:19
快速回复:C语言迷宫问题,紧急,谢谢进来帮忙的大侠
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026395 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved