首页 > 其他 > 详细

走迷宫

时间:2020-07-14 01:02:25      阅读:88      评论:0      收藏:0      [点我收藏+]

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <getch.h>//读取键盘

int main()
{
char maze[10][10] = {
{‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘},
{‘#‘,‘@‘,‘#‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘},
{‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘#‘},
{‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘#‘,‘#‘,‘ ‘,‘#‘,‘#‘},
{‘#‘,‘ ‘,‘#‘,‘ ‘,‘ ‘,‘ ‘,‘#‘,‘ ‘,‘ ‘,‘#‘},
{‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘#‘,‘#‘,‘#‘},
{‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘#‘},
{‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘ ‘,‘#‘,‘#
{‘#‘,‘ ‘,‘ ‘,‘ ‘,‘#‘,‘ ‘,‘ ‘,‘ ‘,‘#‘,‘#‘},
{‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘,‘#‘},
};//建立数组

int mouse_x = 1 , mouse_y = 1;//起始位置

time_t start_time = time(NULL);

for(;;)
{
system("clear");//清屏

for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
printf("%c ",maze[i][j]);
}
printf("\n");
}

if(1 == mouse_x && 9 == mouse_y)
{
printf("恭喜你走出迷宫,共用时%u秒!\n",time(NULL) - start_time);
return 0;
}//到达终点

switch(getch())
{
case 183://向上
if(0 != mouse_x && ‘ ‘==maze[mouse_x-1][mouse_y])
{
maze[mouse_x][mouse_y] = ‘ ‘;
maze[--mouse_x][mouse_y] = ‘@‘;
}
break;
case 184://向下
if(9 != mouse_x && ‘ ‘==maze[mouse_x+1][mouse_y])
{
maze[mouse_x][mouse_y] = ‘ ‘;
maze[++mouse_x][mouse_y] = ‘@‘;
}
break;;
case 185://向右
if(9 != mouse_y && ‘ ‘==maze[mouse_x][mouse_y+1])
{
maze[mouse_x][mouse_y] = ‘ ‘;
maze[mouse_x][++mouse_y] = ‘@‘;
}
break;
case 186://向左
if(0 != mouse_y && ‘ ‘==maze[mouse_x][mouse_y-1])
{
maze[mouse_x][mouse_y] = ‘ ‘;
maze[mouse_x][--mouse_y] = ‘@‘;
}
break;
}
}
}

走迷宫

原文:https://www.cnblogs.com/Nxet/p/13296665.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!