之前有许多人来问我C语言能不能写个迷宫类的游戏,我也是思来想去,觉得必须安排上
还可以加上一些图形库的运用,由于博主还没学会,期待大佬来完善。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define row 1024
#define col 1024
typedef struct Maze{
int map[row][col];
int information[row][col];
}Maze;
typedef struct Point{
int x,y;
}Point;
void into_Maze(Maze *q,Maze *Q,Point *origin,Point *exit)
{
int i,j,n,h,w=1;
char str[1000000];
do
{
gets(str);
j=strlen(str);
if(j == 0)
break;
for(i=1;i<j+1;i++)
{
q->map[w][i]=str[i-1]-'0';
}
w++;
}while(1);
for(h=0;h<i;h++)
{
q->map[0][h]=1;
q->map[w][h]=1;
}
for(n=0;n<w;n++)
{
q->map[n][0]=1;
q->map[n][i]=1;
}
for(h=1;h<w;h++)
{
for(n=1;n<i;n++)
{
Q->information[h][n]=0;
}
}
for(h=0;h<i;h++)
{
Q->information[0][h]=0;
Q->information[w][h]=0;
}
for(n=0;n<w;n++)
{
Q->information[n][0]=0;
Q->information[n][i]=0;
}
for(h=0;h<w+1;h++)
{
for(n=0;n<=i;n++)
;
}
for(h=0;h<w+1;h++)
{
for(n=0;n<i+1;n++)
;
}
origin->x=0,origin->y=0;
exit->x=h-3,exit->y=i-2;
}
int number(Maze *Q,Maze *q,Point *present)
{
int min=999;
int num[999],j=0,i;
if(q->map[present->x][present->y+1]==1)
{
num[0]=999;
}else
num[0]=Q->information[present->x][present->y+1];
if(q->map[present->x+1][present->y]==1)
{
num[1]=999;
}else
num[1]=Q->information[present->x+1][present->y];
if(q->map[present->x][present->y-1]==1)
{
num[2]=999;
}else
num[2]=Q->information[present->x][present->y-1];
if(q->map[present->x-1][present->y]==1)
{
num[3]=999;
}else
num[3]=Q->information[present->x-1][present->y];
min=num[0];
for(i=0;i<4;i++)
{
if(min>num[i])
{
min=num[i];
j=i;
}
}
return j;
}
int move(Maze *q,Maze*Q,Point *origin,Point *exit,Point *present)
{
int num=0,m=0,n=0,k=0,i=0;
char Direction[999]={0};
present->x=origin->x+1;
present->y=origin->y+1;
Q->information[present->x][present->y]=Q->information[origin->x][origin->y]+1;
while(!(present->x == exit->x+1 && present->y == exit->y+1))
{
num=number(Q,q,present);
if(Q->information[origin->x+1][origin->y+1]>27)
{
k=2;
break;
}
else if(i>1000){
k=2;
break;
}
else if(q->map[present->x][present->y+1]==0 && num==0)
{
present->y=present->y+1;
Q->information[present->x][present->y] = Q->information[present->x][present->y]+1;
if( Direction[m-1] =='L')
{ m=m-1;
continue;
}
Direction[m] = 'R';
m++;
continue;
}
else if(q->map[present->x+1][present->y]==0 && num==1)
{
present->x=present->x+1;
Q->information[present->x][present->y] = Q->information[present->x][present->y]+1;
if(Direction[m-1]=='U')
{ m=m-1;
continue;
}
Direction[m]='D';
m++;
continue;
}
else if(q->map[present->x][present->y-1]==0 && num==2)
{
present->y=present->y-1;
Q->information[present->x][present->y]=Q->information[present->x][present->y]+1;
if(Direction[m-1]=='R')
{ m=m-1;
continue;
}
Direction[m]='L';
m++;
continue;
}
else if(q->map[present->x-1][present->y]==0 && num==3)
{
present->x=present->x-1;
Q->information[present->x][present->y]=Q->information[present->x][present->y]+1;
if(Direction[m-1]=='D')
{ m=m-1;
continue;
}
Direction[m]='U';
m++;
continue;
}
i++;
}
if(k==2)
printf("NO PASS!\n");
else puts(Direction);
return k;
}
int main()
{
int k=0;
Maze *q = (Maze*)malloc(sizeof(Maze));
Maze *Q = (Maze*)malloc(sizeof(Maze));
Point *origin = (Point*)malloc(sizeof(Point));
Point *exit = (Point*)malloc(sizeof(Point));
Point *present = (Point*)malloc(sizeof(Point));
into_Maze(q,Q,origin,exit);
k=move(q,Q,origin,exit,present);
return 0;
}
|