C语言迷宫程序改错

huaxiamoyun 2010-03-14 11:12:20
很高兴你来帮忙解决问题
本人真实无奈了
怎么看都看不出毛病在哪儿
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct arr)
#define pr printf
#define RE return
#define BR break;
#define CA case
int in1,in2,out1,out2,x,y;
struct arr
{
int a;int b;int c;int d;int e;
int f;int g;int h;int i;int j;
int k;int l;
struct arr *next;
};
struct road
{
int a;int b;int c;
struct road *next;
};
struct arr *crate(int a,int b);
void inout(int,int,int,int,struct arr *);
void prmg(struct arr *head,int b);
struct road *walk(struct arr *,int,int,int);
int check(struct arr *p,int a);
struct road *concl(int a,int b,int c);
void outroad(struct road *head);
void eval(struct arr *p,int a,int b);
void main()
{
struct arr *head;
struct road *headc;
int a,b;
char flog='Y',m,n;
while(flog=='Y')
{
int q=1;
pr("Now,let's crate a maze!\n");
pr("Input the row and rank of the maze:\nas(a,b),a>6 and b<=10\n");
scanf("%c%d,%d%c",&m,&a,&b,&n);
head=crate(a,b);
pr("Please set the entrance and exit!\n");
pr("Input the entrance as (a,b) 0<a<%d,0<b<%d\n",a+1,b+1);
do{
if(q!=1)pr("Wrong!Input again!\n");
scanf("%c%d,%d%c",&m,&in1,&in2,&n);
++q;
}while(in1<=0||in1>b+1||in2<=0||in2>a+1);
q=1;
pr("Input the exit as (a,b) 0<a<%d,0<b<%d\n",a+1,b+1);
do{
if(q!=1)pr("Wrong!Input again!\n");
scanf("%c%d,%d%c",&m,&out1,&out2,&n);
++q;
}while(out1<=0||out1>b+1||out2<=0||out2>a+1);
inout(in1,in2,out1,out2,head);
pr("The maze as follows:\n");
prmg(head,b);
headc=walk(head,in1,in2,0);
if(headc==NULL)pr("There is no road!\n");
else
{
pr("The road is as follows:\n");
outroad(headc);
}
pr("Do you want to try again?(Y or N)\n");
flog=getchar();
}
}
struct arr *crate(int a,int b)
{
int i,j;
struct arr *head,*p1,*p2;
head=p1=p2=(struct arr *)malloc(LEN);
for(j=0;j<a+2;j++)
{
if(j==0)for(i=0;i<b+2;i++)eval(p1,i,1);
else if(j>0&&j<a+1)
{
p1=(struct arr *)malloc(LEN);
pr("input %d number!0 or 1\n",b);
for(i=0;i<b+2;i++)
{
int j;
if(i==0)eval(p1,i,1);
else if(i==b+1)eval(p1,i,1);
else
{
scanf("%d",&j);
eval(p1,i,j);
}
}
p2->next=p1;
p2=p1;
}
else
{
p1=(struct arr *)malloc(LEN);
for(i=0;i<b+2;i++)eval(p1,i,1);
p2->next=p1;
p1->next=NULL;
}
}
return(head);
}
void inout(int in1,int in2,int out1,int out2,struct arr *head)
{
int i;
struct arr *p1=head,*p2;
p2=p1;
for(i=0;i<in1;i++)
{
p1=p2->next;
p2=p1;
}
eval(p1,in2,0);
p1=p2=head;
for(i=0;i<out1;i++)
{
p1=p2->next;
p2=p1;
}
eval(p1,out2,0);
}
void prmg(struct arr *head,int b)
{
int i,j;
struct arr *p=head;
do{
for(i=0;i<b+2;i++)
pr("%2d",j=check(p,i));
pr("\n");
p=p->next;
}
while(p!=NULL);
}
struct road *walk(struct arr *head,int n1,int n2,int d)
{
int d1,i;
struct arr *p1,*p2;
p2=p1=head;
struct road *headc=NULL,*pc;
x=1;
for(i=0;i<n1-1;i++)
{
p1=p2->next;
p2=p1;
}
if(check(p1,n1)==0)
{
d1=1;
if(x==1)headc=walk(head,n1-1,n2,d1);
else if(d1-d!=2&&d1-d!=-2)
headc=walk(head,n1-1,n2,d1);
x++;
if(headc!=NULL)
{
pc=concl(n1,n2,d);
pc->next=headc;
RE headc=pc;
}
};
p1=p2=head;
for(i=0;i<n1;i++)
{
p1=p2->next;
p2=p1;
}
if(check(p1,n2+1)==0)
{
d1=2;
if(x==1)headc=walk(head,n1,n2+1,d1);
else if(d1-d!=2&&d1-d!=-2)
headc=walk(head,n1,n2+1,d1);
x++;
if(headc!=NULL)
{
pc=concl(n1,n2,d);
pc->next=headc;
RE headc=pc;
}
};
for(i=0;i<in1+2;i++)
{
p1=p2->next;
p2=p1;
}
if(check(p1,n1)==0)
{
d1=3;
if(x==1)headc=walk(head,n1,n2+1,d1);
else if(d1-d!=2&&d1-d!=-2)
headc=walk(head,n1+1,n2,d1);
x++;
if(headc!=NULL)
{
pc=concl(n1,n2,d);
pc->next=headc;
RE headc=pc;
}
};
p1=p2=head;
if(check(p1,n2-1)==0)
{
d1=4;
if(x==1)headc=walk(head,n1,n2+1,d1);
else if(d1-d!=2&&d1-d!=-2)
headc=walk(head,n1,n2-1,d1);
x++;
if(headc!=NULL)
{
pc=concl(n1,n2,d);
pc->next=headc;
RE headc=pc;
}
}
else
{
if(++y==1&&n1==out1&&n2==out2)
if(headc==NULL)RE headc=concl(n1,n2,d);
else pc=concl(n1,n2,d);
else RE NULL;
}
RE pc;
}
struct road *concl(int a,int b,int c)
{
struct road *p;
p=(struct road *)malloc(sizeof(struct road));
p->a=a;p->b=b;p->c=c;p->next=NULL;
RE p;
}
void outroad(struct road *head)
{
int n=0;
struct road *p;
p=head;
do{
pr("(%d,%d,%d) ",p->a,p->b,p->c);
p=p->next;
n++;
if(n%3==0)pr("\n");
}
while(p->next!=NULL);
}
int check(struct arr *p,int a)
{
switch(a)
{
CA 0:RE(p->a);BR
CA 1:RE(p->b);BR
CA 2:RE(p->c);BR
CA 3:RE(p->d);BR
CA 4:RE(p->e);BR
CA 5:RE(p->f);BR
CA 6:RE(p->g);BR
CA 7:RE(p->h);BR
CA 8:RE(p->i);BR
CA 9:RE(p->j);BR
CA 10:RE(p->k);BR
CA 11:RE(p->l);BR
}
RE 0;
}
void eval(struct arr *p,int a,int b)
{
switch(a)
{
CA 0:p->a=b;BR
CA 1:p->b=b;BR
CA 2:p->c=b;BR
CA 3:p->d=b;BR
CA 4:p->e=b;BR
CA 5:p->f=b;BR
CA 6:p->g=b;BR
CA 7:p->h=b;BR
CA 8:p->i=b;BR
CA 9:p->j=b;BR
CA 10:p->k=b;BR
CA 11:p->l=b;BR
}
}
...全文
120 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
huaxiamoyun 2010-03-14
  • 打赏
  • 举报
