这是一个类似魔塔的控制台c++小游戏

    技术2023-09-09  122

    这个也是以前写的 写到商人那边买武器哪里不知道咋写了 然后第三关的boos 也是这样 里面有两种模式,也用了多线程提速 1.是手动模式,2.是自动模式 这个是深度寻路算法 手动模式第一关

    手动模式第二关

    Maze.h

    #pragma once #pragma once #include <iostream> #include <vector> #include <stack> #include <conio.h> #include <Windows.h> #include <time.h> #include <thread> #include <mutex> using namespace std; #define ROW 30 #define COL 30 #define ONE 1 enum face { f_down, f_up, f_left, f_right }; class Maze { public: Maze() = default; void initMaze(); //void gotoxy(); private: }; class Mazepos { public: Mazepos(int y, int x) :x{ x }, y{ y } { x_x = 3, y_y = 6, x_x_x = 28, y_y_y = 1,// 2828 n = 1, life = 10, trap = 1, isgirl = true, iseye = true, istrap = false, egg = true, door = true, inttrap = 0, next = 0, money = 100; } Mazepos() = default; void mymove(); void showmap(); void shownovice(); void girlmove(); void randgirl(); int getx() { return x; } int gety() { return y; } int get_n() { return n; } int get_life() { return life; } int getx_x() { return x_x; } int gety_y() { return y_y; } int getx_x_x() { return x_x_x; } int gety_y_y() { return y_y_y; } int get_inttrap() { return inttrap; } int get_next() { return next; } int get_money() { return money; } bool get_istrap() { return istrap; } bool get_door() { return door; } void set_door(bool n) { door = n; } void set_inttrap(int b) { inttrap += b; if (inttrap >= 30) inttrap = 0; } void setx1() { x--; } void sety1() { y--; } void setx2() { x++; } void sety2() { y++; } void setn() { n++; } void set_life(int s) { life -= s; } void set_life() { life++; } void set_istrap(bool booll) { istrap = booll; } void set_next(int n) { if (next == 0) { next++; } else if (n == 10) { next--; } else if (n == 11) { next++; } } void set_money(int n) { money += n; } private: int x, y; // 人 int x_x, y_y; // 鬼 int x_x_x, y_y_y; // 鬼2 int n; // 钥匙 int life; // 生命 int trap; // 陷阱 bool isgirl; // 鬼锁 bool iseye; // 鬼锁2 bool istrap; // 陷阱锁 bool egg; // 彩蛋锁 bool door; // 线程门锁 int inttrap; // 自雷帧 int next; // 下一关 int money; }; class Mazeauxiliary { public: Mazeauxiliary() { val = 0; isFind = 0; fid; }; //fid = f_down; int getval() { return val; } bool getisFind() { return isFind; } void playMaze(); face fid; private: int val; bool isFind; };

    Maze.cpp

    #include "Maze.h" void iniDate(); vector<vector<vector<int>>>map = { { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {10,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,1,1}, {1,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, {1,0,1,0,1,0,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, {1,0,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1}, {1,0,4,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,4,0,1,1,1}, {1,0,1,1,0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,1,1,1,0,1,6,1,0,0,0,1}, {1,0,1,1,0,1,0,1,0,1,1,0,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1}, {1,0,1,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1,1,1,1,0,1,1,1,1,1,0,1}, {1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1,1,1,1,0,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,0,0,0,0,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,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, {1,0,1,0,1,0,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, {1,0,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1}, {1,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,1,0,1}, {1,0,1,1,0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,1,1,1,0,1,5,1,0,0,0,1}, {1,0,1,1,0,1,5,1,0,1,1,0,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1}, {1,0,1,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,0,1,1,1,1,0,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,1,1,1,1,0,1,1,1,1,1,11,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, } }; // 钥匙坐标 x 6 y 10 void gotoxy(int x, int y) { COORD coord{ x,y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; } void Mazepos::showmap() { //for (int n = 0; n <= this->next; n++) { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (i == this->y && j == this->x) cout << "♂"; else if (i == this->y_y && j == this->x_x) cout << "♀"; else if (i == this->y_y_y && j == this->x_x_x) cout << "◎"; else if (map[next][i][j] == 1) cout << "■"; else if (map[next][i][j] == 0) cout << " "; else if (map[next][i][j] == 10 || map[next][i][j] == 11) cout << "¤"; else if (map[next][i][j] == 4) cout << "▲"; else if (map[next][i][j] == 5) cout << "∮"; else if (map[next][i][j] == 6) cout << "★"; else if (map[next][i][j] == 7) cout << "×"; else if (map[next][i][j] == 8) cout << "±"; else if (map[next][i][j] == 9) cout << "$"; } cout << endl; } //} } void Mazepos::mymove() { go: if (_kbhit()) { int ch = _getch(); switch (ch) { case 'w':if (map[next][y - 1][x] != 1 && map[next][y - 1][x] != 4 && map[next][y - 1][x] != 10 && map[next][y - 1][x] != 9 || map[next][y - 1][x] == 4 && n > 0) { y--; if (map[next][y][x] == 5) { map[next][y][x] = 0; n++; } else if (map[next][y][x] == 4 && n > 0) { map[next][y][x] = 0; n--; } else if (map[next][y][x] == 6) { map[next][y][x] = 0; life++; } else if (map[next][y][x] == 8) { map[next][y][x] = 0; trap++; } } else if (map[next][y - 1][x] == 10) set_next(10); else if (map[next][y - 1][x] == 9) { gotoxy(35, 35); cout << "商人: 返回0"; cout << "1.钥匙 5金,2.生命+1 10金"; gotoxy(35, 36); cout << "3.手枪 100金 4.稿子200金"; char ch; cin >> ch;//std::getline(cin, ch); switch (ch) { case '1':set_money(-5); setn(); goto go; case '2':set_money(-10); set_life(); goto go; case '3': break; case '4': break; case '5': break; } } break; case 's':if (map[next][y + 1][x] != 1 && map[next][y + 1][x] != 4 && map[next][y + 1][x] != 10 && map[next][y + 1][x] != 9 || map[next][y + 1][x] == 4 && n > 0) { y++; if (map[next][y][x] == 5) { map[next][y][x] = 0; n++; } else if (map[next][y][x] == 4 && n > 0) { map[next][y][x] = 0; n--; } else if (map[next][y][x] == 6) { map[next][y][x] = 0; life++; } else if (map[next][y][x] == 8) { map[next][y][x] = 0; trap++; } } else if (map[next][y + 1][x] == 10) set_next(10); else if (map[next][y + 1][x] == 9) { gotoxy(35, 35); cout << "商人: 返回0"; cout << "1.钥匙 5金,2.生命+1 10金"; gotoxy(35, 36); cout << "3.手枪 100金 4.稿子200金"; char ch; cin >> ch;//std::getline(cin, ch); switch (ch) { case '1':set_money(-5); setn(); goto go; case '2':set_money(-10); set_life(); goto go; case '3': break; case '4': break; case '5': break; } } break; case 'a':if (map[next][y][x - 1] != 1 && map[next][y][x - 1] != 4 && map[next][y][x - 1] != 10 && map[next][y][x - 1] != 9 || map[next][y][x - 1] == 4 && n > 0) { x--; if (map[next][y][x] == 5) { map[next][y][x] = 0; n++; } else if (map[next][y][x] == 4 && n > 0) { map[next][y][x] = 0; n--; } else if (map[next][y][x] == 6) { map[next][y][x] = 0; life++; } else if (map[next][y][x] == 8) { map[next][y][x] = 0; trap++; } } else if (map[next][y][x - 1] == 10) set_next(10); else if (map[next][y][x - 1] == 9) { gotoxy(35, 35); cout << "商人: 返回0"; cout << "1.钥匙 5金,2.生命+1 10金"; gotoxy(35, 36); cout << "3.手枪 100金 4.稿子200金"; char ch; cin >> ch;//std::getline(cin, ch); switch (ch) { case '1':set_money(-5); setn(); goto go; case '2':set_money(-10); set_life(); goto go; case '3': break; case '4': break; case '5': break; } } break; case 'd':if (map[next][y][x + 1] != 1 && map[next][y][x + 1] != 4 && map[next][y][x + 1] != 10 && map[next][y][x + 1] != 9 || map[next][y][x + 1] == 4 && n > 0) { x++; if (map[next][y][x] == 5) { map[next][y][x] = 0; n++; } else if (map[next][y][x] == 4 && n > 0) { map[next][y][x] = 0; n--; } else if (map[next][y][x] == 6) { map[next][y][x] = 0; life++; } else if (map[next][y][x] == 8) { map[next][y][x] = 0; trap++; } } else if (map[next][y][x + 1] == 10) set_next(10); else if (map[next][y][x + 1] == 9) { gotoxy(35, 35); cout << "商人: 返回0"; cout << "1.钥匙 5金,2.生命+1 10金"; gotoxy(35, 36); cout << "3.手枪 100金 4.稿子200金"; Sleep(100); char ch; cin >> ch;//std::getline(cin, ch); switch (ch) { case '1':set_money(-5); setn(); goto go; case '2':set_money(-10); set_life(); goto go; case '3': break; case '4': break; case '5': break; } } break; case 32:if (trap > 0) { map[next][y][x] = 7; set_istrap(true); trap--; }break; } } } void Mazepos::shownovice() { gotoxy(62, 3); cout << "新手提示: 上:w 下:s 左:a 右:d 空格:陷阱"; gotoxy(62, 4); cout << "♀:女鬼"; gotoxy(62, 5); cout << "大眼怪:◎"; gotoxy(62, 6); cout << "▲:大门"; gotoxy(62, 7); cout << "∮:钥匙"; gotoxy(62, 8); cout << "★:生命值永久+1"; gotoxy(62, 9); cout << "¤:下一关"; gotoxy(62, 10); cout << "×:陷阱"; gotoxy(62, 11); cout << "±:陷阱永久+1"; gotoxy(62, 12); cout << "$:商人"; gotoxy(62, 20); cout << "当前钥匙:" << n; gotoxy(62, 22); cout << "当前陷阱:" << trap; gotoxy(62, 24); cout << "当前关卡:" << next; gotoxy(62, 26); cout << "当前金币:" << money; gotoxy(62, 28); cout << "当前生命值:"; for (int i = 1; i <= life; i++) cout << "★" << ends; } void Mazeauxiliary::playMaze() { // 自动模式 Mazeauxiliary pat[ONE][ROW][COL]; for (int n = 0; n < ONE; n++) { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { pat[n][i][j].val = map[n][i][j]; } } } Mazepos mypos; Mazeauxiliary ini; Mazepos begPos = { 15, 15 }; Mazepos endPos = { 1, 0 }; stack<Mazepos> mystack; Mazepos currentPos = begPos;//当前点 Mazepos searchPos; //试探点 bool isFindEnd = false; //int y = begPos.gety(); //int x = begPos.getx(); pat[0][begPos.gety()][begPos.getx()].isFind = true; // 标记初始位置走过 mystack.push(begPos); while (!isFindEnd) { gotoxy(0, 0); //system("cls"); searchPos = currentPos; switch (pat[0][currentPos.gety()][currentPos.getx()].fid) { case f_down:searchPos.sety2(); pat[0][currentPos.gety()][currentPos.getx()].fid = f_up; if (pat[0][searchPos.gety()][searchPos.getx()].isFind != 1 && //没走过 pat[0][searchPos.gety()][searchPos.getx()].val != 1) { // 不是墙 pat[0][searchPos.gety()][searchPos.getx()].isFind = true; mystack.push(searchPos); currentPos = searchPos; } break; case f_up:searchPos.sety1(); pat[0][currentPos.gety()][currentPos.getx()].fid = f_left; if (pat[0][searchPos.gety()][searchPos.getx()].isFind != 1 && //没走过 pat[0][searchPos.gety()][searchPos.getx()].val != 1) { // 不是墙 pat[0][searchPos.gety()][searchPos.getx()].isFind = true; mystack.push(searchPos); currentPos = searchPos; } break; case f_left:searchPos.setx1(); pat[0][currentPos.gety()][currentPos.getx()].fid = f_right; if (pat[0][searchPos.gety()][searchPos.getx()].isFind != 1 && //没走过 pat[0][searchPos.gety()][searchPos.getx()].val != 1) { // 不是墙 pat[0][searchPos.gety()][searchPos.getx()].isFind = true; mystack.push(searchPos); currentPos = searchPos; } break; case f_right:searchPos.setx2(); if (pat[0][searchPos.gety()][searchPos.getx()].isFind != 1 && //没走过 pat[0][searchPos.gety()][searchPos.getx()].val != 1) { // 不是墙 pat[0][searchPos.gety()][searchPos.getx()].isFind = true; mystack.push(searchPos); currentPos = searchPos; } else {//回退 mystack.pop(); currentPos = mystack.top(); } break; } Sleep(10); Mazepos m(currentPos.gety(), currentPos.getx()); m.showmap(); if (endPos.gety() == currentPos.gety() && endPos.getx() == currentPos.getx()) { isFindEnd = true; } if (mystack.empty()) break; } } void Mazepos::girlmove() { if (isgirl) { Mazeauxiliary pat; int girl = rand() % 4; if (girl == 0) pat.fid = f_down; else if (girl == 1) pat.fid = f_up; else if (girl == 2) pat.fid = f_left; else if (girl == 3) pat.fid = f_right; switch (pat.fid) { case f_down:if (map[next][this->y_y + 1][this->x_x] != 1 && map[next][this->y_y + 1][this->x_x] != 10) y_y++; break; case f_up:if (map[next][this->y_y - 1][this->x_x] != 1 && map[next][this->y_y - 1][this->x_x] != 10) y_y--; break; case f_left:if (map[next][this->y_y][this->x_x - 1] != 1 && map[next][this->y_y][this->x_x - 1] != 10) x_x--; break; case f_right:if (map[next][this->y_y][this->x_x + 1] != 1 && map[next][this->y_y][this->x_x + 1] != 10) x_x++; break; } if (map[next][this->y_y][this->x_x] == 7) { map[next][this->y_y][this->x_x] = 0; y_y = -1; x_x = -1; isgirl = false; } } if (iseye) { Mazeauxiliary pat2; int girl2 = rand() % 4; if (girl2 == 0) pat2.fid = f_down; else if (girl2 == 1) pat2.fid = f_up; else if (girl2 == 2) pat2.fid = f_left; else if (girl2 == 3) pat2.fid = f_right; switch (pat2.fid) { case f_down:if (map[next][this->y_y_y + 1][this->x_x_x] != 1 && map[next][this->y_y_y + 1][this->x_x_x] != 10) y_y_y++; break; case f_up:if (map[next][this->y_y_y - 1][this->x_x_x] != 1 && map[next][this->y_y_y - 1][this->x_x_x] != 10) y_y_y--; break; case f_left:if (map[next][this->y_y_y][this->x_x_x - 1] != 1 && map[next][this->y_y_y][this->x_x_x - 1] != 10) x_x_x--; break; case f_right:if (map[next][this->y_y_y][this->x_x_x + 1] != 1 && map[next][this->y_y_y][this->x_x_x + 1] != 10) x_x_x++; break; } if (map[next][this->y_y_y][this->x_x_x] == 7) { map[next][this->y_y_y][this->x_x_x] = 0; y_y_y = -1; x_x_x = -1; iseye = false; } } if (this->x_x == this->x_x_x && this->y_y == this->y_y_y && egg) { //彩蛋 system("cls"); egg = false; Sleep(100); gotoxy(62, 12); cout << "彩蛋触发:"; for (int i = 1; i <= 6; i++) { map[next][i][1] = 4; randgirl(); } int y = rand() % 29; int x = rand() % 29; if (map[next][y][x] != 1 && map[next][y][x] != 3 && map[next][y][x] != 4 && // 随机概率出陷阱 map[next][y][x] != 5 && map[next][y][x] != 6 && map[next][y][x] != 7 && map[next][y][x] != 8) map[next][y][x] = 8; } } void Mazepos::randgirl() { A: int y = rand() % 29; int x = rand() % 29; if (map[next][y][x] != 1 && map[next][y][x] != 3 && map[next][y][x] != 4 && map[next][y][x] != 5 && map[next][y][x] != 6 && map[next][y][x] != 7 && map[next][y][x] != 8) map[next][y][x] = 5; else goto A; } void showmapthr(Mazepos& pos) { if (pos.get_door()) { while (true) { if (pos.get_door()) { Sleep(30); gotoxy(0, 0); pos.showmap(); pos.shownovice(); //showmapthr(pos); } } } system("cls"); } void showmovethr(Mazepos& pos) { Maze m; mutex lock; //lock.lock(); if (pos.get_door()) { while (true) { pos.mymove(); pos.girlmove(); if (pos.getx() == 0 && pos.gety() == 1) { Sleep(100); gotoxy(70, 16); cout << "恭喜你通关了 1继续 2下一关 3关闭"; char b; cin >> b; if (b == '1') pos.set_next(1); m.initMaze();//goto A;// break; } else if (pos.getx() == pos.getx_x() && pos.gety() == pos.gety_y()) { pos.set_life(10); system("cls"); } else if ((pos.getx() == pos.getx_x_x() && pos.gety() == pos.gety_y_y())) { pos.set_life(5); system("cls"); } else if (map[pos.get_next()][pos.gety()][pos.getx()] == 7 && pos.get_inttrap() >= 20) { pos.set_life(10); map[pos.get_next()][pos.gety()][pos.getx()] = 0; } if (pos.get_life() <= 0) { pos.set_door(false); Sleep(100); //map.clear(); //iniDate(); gotoxy(70, 16); cout << "哇你输了 1继续 2关闭"; A: char b; gotoxy(70, 16); cin >> b; if (b == '1') { pos.set_door(false); m.initMaze();//goto A; // 初始化地图 //m.initMaze();//goto A; } else if (b == '2') { break; } else { gotoxy(70, 16); cout << "请重新输入:"; goto A; } break; //lock.unlock(); } if (pos.get_istrap()) pos.set_inttrap(1); Sleep(50); } } //A: //m.initMaze(); } void Maze::initMaze() { srand((unsigned int)time(0)); A: iniDate(); // 初始化数组 system("cls"); gotoxy(100, 15); cout << "垃圾画布"; gotoxy(100, 17); cout << "1:手动模式"; gotoxy(100, 19); cout << "2:自动模式"; char a; cin >> a; system("cls"); if (a == '1') { Mazepos pos(15, 15); thread thr = thread(showmapthr, ref(pos)); //线程模式 thread thr2 = thread(showmovethr, ref(pos)); thr.join(); thr2.join(); // while (true) { // 这个是没线程的版本 // gotoxy(0, 0); // bool isPeole = true; // pos.showmap(); // pos.mymove(); // pos.shownovice(); // pos.girlmove(); // if (pos.getx() == 0 && pos.gety() == 1) { // gotoxy(70, 16); // cout << "恭喜你通关了 1继续 2下一关 3关闭"; // char b; // cin >> b; // if (b == '1') // pos.set_next(1); // goto A;//m.initMaze(); // break; // } // else if (pos.getx() == pos.getx_x() && pos.gety() == pos.gety_y()) { // pos.set_life(10); // system("cls"); // } // else if ((pos.getx() == pos.getx_x_x() && pos.gety() == pos.gety_y_y())) { // pos.set_life(5); // system("cls"); // } // else if (map[pos.get_next()][pos.gety()][pos.getx()] == 7 && pos.get_inttrap() >= 20) { // pos.set_life(10); // map[pos.get_next()][pos.gety()][pos.getx()] = 0; // } // if (pos.get_life() <= 0) { // pos.set_door(false); // map.clear(); // map = { //{ //{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //{3,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,1,1}, //{1,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, //{1,0,1,0,1,0,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, //{1,0,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, //{1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1}, //{1,0,4,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, //{1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1}, //{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,4,0,1,1,1}, //{1,0,1,1,0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,1,1,1,0,1,6,1,0,0,0,1}, //{1,0,1,1,0,1,0,1,0,1,1,0,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1}, //{1,0,1,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,0,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"哇你输了 1继续 2关闭"; // char b; // gotoxy(70, 16); // cin >> b; // if (b == '1') { // pos.set_door(false); // goto A; // m.initMaze();//goto A; // } // break; // } // if (pos.get_istrap()) // pos.set_inttrap(1); // Sleep(50); // } } else if (a == '2') { Mazeauxiliary m; m.playMaze(); gotoxy(70, 16); cout << "恭喜你通关了 1继续 2下一关 3关闭"; char b; cin >> b; if (b == '1') goto A; } else { gotoxy(70, 17); cout << "请重新输入"; Sleep(100); goto A; } } void iniDate() { vector<vector<vector<int>>>map = { { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {10,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,1,1}, {1,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, {1,0,1,0,1,0,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,0,1,1}, {1,0,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1}, {1,0,4,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,4,0,1,1,1}, {1,0,1,1,0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,1,1,1,0,1,6,1,0,0,0,1}, {1,0,1,1,0,1,0,1,0,1,1,0,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1}, {1,0,1,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,1,1,1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