数字迷宫

这几天写这个数字迷宫真的不容易。。。。。找bug时间有点长。。。。完善了几个模式,挺好玩的

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <iostream>
#include <time.h>
#include <dos.h>
using namespace std;
void main_show(){
    cout << endl << endl << endl << endl << endl;
    printf("\t\t\t\t欢迎来到数字迷宫\n"); 
    printf("\t\t\t\t  1:经典迷宫\n");
    printf("\t\t\t\t  2:绝命逃亡\n");
    printf("\t\t\t\t  3:  PVP\n");
    printf("\t\t\t\t  4:  PVE\n");
    printf("\t\t\t\t  5:查看规则\n");
    printf("\t\t\t\t  6:退出游戏\n");

}
/***********************************************
                   game函数 
************************************************/
int map[17][24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,-1,0,0,0,0,0,-1,-1,-1,0,0,0,-1,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,-1,0,0,0,0,0,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,0,-1,0,-1,-1,-1,-1,-1,-1,-1,0,0,0,-1,0,0,-1,0,-1,0,0,0,0,-1,0,-1,-1,-1,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,0,-1,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,-1,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,0,0,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,-1,-1,-1,0,-1,-1,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,-1,-1,-1,-1,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int game_player_x,game_player_y; 
int map1[17][24];
int game_number[17];
struct root{
    int a,b;
}vis_space[100],vis_number[100];
struct road{
    int x;
    int y;
}root[1000];
int hp;
int vis_num = 1;
int book[200][200] = {0};
int step1,step2,step;
int esc;
int end,start;
int game_d();
int game_p();
void time();
void ruler_k();
void game_ruler();
void game_map(int a);//打印游戏界面
void game_mark();//统计游戏成绩    
void game_player_printf(int x,int y,char face);
void game_player_printf1(int x,int y,int a);
void game_player_move_d();
void game_player_move_p();
void game_num();
void game_ch();
int game_finsh_p();
int game_finsh_d();
int game_finsh_j();
char num_d;
char game_cho;
int game_j();
int t,sl;
int flag;
DWORD WINAPI Find(LPVOID lpParamter);
DWORD WINAPI Fun(LPVOID lpParamter){
    while(num_d <= '9'){
        if(esc == 1)
            return 0;
        game_player_printf1(61,9,t); 
        t--;
        if(t == 8)
        game_player_printf(62,9,32); 
        if(t==0){
            game_player_printf1(61,9,t);
            return 0;   
        }
        Sleep(1000);   
    }  
    return 0L;
}
void game_num()
{
    int x=0;
    int y=0;
    int num = 1;
    int sum = 5;
    cout << endl << endl << endl << endl << endl << endl << endl;

    cout << "                                   LOADING" << endl << endl;
    cout << "                               ";
    ruler_k(); 
    game_player_printf(28,9,32);
    while(1){
    //  srand((unsigned)time(NULL));
        x=rand()%vis_num-1;
        Sleep(1000);
        if(book[vis_space[x].a][vis_space[x].b] == 1 || vis_space[x].a == 2&&vis_space[x].b==3)
            continue;                           
        vis_number[num].a = vis_space[x].b*2;
        vis_number[num].b = vis_space[x].a+2;
        book[vis_space[x].a][vis_space[x].b] = 1;
        cout << "▇";                     
        num++;
        if(num==10)
        break;
    }
}
void game_player_printf(int x, int y,char face)//x为两倍,y一倍+2 
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleCursorPosition(hOut, pos);
    cout << face << " ";
}
void game_player_printf1(int x, int y,int a)//x为两倍,y一倍+2 
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleCursorPosition(hOut, pos);
    cout << a ;
}
void game_map(int a){
    system("cls");
    if(a==2){
        cout << endl << endl;
        for(int i = 0;i<17;i++){
            for(int j = 0;j < 24;j++){
                if(map[i][j] == 0)
                    cout << "■";
                else if(map[i][j] == -1){
                    cout << "  ";
                    vis_space[vis_num].a = i;
                    vis_space[vis_num].b = j;
                    vis_num++;
                }
                else
                    cout << map[i][j] << " ";
            }
            if(i == 0)
                cout << "      得分     已走步数"; 
            if(i == 5)
                cout << "          倒计时"; 
            if(i == 10)
                cout <<"         操作说明";
            if(i == 12)
                cout <<"           w  ↑";
            if(i == 13)
                cout <<"           a  ←";
            if(i == 14)
                cout <<"           s  ↓";
            if(i == 15)
                cout <<"           d  →"; 
            cout << endl;

        }
        for(int i = 1;i<=9;i++){
            char c = i + '0';
            game_player_printf(vis_number[i].a,vis_number[i].b,c);
        }
    }
    else{
        for(int i = 0;i<17;i++){
            for(int j = 0;j < 24;j++){
                if(map[i][j] == 0){
                }
                else{
                    vis_space[vis_num].a = i;
                    vis_space[vis_num].b = j;
                    vis_num++;
                }
            }
        }
    }
}
/******************************
            单人模式 
******************************/
int game_d(){
    esc = 0;
    game_ch();
    game_map(1);
    game_num();
    game_map(2);
    Sleep(2000);
    game_player_move_d();
    if(esc == 1)
        return 2;
    return game_finsh_d();
}
void game_ch(){
    system("cls");
    cout << endl << endl << endl << endl << endl << endl <<"                                请选择模式";
    cout << endl <<"                                 1 简单";
    cout << endl <<"                                 2 普通";
    cout << endl <<"                                 3 困难";
    char ch;
    ch = getch();
    if(ch == '1'){
        sl = 300;
        t = 21;
    }
    else if(ch == '2'){
        sl = 200;
        t = 14;
    }
    else{
        sl = 100;
        t = 7;
    }

}
int game_finsh_d(){
    while(1){
        system("cls");
        int rot = 9*(300-step1/2);
        if(flag == 1){  
            cout << endl << endl << endl << endl << endl << "                    恭喜你,闯关成功!" << endl << endl; 
            cout << "                       得分:"  << rot << endl;
        }
        else{
            cout << endl << endl << endl << endl << endl << "                    很遗憾,闯关失败!" << endl << endl;
        }
        cout << "              1 -> 再来一局  Esc -> 返回主界面" << endl;
        char choose;
        choose = getch();
        if(choose == '1')
            return 1; 
        if(choose == 27)
            return 2;
        else
            cout << "您的输入有误" << endl;
        cout << "按任意键继续";
        system("cls"); 
    } 
}
void game_player_move_d(){
        int  sum = 1;
        game_player_x = 2;
        game_player_y = 3;
        game_player_printf(game_player_x,game_player_y,1);
        int ch;
        num_d = '1';
        step = 1;
        game_player_printf(56,4,'0');
        game_player_printf1(67,4,0);
        if(game_cho == '1'){
            HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
            CloseHandle(hThread);
        }
        flag = 0;
        hp = 1;
        end = 1;
        start = 1;
        root[1].x = 2;
        root[1].y = 3; 
        while(1){

                if(hp == 0&&game_cho == '2')
                    return ;
                if(game_cho == '2'){
                    if(sum == 4){
                        HANDLE hThread = CreateThread(NULL, 0, Find, NULL, 0, NULL);
                        CloseHandle(hThread);
                    }
                }
                sum ++;
                if(t==0)
                    return ;

                ch = getch();
                game_player_printf(game_player_x,game_player_y,1);
                int x = game_player_x;
                int y = game_player_y;
                switch(ch)
                {
                    case 80:
                        game_player_y++;
                        break;
                    case 72:
                        game_player_y--;
                        break;
                    case 75:
                        game_player_x -= 2;
                        break;
                    case 77:
                        game_player_x += 2;
                        break;
                    case 27:
                        esc = 1;
                        return ;
                        break;
                    default:
                        continue;
                }
                if(map[game_player_y-2][game_player_x/2] == 0){
                    game_player_x = x;
                    game_player_y = y;
                    continue;   
                }
                if(game_cho == '2'){
                    if(game_player_x == root[end].x && game_player_y == root[end].y)
                        return ;
                    root[start].x = game_player_x;
                    root[start].y = game_player_y;
                    start++;
                }


                game_player_printf(game_player_x,game_player_y,1);
                if(book[game_player_y-2][game_player_x/2] == 1){
                        game_player_printf(56,4,num_d);    
                        book[game_player_y-2][game_player_x/2] = 0;
                        num_d ++;
                }
                game_player_printf1(67,4,step);
                step += 1;


                game_player_printf(x,y,32);
                if(num_d > '9'){
                    flag = 1;
                    break;  
                }
                if(hp == 0){

                    return ;
                }
        }
}
/******************************
            绝命逃亡
******************************/
int game_j(){
    esc = 0;
    hp = 1;
    end = 1;
    start = 1; 
    game_ch();
    game_map(1);
    game_num();
    game_map(2);
    game_player_move_d();
    if(esc == 1)
        return 2;
    return game_finsh_j();
} 
DWORD WINAPI Find(LPVOID lpParamter)
{
    while(start > end){
        if(num_d > '9')
            break;
        Sleep(sl);
        game_player_printf(root[end].x,root[end].y,2);
        game_player_printf(root[end-1].x,root[end-1].y,32);
        end++;
        if(root[end - 1].x == game_player_x&&root[end - 1].y == game_player_y){
            hp = 0;
            return 0;
        }
    }
}
int game_finsh_j(){
    while(1){
        system("cls");
        int rot = 9*(300-step1/2);
        if(flag == 1){  
            cout << endl << endl << endl << endl << endl << "                    恭喜你,闯关成功!" << endl << endl; 
            cout << "                       得分:"  << rot << endl;
        }
        else{
            cout << endl << endl << endl << endl << endl << "                    很遗憾,闯关失败!" << endl << endl;
        }
        cout << "              1 -> 再来一局  Esc -> 返回主界面" << endl;
        char choose;
        choose = getch();
        if(choose == '1')
            return 1; 
        if(choose == 27)
            return 2;
        else
            cout << "您的输入有误" << endl;
        cout << "按任意键继续";
        system("cls"); 
    } 
}
/******************************
              PVP
******************************/
int game_p(){
    esc = 0;
    game_map(1);
    game_num();
    game_map(2);
    game_player_move_p();
    if(esc == 1)
        return 2;
    return game_finsh_p();
}
void game_player_move_p(){
        int game_player_x1 = 26,game_player_x2 = 2;
        int game_player_y1 = 17,game_player_y2 = 3;
        game_player_printf(game_player_x1,game_player_y1,1);
        game_player_printf(game_player_x2,game_player_y2,2);
        int ch;
        char num1 = '1';
        char num2 = '1';
        step1 = 1;
        step2 = 1;
        game_player_printf(54,4,'0');
        game_player_printf(58,4,'0');
        game_player_printf1(65,4,0);
        game_player_printf1(68,4,0);
        while(1){
                int flag = 2;
                ch = getch();
                int x1 = game_player_x1;
                int y1 = game_player_y1;
                int x2 = game_player_x2;
                int y2 = game_player_y2;
                switch(ch)
                {
                    case 80:
                        game_player_y1++;
                        flag = 1;
                        break;
                    case 72:
                        game_player_y1--;
                        flag = 1;
                        break;
                    case 75:
                        game_player_x1 -= 2;
                        flag = 1;
                        break;
                    case 77:
                        game_player_x1 += 2;
                        flag = 1;
                        break;
                    case 27:
                        esc = 1;
                        return ;
                        break;
                    case 'w':
                        game_player_y2 --;
                        break;
                    case 's':
                        game_player_y2 ++;
                        break;
                    case 'a':
                        game_player_x2 -= 2;
                        break;
                    case 'd':
                        game_player_x2 +=2;
                        break; 
                    default:
                        continue;
                }
                if(flag == 1){
                    if(map[game_player_y1-2][game_player_x1/2] == 0||(game_player_y1==game_player_y2&&game_player_x1 == game_player_x2)){
                        game_player_x1 = x1;
                        game_player_y1 = y1;
                        continue;   
                    }   
                    game_player_printf(game_player_x1,game_player_y1,1);
                    if(book[game_player_y1-2][game_player_x1/2] == 1){
                        game_player_printf(54,4,num1);
                        book[game_player_y1-2][game_player_x1/2] = 0;
                        num1 ++;
                    }
                    game_player_printf1(65,4,step1);
                        step1 += 1;         
                    game_player_printf(x1,y1,32);
                }
                else{
                    if(map[game_player_y2-2][game_player_x2/2] == 0||(game_player_y1==game_player_y2&&game_player_x1 == game_player_x2)){
                        game_player_x2 = x2;
                        game_player_y2 = y2;
                        continue;
                    }
                    game_player_printf(game_player_x2,game_player_y2,2);
                    if(book[game_player_y2-2][game_player_x2/2] == 1){
                        game_player_printf(58,4,num2);
                        book[game_player_y2-2][game_player_x2/2] = 0;
                        num2 ++;
                    }
                    game_player_printf1(68,4,step2);
                    step2 += 1;            
                    game_player_printf(x2,y2,32);
                }
                if((num1 + num2) > '0'+'9' + 1)
                    break;
        }
}
int game_finsh_p(){
    while(1){
        system("cls");
        int rot1 = 9*(300-step1/2);
        int rot2 = 9*(300-step2/2);
        char face; 
        cout << endl << endl << endl << endl << endl << "                        游戏结束!" << endl << endl; 
        cout << "                     play1得分:"  << rot1 << endl;
        cout << "                     play2得分:"  << rot2 << endl;
        if(rot1>rot2){
            face = 1;
            cout <<"                    play1胜利"; 
            game_player_printf(58,6,face);
        }
        else{
            face = 2;
            cout <<"                    play2胜利"; 
            game_player_printf(58,6,face);
        }
        cout << endl << "             1 -> 再来一局  2 -> 返回主界面" << endl;
        char choose;
        choose = getch();
        if(choose == '1')
            return 1; 
        if(choose == 27)
            return 2;
        else
            cout << "您的输入有误" << endl;
        cout << "按任意键继续";
        system("cls"); 
    } 
}
/******************************
              PVE
******************************/