回复
上楼的哥们儿 你发的程序是解决什么问题的 能描述一下吗
huanmie_09 2010-03-14
  • 打赏
  • 举报
回复
很想帮你改,还是重新给你一个吧,
运行环境tc 2.01

#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*得分*/
int gamespeed=50000;/*游戏速度自己调整*/
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;
void Init(void);/*图形驱动*/
void Close(void);/*图形结束*/
void DrawK(void);/*开始画面*/
void GameOver(void);/*结束游戏*/
void GamePlay(void);/*玩游戏具体过程*/
void PrScore(void);/*输出成绩*/
/*主函数*/
void main(void)
{
Init();/*图形驱动*/
DrawK();/*开始画面*/
GamePlay();/*玩游戏具体过程*/
Close();/*图形结束*/
}
/*图形驱动*/
void Init(void)
{
int gd=DETECT,gm;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
cleardevice();
}
/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/
for(i=50;i <=600;i+=10)/*画围墙*/
{
rectangle(i,40,i+10,49); /*上边*/
rectangle(i,451,i+10,460);/*下边*/
}
for(i=40;i <=450;i+=10)
{
rectangle(50,i,59,i+10); /*左边*/
rectangle(601,i,610,i+10);/*右边*/
}
}
/*玩游戏具体过程*/
void GamePlay(void)
{
randomize();/*随机数发生器*/
food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/
snake.life=0;/*活着*/
snake.direction=1;/*方向往右*/
snake.x[0]=100;snake.y[0]=100;/*蛇头*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*节数*/
PrScore();/*输出得分*/
while(1)/*可以重复玩游戏,压ESC键结束*/
{
while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/
{
if(food.yes==1)/*需要出现新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*画面上有食物了*/
}
if(food.yes==0)/*画面上有食物了就要显示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i <snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*显示失败*/
snake.life=1;
break;
}
}
if(snake.x[0] <55||snake.x[0]>595||snake.y[0] <55||
snake.y[0]>455)/*蛇是否撞到墙壁*/
{
GameOver();/*本次游戏结束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把画面上的食物东西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++;/*蛇的身体长一节*/
food.yes=1;/*画面上需要出现新的食物*/
score+=10;
PrScore();/*输出新得分*/
}
setcolor(4);/*画出蛇*/
for(i=0;i <snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一节*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1)/*如果蛇死就跳出循环*/
break;
key=bioskey(0);/*接收按键*/
if(key==ESC)/*按ESC键退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判断是否往相反的方向移动*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/*图形结束*/
void Close(void)
{
getch();
closegraph();
}

