#include <stdio.h> #include <stdlib.h> #include <time.h> #include <getch.h> int main() { char arr[10][10]={ //自己设定迷宫路径,‘ ‘表示路径,‘1‘表示墙,‘s‘表示人 ‘s‘,‘ ‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘, ‘1‘,‘ ‘,‘ ‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘, ‘1‘,‘1‘,‘ ‘,‘ ‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘, ‘1‘,‘1‘,‘1‘,‘ ‘,‘ ‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘, ‘1‘,‘1‘,‘1‘,‘1‘,‘ ‘,‘ ‘,‘1‘,‘1‘,‘1‘,‘1‘, ‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘ ‘,‘ ‘,‘1‘,‘1‘,‘1‘, ‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘ ‘,‘ ‘,‘1‘,‘1‘, ‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘ ‘,‘ ‘,‘1‘, ‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘ ‘,‘1‘, ‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘,‘ ‘,‘ ‘}; int lsx,lsy; time_t start_time=time(NULL); while(10!=lsy) { system("clear"); for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { printf("%c",arr[i][j]); if(arr[i][j]==‘s‘) { lsx=i; lsy=j; } } printf("\n"); } if(9==lsx && 9==lsy) { printf("恭喜你走出迷宫,共用时%u秒!\n",time(NULL)-start_time); return 0; } switch(getch()) { case 185: if(9!=lsy && arr[lsx][lsy+1]==‘ ‘) { arr[lsx][lsy+1]=‘s‘; arr[lsx][lsy]=‘ ‘; ++lsy; } break; case 186: if(0!=lsy && arr[lsx][lsy-1]==‘ ‘) { arr[lsx][lsy-1]=‘s‘; arr[lsx][lsy]=‘ ‘; --lsy; } break; case 184: if(9!=lsx && arr[lsx+1][lsy]==‘ ‘) { arr[lsx+1][lsy]=‘s‘; arr[lsx][lsy]=‘ ‘; ++lsx; } break; case 183: if(0!=lsx && arr[lsx-1][lsy]==‘ ‘) { arr[lsx-1][lsy]=‘s‘; arr[lsx][lsy]=‘ ‘; --lsx; } break; } } }
#ifndef GETCH_H #define GETCH_H #include <stdio.h> #include <termios.h> #include <unistd.h> // 修改终端的控制方式,1取消回显、确认 2获取数据 3还原 static int getch(void) { // 记录终端的配置信息 struct termios old; // 获取终端的配置信息 tcgetattr(STDIN_FILENO,&old); // 设置新的终端配置 struct termios _new = old; // 取消确认、回显 _new.c_lflag &= ~(ICANON|ECHO); // 设置终端配置信息 tcsetattr(STDIN_FILENO,TCSANOW,&_new); // 在新模式下获取数据 unsigned int key_val = 0; do{ key_val = key_val+getchar(); }while(stdin->_IO_read_end - stdin->_IO_read_ptr); // 还原配置信息 tcsetattr(STDIN_FILENO,TCSANOW,&old); return key_val; } #endif//GETCH_H
原文:https://www.cnblogs.com/zhukj/p/13295375.html