C++迷宫探险1.0出炉了!!

· · 休闲·娱乐

目前只做了基础板块,没有脏矩阵处理和状态机,但有随机地图和小怪日常巡逻。
如果你想更改或者升级或者催更都请私信me.

#include<bits/stdc++.h>
#include<windows.h> 
#include<conio.h>
using namespace std; 
const int N=33;
const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
enum Monster{
    Chasing,
    Resting,
    Going
}em[4];
struct node {
    int x;
    int y;
    int hp;
    int kl;
    int level;
    node() : x(1), y(1), hp(100), level(1), kl(0) {}
}pl;
POINT mon[3];
bool dead[3];
char maze[N][N];
unordered_map<int,int> mp;
void move(int x,int y){
    COORD coord;
    coord.X=x;coord.Y=y;
    SetConsoleCursorPosition(hConsole, coord);
}
void dfs(int x,int y){
    maze[x][y]=' ';
    POINT dis[4]={{2,0},{-2,0},{0,2},{0,-2}};
    for(int i=3;i>=0;i--){
        int j=rand()%(i+1);
        POINT t=dis[i];dis[i]=dis[j];dis[j]=t;
    }
//  cout<<"Maze was in"<<x<<' '<<y<<"of \'"<<maze[x][y]<<'\''<<'\n';
    for(int i=0;i<=3;i++){
        int nx=dis[i].x+x;
        int ny=dis[i].y+y;
        if(nx>0 && ny>0 && nx<20 && ny<20 && maze[nx][ny]=='#'){
            maze[dis[i].x/2+x][dis[i].y/2+y]=' ';
            dfs(nx,ny);
        }
    }
    return ;
}
void m(int x,int y,char p){
    POINT dis[4]={{1,0},{-1,0},{0,1},{0,-1}};
    if(p=='M'){
        int nx,ny;
        do{
            POINT t=dis[rand()%4];
            nx=mon[x].x+t.x;ny=mon[x].y+t.y;
        }while(maze[nx][ny]=='#');
        if(nx==pl.x && ny==pl.y) pl.hp-=10,dead[x]=1;
        else maze[nx][ny]='Q';
        maze[mon[x].x][mon[x].y]=' ';
        /*move(nx+4,0);
        for(int i=0;i<20;i++)
            cout<<maze[nx][i];
        move(x+4,0);
        for(int i=0;i<20;i++)
            cout<<maze[x][i];*/
    } else {
        //int poi=p-'0';
        //POINT t=dis[poi];
        int nx=x+pl.x,ny=y+pl.y;
        if(maze[nx][ny]=='Q')
            pl.hp-=5,maze[nx][ny]=' ',pl.kl++,dead[mp[nx*20+ny]]=1;
        if(maze[nx][ny]!='#')
            pl.x=nx,pl.y=ny;
        /*move(nx+4,0);
        for(int i=0;i<20;i++){
            if(i==ny) cout<<'P';
            else cout<<maze[nx][i];
        }
        move(x+4,0);
        for(int i=0;i<20;i++)
            cout<<maze[x][i];*/
    }
    system("cls");
    cout<<"Adventruing :\n";
    cout<<"Level : "<<pl.level<<" .\n";
    cout<<"Health (HP) : "<<pl.hp<<" .\n";
    cout<<"Killed "<<pl.kl<<" Monsters.\n";
    for(int i=0;i<21;cout<<'\n',i++)
        for(int j=0;j<21;j++){
            if(i==pl.x && j==pl.y) cout<<'P';
            else cout<<maze[i][j];
        }
}
void Maze(){
    for(int i=0;i<21;i++)
        for(int j=0;j<21;j++)
            maze[i][j]='#';
    srand(time(0));
    dfs(1,1);
//  cout<<"Maze was done";
    maze[1][1]='S';
    maze[19][19]='E';
    for(int i=1;i<=3;i++){
        while(true){
            int nx=rand()%19+1,ny=rand()%19+1;
            if(maze[nx][ny]==' '){
                mon[i]={nx,ny};
                mp[nx*20+ny]=i;
                maze[nx][ny]='Q';
                break;
            }
        }
    }
    return ;
}
int main(){
    cout<<"---Maze Adventrue---\n";
    cout<<"1.New Game\n";
    //cout<<"2.Load Game\n";
    cout<<"3.Exit\n";
    do{
        char ch=_getch();
        if(ch=='1'){
            Maze();
            break;
        } else if(ch=='2'){
            //Maze();
            break;
        } else if(ch=='3')
            break;
        else continue;
    }while(true);
    system("cls");
    cout<<"Adventruing :\n";
    cout<<"Level : "<<pl.level<<" .\n";
    cout<<"Health (HP) : "<<pl.hp<<" .\n";
    cout<<"Killed "<<pl.kl<<" Monsters.\n";
    for(int i=0;i<21;cout<<'\n',i++)
        for(int j=0;j<21;j++){
            if(i==pl.x && j==pl.y) cout<<'P';
            else cout<<maze[i][j];
        }int c=0;
    while(true){
        if(pl.x==19 && pl.y==19){
            pl.x=1;pl.y=1;
            memset(dead,0,sizeof dead);
            mp.clear();
            cout<<"Get to New level!!";
            Maze();
            system("cls");
            cout<<"Adventruing :\n";
            cout<<"Level : "<<++pl.level<<" .\n";
            cout<<"Health (HP) : "<<pl.hp<<" .\n";
            cout<<"Killed "<<pl.kl<<" Monsters.\n";
            for(int i=0;i<21;cout<<'\n',i++)
                for(int j=0;j<21;j++){
                    if(i==pl.x && j==pl.y) cout<<'P';
                    else cout<<maze[i][j];
                }
        }
        if(_kbhit()){
            char ch=_getch();
            switch(ch){
                case 'W': case 'w': m(-1,0,'1');break; //Up
                case 'S': case 's': m(1,0,'2');break; //Down
                case 'A': case 'a': m(0,-1,'3');break; //Right
                case 'D': case 'd': m(0,1,'4');break; //Left
            } 
        }c++;Sleep(1);
        for(int i=1;i<=3;i++){
            if(!dead[i]){
                /*if(em[i]==Chasing)
                    m(i,0,'M');
                else */if(c>=200){
                    m(i,1,'M');
                    c=0;
                }
            }
        }
    }
    return 0; 
}