|
|
|
@ -0,0 +1,237 @@
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<conio.h>
|
|
|
|
|
#include<time.h>
|
|
|
|
|
#include<windows.h>
|
|
|
|
|
#define M 3
|
|
|
|
|
#define N 18
|
|
|
|
|
#define Max 10000
|
|
|
|
|
void draw(int map[20][20]);
|
|
|
|
|
int mg[20][20]={0} ; //这是搜索算法要用的地图
|
|
|
|
|
void gotoxy(int x, int y) {
|
|
|
|
|
COORD pos = {x,y};
|
|
|
|
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
|
|
|
|
|
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
|
|
|
|
|
}
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
int i,j; //块的位置
|
|
|
|
|
int pre; //本路径中上一块在队列中的下标
|
|
|
|
|
}Qu[Max];
|
|
|
|
|
void print(int n);
|
|
|
|
|
int mgpath(int xi,int yi,int xe,int ye) //搜索算法
|
|
|
|
|
{
|
|
|
|
|
int i,j,di,pp=250;
|
|
|
|
|
int front=-1,rear=-1;
|
|
|
|
|
rear++;
|
|
|
|
|
Qu[rear].i=xi;
|
|
|
|
|
Qu[rear].j=yi;
|
|
|
|
|
Qu[rear].pre=-1;
|
|
|
|
|
mg[xi][yi]=-1;
|
|
|
|
|
while(front<rear)
|
|
|
|
|
{
|
|
|
|
|
front++;
|
|
|
|
|
i=Qu[front].i;
|
|
|
|
|
j=Qu[front].j;
|
|
|
|
|
if(i==xe&&j==ye)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
print(front);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
for(di=0;di<4;di++)
|
|
|
|
|
{
|
|
|
|
|
switch(di) //四个方向
|
|
|
|
|
{
|
|
|
|
|
case 0:i=Qu[front].i-1;j=Qu[front].j;break;
|
|
|
|
|
case 1:i=Qu[front].i;j=Qu[front].j+1;break;
|
|
|
|
|
case 2:i=Qu[front].i+1;j=Qu[front].j;break;
|
|
|
|
|
case 3:i=Qu[front].i;j=Qu[front].j-1;break;
|
|
|
|
|
}
|
|
|
|
|
if(mg[i][j]==0||mg[i][j]==1||mg[i][j]==3||mg[i][j]==5)
|
|
|
|
|
{
|
|
|
|
|
rear++;
|
|
|
|
|
Qu[rear].i=i;
|
|
|
|
|
Qu[rear].j=j;
|
|
|
|
|
Qu[rear].pre=front;
|
|
|
|
|
mg[i][j]=4; //避免死循环
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pp;
|
|
|
|
|
}
|
|
|
|
|
void print(int n) //输出 路径算法
|
|
|
|
|
{
|
|
|
|
|
int k=n,j,m=1;
|
|
|
|
|
|
|
|
|
|
do //将输出的路径上的所有pre改为-1
|
|
|
|
|
{
|
|
|
|
|
j=k;
|
|
|
|
|
k=Qu[k].pre;
|
|
|
|
|
Qu[j].pre=-1;
|
|
|
|
|
}while(k!=0);
|
|
|
|
|
|
|
|
|
|
k=0;
|
|
|
|
|
while(k<Max)
|
|
|
|
|
{
|
|
|
|
|
if(Qu[k].pre==-1)
|
|
|
|
|
{
|
|
|
|
|
mg[(Qu[k].i)][(Qu[k].j)]=5;
|
|
|
|
|
}
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
draw(mg);
|
|
|
|
|
}
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
system("cls");
|
|
|
|
|
printf("\33[?25l") ;
|
|
|
|
|
printf("\n\n\n---------------------欢迎来到迷宫小游戏----------------\n----------------------按任意键即可开始-------------------");
|
|
|
|
|
getch();
|
|
|
|
|
system("cls");
|
|
|
|
|
srand((unsigned)time(NULL)); //生成随机地图
|
|
|
|
|
int map[20][20]={0};
|
|
|
|
|
int y,x,pp;
|
|
|
|
|
for(y=0;y<20;y++)
|
|
|
|
|
{
|
|
|
|
|
for(x=0;x<20;x++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
map[y][x]=rand()%3;
|
|
|
|
|
if(y==0||y==19||x==0||x==19)
|
|
|
|
|
map[y][x]=2;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} //地图随机生成结束
|
|
|
|
|
int i,j;
|
|
|
|
|
for(i=0;i<20;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<20;j++)
|
|
|
|
|
{
|
|
|
|
|
if(map[i][j]==1)
|
|
|
|
|
map[i][j]=0;
|
|
|
|
|
//将没考虑好的地图变得正确
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char m;
|
|
|
|
|
x=13;y=1;
|
|
|
|
|
|
|
|
|
|
map[1][13]=1;//定义起点终点
|
|
|
|
|
map[18][3]=3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
draw(map);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
if(x==3&&y==18)
|
|
|
|
|
printf("恭喜胜利\a");
|
|
|
|
|
m=getch(); //getch()是写入字符,与getchar()的不同是,输入的字符不会在屏幕上显示
|
|
|
|
|
//printf("\a"); //声音 !!!!!!!!!
|
|
|
|
|
switch(m)
|
|
|
|
|
{
|
|
|
|
|
case 'a':if(map[y][x-1]!=2) //判断会不会撞墙
|
|
|
|
|
{//判断有没有箱子
|
|
|
|
|
if(map[y][x-1]!=4){x=x-1;map[y][x]=1;map[y][x+1]=0;gotoxy(x*2,y+1);printf("人");gotoxy(x*2+2,y+1);printf(" ");}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
printf("\a");
|
|
|
|
|
break;
|
|
|
|
|
case 's':if(map[y+1][x]!=2)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(map[y+1][x]!=4){y=y+1;map[y][x]=1;map[y-1][x]=0;gotoxy(x*2,y+1);printf("人");gotoxy(x*2,y);printf(" ");}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
printf("\a");
|
|
|
|
|
break;
|
|
|
|
|
case 'd':if(map[y][x+1]!=2)
|
|
|
|
|
{
|
|
|
|
|
if(map[y][x+1]!=4){x=x+1;map[y][x]=1;map[y][x-1]=0;gotoxy(x*2,y+1);printf("人");gotoxy(x*2-2,y+1);printf(" ");}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
printf("\a");
|
|
|
|
|
break;
|
|
|
|
|
case 'w':if(map[y-1][x]!=2)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(map[y-1][x]!=4){y=y-1;map[y][x]=1;map[y+1][x]=0;gotoxy(x*2,y+1);printf("人");gotoxy(x*2,y+2);printf(" ");}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
printf("\a");
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
{
|
|
|
|
|
srand((unsigned)time(NULL)); //生成随机地图
|
|
|
|
|
map[20][20]={0};
|
|
|
|
|
|
|
|
|
|
for(y=0;y<20;y++)
|
|
|
|
|
{
|
|
|
|
|
for(x=0;x<20;x++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
map[y][x]=rand()%3;
|
|
|
|
|
if(y==0||y==19||x==0||x==19)
|
|
|
|
|
map[y][x]=2;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} //地图随机生成结束
|
|
|
|
|
for(i=0;i<20;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<20;j++)
|
|
|
|
|
{
|
|
|
|
|
if(map[i][j]==1)
|
|
|
|
|
map[i][j]=0;
|
|
|
|
|
if(j==0||j==19||i==0||i==19)
|
|
|
|
|
map[i][j]=2;//将没考虑好的地图变得正确
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
x=13;y=1;
|
|
|
|
|
map[1][13]=1;//定义起点终点
|
|
|
|
|
map[18][3]=3;
|
|
|
|
|
map[17][3]=0;
|
|
|
|
|
map[18][4]=0;
|
|
|
|
|
draw(map);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 't':
|
|
|
|
|
{
|
|
|
|
|
for(i=0;i<20;i++)
|
|
|
|
|
{
|
|
|
|
|
for(j=0;j<20;j++)
|
|
|
|
|
{
|
|
|
|
|
mg[i][j]=map[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pp=mgpath(y,x,N,M);
|
|
|
|
|
if(pp==250) {
|
|
|
|
|
|
|
|
|
|
printf("此地图无法到达终点,按R键重新开始");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|