/***********************************************
                   ruler函数 
************************************************/
void ruler_k(){
    if(game_cho == '1'){
        COORD pos = {10,13};
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleCursorPosition(hOut, pos);
        cout << "经典迷宫:玩家只需要操作人物在时限内吃完所有数字即可获得胜利";
    }
    else if(game_cho == '2'){
        COORD pos = {9,13};
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleCursorPosition(hOut, pos);
        cout << "绝命逃亡:玩家在控制人物吃数字时会有怪物追踪玩家,遭遇怪物后游戏失败";
    }
    else if(game_cho == '3'){
        COORD pos = {10,13};
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleCursorPosition(hOut, pos);
        cout << "PVP:玩家1用↑↓←→控制方向,玩家2用wasd控制方向,分数多者获胜";
    }
}
void ruler(){
    cout << endl << endl << endl << endl << endl << endl;
    cout << "                               玩法介绍" << endl;
    cout << "    本游戏玩法简单,玩家只要控制wasd,就可以控制人物笑脸上下左右移动"; 
    cout << endl << endl << endl << "按任意键继续"; 
    char s = getch();
}
int main(){
    system("color 80"); 
    while(1){
        system("cls");
        main_show();
        cout << "                                请输入您的选择" << endl;
        game_cho = getch();
        if(game_cho< '1'||game_cho > '5'){
            cout << endl << "                              操作错误,请重新输入" << endl;
            Sleep(2000); 
            continue;
        }
        memset(book,0,sizeof(book));
        system("cls");
        if(game_cho == '1'){
            while(1){
                if(game_d()==2)
                    break;
            }   
        }
        else if(game_cho == '2'){
            while(1){
                if(game_j()==2)
                    break;
            }   
        }
        else if(game_cho == '3'){
            while(1){
                if(game_p()==2)
                    break; 
            }   
        }
        else if(game_cho == '4'){
            system("cls");
            cout << endl << endl << endl << endl; 
            cout << "          老夫还没写。。。。。只会dp+深搜+暴力+贪心。。。";
            cout << "                 老夫要写的好,就去写AlphaGo了";
            Sleep(5000);
        }
        else
            return 0;
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值