一个好玩的迷宫小游戏
include<bits/stdc++.h>
include<conio.h>
using namespace std; int cnt=0; char b; int n,m,t,st1,st2,end1,end2; char a[18][18]; int vis[18][18]; int to[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; void dfs(int x,int y){ if(x==end1&&y==end2){ cnt++; return; } vis[x][y]=1; for(int i=0;i<4;i++){ int tx=x+to[i][0]; int ty=y+to[i][1]; if(vis[tx][ty]==0&&x<=n&&x>=1&&y>=1&&y<m&&a[tx][ty]==' '){ dfs(tx,ty); vis[tx][ty]=0; } } return; } int main(){ srand(time(0)); while(1){ memset(a,' ',sizeof(a)); memset(vis,0,sizeof(vis)); for(int i=1;i<18;i++) a[1][i]=a[i][1]=a[17][i]=a[i][17]='#'; n=17;m=17;t=150; st1=2;st2=2; end1=16;end2=16;; int t1,t2; while(t--){ t1=rand()%16+2;t2=rand()%16+2;; a[t1][t2]='#'; } dfs(st1,st2); if(cnt!=0){ break; } } cout<<"加载完毕"; int xx=2,yx=2; while(1) { if(_kbhit()) { b=_getch(); if(b==65&&a[yx][xx-1]==' ') { xx--; a[yx][xx+1]=' '; } if(b==87&&a[yx-1][xx]==' ') { yx--; a[yx+1][xx]=' '; } if(b==68&&a[yx][xx+1]==' ') { xx++; a[yx][xx-1]=' '; } if(b==83&&a[yx+1][xx]==' ') { yx++; a[yx-1][xx]=' '; } system("cls"); a[yx][xx]='8'; for(int i=1; i<18; i++) { for(int j=1; j<18; j++) { cout<<a[i][j]; } cout<<endl; } } if(xx==16&&yx==16) break; } system("cls"); cout<<"游戏结束"; }//第一次加载可能加载不出来,多编译运行几次就好了,等显示加载完毕再按WASD开始