如果觉得对你有帮助,能否点个赞或关个注,以示鼓励笔者呢?!博客目录 | 先点这里
文章目录偶然翻到了自己大一时候写的一些代码,代码冗余,但还是想保存下来,以作纪念。
- 1.运行效果
- 2.运行环境
- 3.完整代码
源码(Dev5.4.0版本下可直接(.cpp文件)运行 gcc 4.7.2)
注意:由于部分代码为C11标准,后缀名应改为cpp运行
编译器下载链接:https://pan.baidu.com/s/1Hywq6hxzxMzX1-22ig2HdA
提取码:kd65
下载后解压即可使用
#include#include #include using namespace std; #define MAXN 50//迷宫大小 typedef struct//BFS寻路用 { int x,y; int step; }Pos; Pos start,finish,path[2500];//起点,终点,路径 int k;//BFS路径标记 int Rank=10;//控制难度,Rank越小难度越大 int maze[MAXN][MAXN]; int bookBFS[MAXN][MAXN];//用于BFS是否访问标记 int visit[MAXN][MAXN];//标记BFS的过程 void BFS();//BFS最短路径寻路 void Gamer();//用户 *** 作界面 void DrawMaze();//绘制迷宫 void DrawPath();//绘制路径 void choice_level();//选择关卡 void CreateMaze(int,int,int);//创建迷宫 void gotoxy(int,int);//坐标函数 void InitMaze();//初始化迷宫 void load_cartoon();//加载动画 void HideCursor();//隐藏光标 void ShowCursor();//显示光标 //加载动画 char cartoon1[20][150] = { {" /~/~/ "}, {" /~/~/ "}, {" Microsotf@ ~ ~ __ XP "}, {" \ /\ / - _ _| _ _ _ /_ "}, {" \/ \/ | | | |_| |_| \/\/ __/ "}, {" "}, {" _____________________ "}, {" │ │ "}, {" └────────────────────┘ "}, {" "}, {" "}, {" "}, {" "}, {" "}, {" "}, {" Copyright ΘMicrosoft Corporation Microsoft* "}, }; char cartoon2[40][150]={ {" 健 康 游 戏 忠 告 "}, {" "}, {" "}, {" "}, {" "}, {" 抵 制 不 良 游 戏 , 拒 绝 盗 版 游 戏。 "}, {" "}, {" "}, {" "}, {" "}, {" 注 意 自 我 保 护 , 谨 防 受 骗 上 当。 "}, {" "}, {" "}, {" "}, {" "}, {" 适 度 游 戏 益 脑 , 沉 迷 游 戏 伤 身。 "}, {" "}, {" "}, {" "}, {" "}, {" 合 理 安 排 时 间 , 享 受 健 康 生 活。 "}, }; //Logo char logo[30][100]={ {" BBBBB BBBBBP BBBBBBB "}, {" SBBBM BQBBBQ BBBBBBB "}, {" BBBBBBB. iEBBBI BBBBQE gBBQP: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "}, {" uBBBBBBv 1BBBBBB BBBBBb BQBBBBJ BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "}, {" :BBBBBBQ .BBBBBB. BBQBBd iBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "}, {" .BQBB5. BQBQBB BBBBBq IBBBBBZ BBBBBB BBBBBB "}, {" d. BBBr BBBBBq :RBBi BBBBBB BBBBBB "}, {" BBBBB2 BBBBBDBBQBBBBBBBBBBBQBBBBBBBBBQBQBBBBBBB "}, {" Ivv7vr77j. 1BBBBBMBBBBBBBBBBBBBQBRQBBBBBR BQBBBBBBBQBBBBBQBBBBBBBBBQBB "}, {" :BBBBBBBBB7 KBBQBBBBBBBBBQBQBQBQBBBQBQBBBB BBBBBB BBBBBB "}, {" :BBBBBBBBB: 2BQBBBBBQBBBBBBBBBBBBBBBBBBBBQ BBBBBB BBBQBE "}, {" .BQbQBBBBBi QBBBBBBB1 BBBBBB BBBBBB "}, {" .BBBBBi 7QBBBQBQBBL BB: BBBBBBBBBQBBBBBBBBBBBBBQBBBQ "}, {" iBBBBBi :BBBBBBBBBBBBBBBBBs BBBBBBBBBBBBQBBBBBBBBBBBQBBM "}, {" iBBBBB: :BBBBQBg MBQBBMEBBBBBQd "}, {" rBBBBB: iBBBQBBBU BBBBBj .BQBBBBBM: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "}, {" iBBBBB. BBBBBBBB. BBQBBb :QBBBBBBK BBBBQBBBBBBBBBQBQBBBBBBBBBBBBBQBBB "}, {" :QBBBQ. .QBBBBY BBBBBd iBBBBr BBBBBBBBBBBQBBBBBBBQBBBQBBBBBBBBBB "}, {" LBBBBB7 :B2 QBBBBg sZ BBBBBB BBBBQB "}, {" rBBBBBBBBM. BBBQB1 BQBBBB BBBBBB "}, {" iBBBBBBBQBBBBBE BBBBQB BBBBBB "}, {" .BBBBQBSBBBBBBBQBQBBBBBBBBBBBBBBBBBBBBBBBB. BBBBBQBBBBBQBBBQBBBBBBBBBBBBBBBBBB "}, {" vBBQB. rBBQBQBQBBBQBBBQBBBBBQBBBBBBBBBBv BBQBBBBBQBBBBBQBQBBBBBBBBBBBQBBBBB "}, {" BBr sPDQMQBBBBBBBBBBBBBBQBBBBBBB BQBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "}, {" BBBBBB BBBBBB "}, {" "}, {" 制 作 "}, {" "}, {" 山 河 "} }; //坐标函数 void gotoxy(int x, int y) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos); } //隐藏光标 void HideCursor() { CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } //显示光标 void ShowCursor() { CONSOLE_CURSOR_INFO cursor_info={1,1}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } //初始化迷宫 void InitMaze() { for(int i=0;i 0) { dx+=dir[i][0]; dy+=dir[i][1]; //排除掉回头路 if (maze[dx][dy] == 0) break; //判断是否挖穿路径 int count = 0; for (int j = dx - 1; j < dx + 2; j++) for (int k = dy - 1; k < dy + 2; k++) if (abs(j - dx) + abs(k - dy) == 1 && maze[j][k] == 0) count++; if (count > 1) break; //确保不会挖穿时,前进 range--; maze[dx][dy] = 0; } if (range <= 0) CreateMaze(dx,dy,Rank); } } void DrawMaze() { //入口 maze[2][1]=0; start.x=2;start.y=1;start.step=0; //出口 for(int i=MAXN-3;i>=0;i--) if(maze[i][MAXN-3]==0){ maze[i][MAXN-2]=0; finish.x=i;finish.y=MAXN-2;finish.step=0; break; } for(int i=0;i q; q.push(start); int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; bookBFS[start.x][start.y]=1; int flag=0; while(q.size()!=0) { Pos t=q.front(); q.pop(); if(t.x==finish.x&&t.y==finish.y)//找到终点 { flag=1; finish.step=t.step; Pos t=finish;//倒推路径 while(!(t.x==start.x&&t.y==start.y))//不在起点的时候 { path[k].x=t.x;path[k++].y=t.y; int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; for(int i=0;i<4;i++){ Pos s=t; s.x+=dx[i];s.y+=dy[i]; if(s.x<=0||s.x>MAXN-2||s.y<=0||s.y>MAXN-2||maze[s.x][s.y]==1)continue;//超出范围 if(visit[s.x][s.y]==visit[t.x][t.y]-1){t.x=s.x;t.y=s.y;break;}//倒退回去 } } path[k]=start;//放入起点 } if(flag==1)break; //向四个方向扩展 for(int i=0;i<4;i++) { Pos p=t; p.x+=dx[i];p.y+=dy[i];p.step++; if(p.x<=0||p.x>MAXN-2||p.y<=0||p.y>MAXN-2||maze[p.x][p.y]==1||bookBFS[p.x][p.y]==1) continue; bookBFS[p.x][p.y]=1; visit[p.x][p.y]=p.step; q.push(p); } } } //绘制最短路径BFS void DrawPath() { //路径坐标 int flag=0; int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1}; string road; for(int i=k;i>=0;i--) { if(flag==0){flag=1;road="──";}//第一次进入 Pos t=path[i];//当前打印位置 if(flag==1&&i!=0){ Pos s=path[i+1];//存储上一个位置 Pos p=path[i-1];//存储下一个位置 int tx1,tx2,ty1,ty2; tx1=t.x-s.x;ty1=t.y-s.y; tx2=t.x-p.x;ty2=t.y-p.y; //"┘""┐""└""┌""──""│" if((tx1==-1&&tx2== 1&&ty1== 0&&ty2== 0)||(tx1== 1&&tx2==-1&&ty1== 0&&ty2== 0))road="│"; if((tx1== 0&&tx2== 0&&ty1== 1&&ty2==-1)||(tx1== 0&&tx2== 0&&ty1==-1&&ty2== 1))road="──"; if((tx1== 0&&tx2==-1&&ty1== 1&&ty2== 0)||(tx1==-1&&tx2== 0&&ty1== 0&&ty2== 1))road="┐"; if((tx1== 1&&tx2== 0&&ty1== 0&&ty2==-1)||(tx1== 0&&tx2== 1&&ty1==-1&&ty2== 0))road="└"; if((tx1==-1&&tx2== 0&&ty1== 0&&ty2==-1)||(tx1== 0&&tx2==-1&&ty1==-1&&ty2== 0))road="┌"; if((tx1== 1&&tx2== 0&&ty1== 0&&ty2== 1)||(tx1== 0&&tx2== 1&&ty1== 1&&ty2== 0))road="┘"; } if(i==0)road="─";//出口位置 gotoxy(2*t.y,t.x);//每个方格占两个单位 cout< =1){ gotoxy(posx,posy);cout<<" "; y--;posx-=2;gamerstep++; gotoxy(posx,posy);cout<<"●"; gotoxy(stepx,stepy);cout< >choice; if(choice==1){ InitMaze(); CreateMaze(2,1,--Rank); system("cls"); DrawMaze(); Gamer(); } else{ system("cls"); return; } } void load_cartoon() { //动态加载页面 int i,l, j; gotoxy(25, 15); for (i = 0; i <= 16; i++) { cout< = 53 && j <= 70) { gotoxy(j+25, 22); printf("[]"); } Sleep(100); } for (j = i - 4; j <= i; j = j + 2) { if (j >= 53 && j <= 70) { gotoxy(j+25, 22); printf(" "); } } } } //隐藏光标 HideCursor(); //游戏忠告 system("cls"); for (i = 0; i <= 20; i++) { gotoxy(25, 10+i); cout< >level; Rank=10-level; InitMaze(); CreateMaze(2,1,Rank); //重新初始化 system("cls"); DrawMaze(); Gamer(); } int main() { srand((unsigned)time(NULL));//用时间做随机数种子 system("mode con cols=200 lines=100"); system("color 00"); load_cartoon(); system("color 70"); choice_level(); gotoxy(75,20); system("pause"); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)