a7182388 2010-03-14
  • 打赏
  • 举报
回复
把代码放在这个中间,会好看一点,建议!
==========================


===代码区===


======================
huanmie_09 2010-03-14
  • 打赏
  • 举报
回复
恩,这个是贪吃蛇。
[Quote=引用 9 楼 shashenyidaoocean 的回复:]
你的是贪吃蛇吧?

引用 2 楼 huanmie_09 的回复:
很想帮你改,还是重新给你一个吧,
运行环境tc 2.01

C/C++ code

#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d0……
[/Quote]
bladesoft 2010-03-14
  • 打赏
  • 举报
回复
... 干脆来个贪吃蛇吃迷宫的程序算了。
代码长,不方便看。
还好楼主问题已经解决了
shashenyidaoOCEAN 2010-03-14
  • 打赏
  • 举报
回复
你的是贪吃蛇吧?[Quote=引用 2 楼 huanmie_09 的回复:]
很想帮你改,还是重新给你一个吧,
运行环境tc 2.01

C/C++ code

#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#defi……
[/Quote]
logiciel 2010-03-14
  • 打赏
  • 举报
回复
struct road *walk(struct arr *head,int n1,int n2,int d)
{
int d1,i;
struct arr *p1,*p2;
p2=p1=head;
struct road *headc=NULL,*pc;

改为
struct road *walk(struct arr *head,int n1,int n2,int d)
{
int d1,i;
struct arr *p1,*p2;
struct road *headc=NULL,*pc; //2行交换位置
p2=p1=head;
huaxiamoyun 2010-03-14
  • 打赏
  • 举报
回复
唉 是我使用的软件有问题 真气人 感谢大家的回答
harold2007 2010-03-14
  • 打赏
  • 举报
回复
程序太长,慢慢看吧 !
你们都是牛人啊 ~自己写的?
huanmie_09 2010-03-14
  • 打赏
  • 举报
回复
不好意思,刚贴了个贪吃蛇的程序。
下面给个迷宫探险的:

/*
迷宫探险
*/

#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <graphics.h>
#define N 22
#define M 22
int bg[M][N];

void makebg(int,int);
void drawbg(int[][],int,int,int,int,int);
void drawman(int,int,int);
void rect(int,int,int,int);

void main()
{ /* main()开始 */
int gdriver=VGA,gmode=VGAHI;
int direc;
char ch,ch2;
int step,len,size,x,y,i,j;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
Startgame:
step=20;
len=10;
size=20;
x=0;
y=0;
i=0;
j=0;
makebg(M,N);
cleardevice();
setbkcolor(LIGHTBLUE);
setwritemode(XOR_PUT);
settextstyle(1,0,3);
/*setcolor(GREEN);
outtextxy(100,180,"Press <Q> to quit");*/
setcolor(RED);
setfillstyle(LINE_FILL,RED);
drawbg(bg,M,N,size,0,0);
setcolor(WHITE);
x+=len;y+=len;
drawman(x,y,len);
gotoxy(60,3);
printf("M-by Manual");
gotoxy(60,6);
printf("C-by Computer");
gotoxy(60,9);
printf("Please choice:");
setcolor(WHITE);
while(1)
{
ch=getch();
if(ch=='M'||ch=='m')
{
/* 人工控制 */
gotoxy(60,3);
printf("A-Left ");
gotoxy(60,6);
printf("D-Right ");
gotoxy(60,9);
printf("W-Up ");
gotoxy(60,12);
printf("S-Down");
gotoxy(60,15);
printf("Q-Quit");
while(1)
{
ch=getch();
drawman(x,y,len);
if(ch=='Q'||ch=='q')
{
/*按了Q键,退出游戏*/
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]");
while(1)
{
ch2=getch();
if(ch2=='Y'||ch2=='y')
{
closegraph();
clrscr();
exit(0);
}
else if(ch2=='N'||ch2=='n')
goto Startgame;
}

}
switch(ch)
{
case 'a':
case 'A':
if(j>0&&bg[i][j-1]==0)
{
if(x>step)
{
x-=step;
j--;
}
}
break;
case 's':
case 'S':
if(i<M-1&&bg[i+1][j]==0)
{
if(y<479-step)
{
y+=step;
i++;
}
}
break;
case 'd':
case 'D':
if(j<N-1&&bg[i][j+1]==0)
{
if(x<639-step)
{
x+=step;
j++;
}
}
break;
case 'w':
case 'W':
if(i>0&&bg[i-1][j]==0)
{
if(y>step)
{
y-=step;
i--;
}
}
break;
default :break;
}
drawman(x,y,len);
delay(800);
if(i>=M-1&&j>=N-1)
{
setcolor(LIGHTGREEN);
rectangle(450,300,620,380);
setfillstyle(SOLID_FILL,LIGHTGRAY);
floodfill(480,360,LIGHTGREEN);
settextstyle(0,0,1);
setcolor(MAGENTA);
outtextxy(455,325,"You won the game!");
outtextxy(455,355,"Press Q to quit...");
while(1)
{
ch2=getch();
if(ch2=='Q'||ch2=='q')
break;
}
closegraph();
clrscr();
exit(0);
/*
settextstyle(4,0,3);
setcolor(RED);
outtextxy(150,260,"YOU WIN!");*/
}
}

}/* 人工控制结束 */

else if(ch=='C'||ch=='c')
{
/* 电脑控制 */
/* direc表示上一步运动方向 */
/* 并表示下一步运动方向 */
/* 0~3分别表示 西、北、东、南 */
gotoxy(60,3);
printf("Computer now");
gotoxy(60,6);
printf("Run the maze");
gotoxy(60,9);
printf("Automatically...");
direc=2;
i=j=0;
while(i<M-1||j<N-1)
{
delay(80000);
drawman(x,y,len);
switch(direc)
{
case 0:
/* 以3,0,1的次序尝试 */
if(i<M-1&&bg[i+1][j]==0){
y+=step;i++;
direc=3;
}
else if(j>0&&bg[i][j-1]==0){
x-=step;j--;
direc=0;
}
else if(i>0&&bg[i-1][j]==0){
y-=step;i--;
direc=1;
}
else {
x+=step;j++;
direc=2;
}
break;
case 1:
if(j>0&&bg[i][j-1]==0){
x-=step;j--;
direc=0;
}
else if(i>0&&bg[i-1][j]==0){
y-=step;i--;
direc=1;
}
else if(j<N-1&&bg[i][j+1]==0){
x+=step;j++;
direc=2;
}
else{
y+=step;i++;
direc=3;
}
break;
case 2:
if(i>0&&bg[i-1][j]==0){
y-=step;i--;
direc=1;
}
else if(j<N-1&&bg[i][j+1]==0){
x+=step;j++;
direc=2;
}
else if(i<M-1&&bg[i+1][j]==0){
y+=step;i++;
direc=3;
}
else {
x-=step;j--;
direc=0;
}
break;
case 3:
if(j<N-1&&bg[i][j+1]==0){
x+=step;j++;
direc=2;
}
else if(i<M-1&&bg[i+1][j]==0){
y+=step;i++;
direc=3;
}
else if(j>0&&bg[i][j-1]==0){
x-=step;j--;
direc=0;
}
else {
y-=step;i--;
direc=1;
}
break;
default :break;
}
drawman(x,y,len);
}
gotoxy(60,12);
printf("Finished!");
gotoxy(60,15);
printf("Press any");
gotoxy(60,18);
printf("Key to quit...");
getch();
closegraph();
clrscr();
exit(0);
}/* 电脑控制结束 */
}
}/* main()结束 */

/* 绘制小人 */
void drawman(int x,int y,int len){
int r=len/4;
rect(x-r,y-len,x+r,y-len+2*r);
line(x,y-len+2*r,x,y);
line(x-len,y,x+len,y);
line(x,y,x-len,y+len);
line(x,y,x+len,y+len);
}
/* 绘制迷宫地图 */
void drawbg(int bg[][N],int a,int b,int size,int x,int y){
int startx=x;
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
if(bg[i][j]==1)
rect(x,y,x+size-1,y+size-1);
x+=size;
}
x=startx;
y+=size;
}
rectangle(0,0,size*b,size*a);
line(0,0,size,0);line(0,0,0,size);
line(size*b,size*(a-1),size*b,size*a);
line(size*(b-1),size*a,size*b,size*a);
}
/* 绘制实心矩形 */
void rect(int x0,int y0,int x1,int y1){
int i,j;
for(i=x0;i<=x1;i++)
line(i,y0,i,y1);
}

/* 随机生成代表迷宫地图的数组 */
void makebg(int a,int b){
int i,j;
int ran;
int direc;
/* 初始化迷宫地图 */
for(i=0;i<a;i++)
for(j=0;j<b;j++)
bg[i][j]=1;

/* 随机生成迷宫通路 */
randomize();
i=j=0;direc=2;
while(1){
bg[i][j]=0;
if(i>=M-1&&j>=N-1)break;
ran=(int)rand()*4;
if(ran<1){
if(direc!=1&&i<a-1){
i++;
direc=3;
}
}
else if(ran<2){
if(direc!=2&&j>0){
j--;
direc=0;
}
}
else if(ran<3){
if(direc!=3&&i>0){
i--;
direc=1;
}
}
else {
if(direc!=0&&j<b-1){
j++;
direc=2;
}
}
}
/* 随机生成迷宫其余部分 */
for(i=0;i<a;i++)
for(j=0;j<b;j++)
if(bg[i][j]==1){
ran=(int)rand()*10;
if(ran<7)bg[i][j]=0;
}
}

yianyongle 2010-03-14
  • 打赏
  • 举报
回复
太复杂了,看不懂,回去扎实基础去了

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