C++Game

· · 个人记录

随机变换迷宫

#include <bits/stdc++.h>
#include <conio.h>
using namespace std;

const int N=1000;

int x=1,y=1,n,m;

char a[N][N];

int main(){
    int s;
    cout<<"请输入种子:";
    cin>>s;
    srand(s);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int n=30,m=30;
    memset(a,'.',sizeof(a));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(i==y&&j==x)cout<<"@";
            else cout<<a[i][j];
        }
        cout<<endl;
    }
    while(1){
        bool ll=0;
        int t=_getch();
        if(t==224||t==0){
            int t=_getch(); 
            switch(t){
                case 72:
                    y--;
                    if(a[y][x]=='#'||y<=0)y++,ll=1;
//                  cout<<x<<" "<<y<<endl;
                    break;
                case 80:
                    y++;
                    if(a[y][x]=='#'||y>n)y--,ll=1;
//                  cout<<x<<" "<<y<<endl;
                    break;
                case 75:
                    x--;
                    if(a[y][x]=='#'||x<=0)x++,ll=1;
//                  cout<<x<<" "<<y<<endl;
                    break;
                case 77:  
                    x++;
                    if(a[y][x]=='#'||x>m)x--,ll=1;
//                  cout<<x<<" "<<y<<endl;
                    break;
            }
            if(ll)continue;
            system("cls");
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    a[i][j]=(rand()%3==0?'#':'.');
                    if(i==y&&j==x)cout<<"@";
                    else cout<<a[i][j];
                }
                cout<<endl;
            }
        }
    }




    return 0;
}