#include<bits/stdc++.h>
#include<Windows.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define ll long long
#define un unsigned
using namespace std;
void gotoxy(int y,int x){
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
int getRand(ll min,ll max){
return (rand()%(max-min+1))+min;
}
int n,ans,g[35][35],xx=20,yy=0,dg[905],cnt,cnt2;
bool vis[35][35];
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
void dfs(int x,int y){
if(x<1||x>n||y<1||y>n) return ;
if(x==n&&y==n){
cnt2=cnt;
ans++;
return ;
}
vis[x][y]=1;
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(!vis[tx][ty]&&g[tx][ty]==0){
dg[cnt++]=i;
dfs(tx,ty);
cnt--;
}
if(ans!=0) return ;
}
vis[x][y]=0;
}
void make_map(){
memset(g,-1,sizeof(g));
ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) g[i][j]=getRand(0,1);
gotoxy(xx,yy);
cout<<".加载中";
yy++;
yy%=120;
}
g[1][1]=0,g[n][n]=0;
dfs(1,1);
cout<<endl<<endl;
if(ans==0) make_map();
else return ;
}
int main(){
srand(time(0));
cout<<"按A键开始游戏!";
while(1) if(KEY_DOWN('A')) break;
system("cls");
cout<<"1.简单\n2.中等\n3.困难\n";
while(1){
if(KEY_DOWN('1')){
n=10;
break;
}
if(KEY_DOWN('2')){
n=20;
break;
}
if(KEY_DOWN('3')){
n=30;
break;
}
}
system("cls");
make_map();
system("cls");
cout<<" 规则:从左上角到右下角,按键:W(上)、S(下)、A(左)、D(右),‘@’表示当前位置。"
"0为路,1为墙。(如果走不出来可以按R键)\n";
system("pause");
system("cls");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cout<<g[i][j];
cout<<endl;
}
cout<<endl;
for(int i=1;i<=n+1;i++) for(int j=1;j<=n+1;j++) g[i][j-1]=g[i][j];
for(int i=0;i<n;i++) for(int j=1;j<=n+1;j++) g[j-1][i]=g[j][i];
xx=0,yy=0;
gotoxy(xx,yy);
cout<<"@";
while(xx!=(n-1)||yy!=(n-1)){
if(KEY_DOWN('W')){
if(g[xx-1][yy]==0){
gotoxy(xx,yy);
cout<<0;
xx--;
}
}
else if(KEY_DOWN('S')){
if(g[xx+1][yy]==0){
gotoxy(xx,yy);
cout<<0;
xx++;
}
}
else if(KEY_DOWN('A')){
if(g[xx][yy-1]==0){
gotoxy(xx,yy);
cout<<0;
yy--;
}
}
else if(KEY_DOWN('D')){
if(g[xx][yy+1]==0){
gotoxy(xx,yy);
cout<<0;
yy++;
}
}
else if(KEY_DOWN('R')){
int f;
system("cls");
cout<<"是(Y)否(N)观看教程?";
while(1){
if(KEY_DOWN('Y')){
f=1;
break;
}
else if(KEY_DOWN('N')){
f=0;
break;
}
}
system("cls");
if(f){
int x2=0,y2=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++) cout<<g[i][j];
cout<<endl;
}
gotoxy(x2,y2);
cout<<"@";
for(int i=0;i<cnt2;i++){
Sleep(800);
x2+=dx[dg[i]];
y2+=dy[dg[i]];
gotoxy(x2,y2);
cout<<"@";
}
}
cout<<"\n";
system("pause");
system("cls");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++) cout<<g[i][j];
cout<<endl;
}
gotoxy(xx,yy);
cout<<"@";
}
gotoxy(xx,yy);
cout<<"@";
Sleep(200);
}
system("cls");
cout<<"游戏结束!";
return 0;
}
过两天出带注释的