数据结构-走迷宫游戏代码

#include
#include
#include
#include
#include
#define h 11
#define w 10
typedef struct
{
int r,c;
}PosType;//坐标 r表示行,c表示列

typedef struct
{
PosType seat;
int d;
}SElemType;//seat表示当前坐标,d表示当前要转的方向序号

typedef struct
{
SElemType data[1000];
int top;
}SqStack;//栈元素类型,含有一个三元组,top表示该数组的元素个数

SqStack *S;
PosType move[4]={{0,1},{1,0},{0,-1},{-1,0}};//move 表示移动,分别是右、下、左、上
int count=1;//用来统计路径条数
SqStack *InitStack() //构造栈
{
SqStack *S;
S=(SqStack *)malloc(sizeof(SqStack));
S->top=-1;
return S;
}

void Push(SqStack *S,SElemType data)//入栈
{
S->top++;
S->data[S->top]=data;
}

void Pop(SqStack *S)//出栈
{
S->top--;
}

void print(SqStack *s) //显示一条路径
{

int map[h][w]={//形成迷宫的矩阵
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,0,0,1,1},
{1,0,1,1,1,0,0,1,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,1,1},
{1,0,1,1,1,1,0,0,1,1},
{1,1,1,0,0,0,1,0,1,1},
{1,1,1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int i;
printf("第%d条路径为:\n",count);

for(i=0;i<=s->top;i++)
{
printf("(%d,%d,%d) ",s->data[i].seat .r,s->data[i].seat.c,s->data[i].d);
map[s->data[i].seat .r][s->data[i].seat.c]=2;
if((i+1)%5==0)
printf("\n");
}
printf("\n");
printf("\n");
count++;
int m,n;
for(m=0;m{
printf("\n");
for(n=0;n{
if(map[m][n]==2)
printf("○");

else
printf("■");
}
}

printf("\n");
printf("\n");
}

void MazePath(int maze[][w],int x,int y)//找到全部路径的函数
{
int i;
SElemType data;
if(x==h-2&&y==w-2)//到达出口
{
print(S);
return;
}
for(i=0;i<4;i++)//进行四个方向的判断
{
data.seat.r=x;
data.seat.c=y;
data.d=i;
maze[x][y]=-1;
x=x+move[i].r;//对下一个方向处理
y=y+move[i].c;

if(maze[x][y]==0)//如果下一方向可通,把此元素入栈
{
Push(S,data);
MazePath(maze,x,y);//求下一元素为开始的路径
Pop(S);
}
x=x-move[i].r;//若下一方向不通,回到此坐标
y=y-move[i].c;
maze[x][y]=0;
}

}

void game(int map1[h][w])//游戏函数
{
#define killtime 15
clock_t start, finish;
double duration;//持续时间

int x=1,y=1,m=0,n=0,M,N,MAP[100][100];//x->colom y->row
char cCtrl='\0';
printf(" 游戏规则:\n w向上,s向下,a向左,d向右,q退出\n 按任意键开始,方向键开始计时\n");

for(M=0;M<=h-1;M++)
for(N=0;N<=w-1;N++)
MAP[M][N]=map1[M][N];
{
{
start = clock();//开始计时
while((cCtrl=getch())!='q')
{
switch(cCtrl)
{
case 'w'://向上
{ p

rintf("\a");//响铃
if(y>0&&!MAP[y-1][x])
y--;
}break;
case 's'://下
{ printf("\a");
if(!MAP[y+1][x])
y++;
}break;
case 'a'://左
{ printf("\a");
if(x>0&&!MAP[y][x-1])
x--;
}break;
case 'd'://右
{ printf("\a");
if(!MAP[y][x+1])
x++;
}break;
}

system("cls");//刷屏
for(m=0;m{
for(n=0;n{
if(m==y&&n==x)
{
system("color 6");
printf("◎");//现实老鼠所在位置
}
else
{
if(MAP[m][n])
printf("■");//打印墙壁
else
{
if(m==9&&n==8)
printf("☆");//显示粮仓
else
printf("□");//显示可行路径
}
}
if(x==8&&y==9)
{
finish = clock();//停止计时
duration = (double)(finish - start) / CLOCKS_PER_SEC;//compute the time
printf("\n");
printf("你耗费的时间是:\n");
printf("%f秒\n",duration);

if(duration>killtime)//lose
{
printf(" 你输了,完蛋了,小老鼠要饿死了 囧rz!!\n");
}
else//win
{ printf(" \n └(^o^)┘ 小老鼠总算找到粮仓了,谢谢啊! \n 这是你赢得的金币◎,小老鼠奉上:\n");
printf(" ");


for(int i=0;i<20-duration;i++)
printf("◎");
printf("\n");
}
return;
}
}
printf("\n");
}
}
}

}
}

void savemap(int map[h][w])//保存地图
{
int i,j;
FILE *fp;
if((fp=fopen("mapshow.txt","a"))==NULL)//以追加的方式打开一个文件
{
printf("Can't open file\n");
exit(0);
}
else{
fprintf(fp,"\n");
fprintf(fp,"new map:");
for( i=0;i{
fprintf(fp,"\n");
for( j=0;j{
if(map[i][j]==1)
fprintf(fp,"■");
else
fprintf(fp,"□");
}
}
fclose(fp);
fp=fopen("ok.txt","w");
for(i=0;i<11;i++)
for(j=0;j<10;j++)
fprintf(fp," %d ",map[i][j]);
}
fclose(fp);
}

void edit(int game[])
{

int a[100000];
FILE *fp;
fp=fopen("ok.txt","r");//打开地图数组文件
int t=0;
while(fscanf(fp,"%d",&a[t])!=EOF)//没有到文件结尾
{
game[t]=a[t];//把读出的赋值到新的数组
t=t+

1;
}
fclose(fp);
}



void change(int map[h][w])//墙变路,路变墙
{
int m,n;


printf("输入你想改变的坐标:\n");
scanf("%d",&m);
scanf("%d",&n);

if(map[m][n]==0)
map[m][n]=1;
else
map[m][n]=0;
printf("地图呈现:\n");

for(m=0;m{
printf("\n");
for(n=0;n{
if(map[m][n]==0)
printf("□");
if(map[m][n]==1)
printf("■");
}
}
printf("\n 你想保存修改吗?\n ");

switch(getch())
{
case 'y':
savemap(map);break;
case 'n':
printf("好吧,不保存地图······");
break;
default:
printf("error!!\n");

}
printf(" 开始新游戏 ");
game(map);
}




int main()
{
FILE *fp;
int game00[1000];
int map[h][w];
// fp=fopen("ok.txt","r");
//void edit(int game00[]);
edit(game00);
int i,j,t=0;
for(i=0;ifor(j=0;j{
map[i][j]=game00[t];
t++;
}
S=InitStack();//先创建栈
while(1) {
printf(" 现在你可以选择操作:\n");
printf(" 1. 帮助老鼠找到粮仓,begin!\n");
printf(" 2. 输出所有路径,come on!\n");
printf(" 3. 先改变地图,这个地图太简单了\n");
printf(" 4. 退出游戏\n");

switch(getch())
{
case '1':
game(map);break;
case '2':

MazePath(map,1,1);break;
case '3':
change(map);break;
case '4':
exit(0);
default:printf("wrong\n");

}
}
}

相关文档
最新文档