#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