diff --git a/README.md b/README.md index 6e967b4..5e49e30 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,8 @@ C++ 控制台 迷宫 使用了自动生成迷宫算法,实现迷宫基本玩 所以该代码只能在windows下编译 程序参数可以在代码头部设置,例如迷宫大小和输出区域大小等 + +需要合适的字体才能正常显示 + + +![游玩示例](play.git) \ No newline at end of file diff --git a/a.exe b/a.exe index 4dfb839..59dafa7 100644 Binary files a/a.exe and b/a.exe differ diff --git a/code.cpp b/code.cpp index 9e2584f..5423293 100644 --- a/code.cpp +++ b/code.cpp @@ -9,10 +9,10 @@ using namespace std; // ʾĴС -const size_t sWidth = 30, sHeight = 24; +const size_t sWidth = 60, sHeight = 24; // ԹʵʴС -const size_t width = 9, height = 9; +const size_t width = 33, height = 15; const char wall = 1; const char path = 0; const char target = 2; @@ -34,10 +34,7 @@ vector, int> > walls; 2ĸѾͨ·ˣǾʹбƳǽ */ -const int up = 0; -const int down = 1; -const int left = 2; -const int right = 3; +enum Dir { Up, Down, Left, Right }; // һ int xx[] = { 0, 0, -1, 1 }; @@ -56,288 +53,280 @@ const int margin = 3; // ƶ굽xyλ void gotoxy(int x, int y) { - COORD coord = { (short)x, (short)y }; - SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); + COORD coord = { (short)x, (short)y }; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } // ȫͼΪǽ void setAllWall() { - for (int y = 0; y < height; ++y) - for (int x = 0; x < width; ++x) - map[y][x] = wall; + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) + map[y][x] = wall; } // ǷԽ bool isOutBounds(int x, int y) { - return (x<0 || x >= width - || y<0 || y >= height); + return (x<0 || x >= width + || y<0 || y >= height); } // ̽ void explore(int x, int y) { - // ĸ - for (int i = 0; i < 4; ++i) - { - // ˳һ· - for (int x1 = x, y1 = y; - !isOutBounds(x1, y1) - && map[y1][x1] == path; - x1 += xx[i], y1 += yy[i]) - { - // ÿһӼǷΪͨ· - for (int fy = y1 - 1; fy <= y1 + 1; ++fy) - for (int fx = x1 - 1; fx <= x1 + 1; ++fx) - if (!isOutBounds(fx, fy) - && fog[fy][fx]) - fog[fy][fx] = false; - } - } + // ĸ + for (int i = 0; i < 4; ++i) + { + // ˳һ· + for (int x1 = x, y1 = y; + !isOutBounds(x1, y1) + && map[y1][x1] == path; + x1 += xx[i], y1 += yy[i]) + { + // ÿһӼǷΪͨ· + for (int fy = y1 - 1; fy <= y1 + 1; ++fy) + for (int fx = x1 - 1; fx <= x1 + 1; ++fx) + if (!isOutBounds(fx, fy) + && fog[fy][fx]) + fog[fy][fx] = false; + } + } } // 괦ǽӵͨǽ void pushAroundWall(int x, int y) { - // ĸ - for (int i = 0; i < 4; ++i) - { - int tx = x + xx[i]; - int ty = y + yy[i]; - if (isOutBounds(tx, ty)) - continue; - int wx = x + x2[i]; - int wy = y + y2[i]; - if (isOutBounds(wx, wy)) - continue; - // ֻҪǽͼб - if (map[ty][tx] == wall) - walls.push_back(pair, int>(pair( tx,ty ),i )); - } + // ĸ + for (int i = 0; i < 4; ++i) + { + int tx = x + xx[i]; + int ty = y + yy[i]; + if (isOutBounds(tx, ty)) + continue; + int wx = x + x2[i]; + int wy = y + y2[i]; + if (isOutBounds(wx, wy)) + continue; + // ֻҪǽͼб + if (map[ty][tx] == wall) + walls.push_back(pair, int>(pair( tx,ty ),i )); + } } // չʾͼ void showMap() { - int dx = sWidth / 2 - playerx; - int dy = sHeight / 2 - playery; - // ƶ굽ʼλ - gotoxy(1, 1); + int dx = sWidth / 2 - playerx; + int dy = sHeight / 2 - playery; + // ƶ굽ʼλ + gotoxy(0, 0); - // ͼ - for (int sy = 0; sy < sHeight; ++sy) - { - for (int sx = 0; sx < sWidth; ++sx) - { - //putchar(map[y][x]?'@':' '); - int x = sx - dx; - int y = sy - dy; + // ͼ + for (int sy = 0; sy < sHeight; ++sy) + { + for (int sx = 0; sx < sWidth; ++sx) + { + //putchar(map[y][x]?'@':' '); + int x = sx - dx; + int y = sy - dy; - const char *Symbol[5] = { "", "", "", "", "" }; + const char *Symbol[5] = { "", "", "", "", "" }; - if (isOutBounds(x, y)) - { - printf(""); - continue; - } - if (x == playerx && y == playery) - { - printf(""); - continue; - } - if (fog[y][x]) - { - printf(""); - continue; - } - if (!walk[y][x]) - { - printf(""); - continue; - } - if (map[y][x] == path) - { - printf(""); - continue; - } - if (map[y][x] == target) - { - printf(""); - continue; - } + if (isOutBounds(x, y)) + { + printf(""); + continue; + } + if (x == playerx && y == playery) + { + printf(""); + continue; + } + if (fog[y][x]) + { + printf(""); + continue; + } + if (!walk[y][x]) + { + printf(""); + continue; + } + if (map[y][x] == path) + { + printf(""); + continue; + } + if (map[y][x] == target) + { + printf(""); + continue; + } - printf(""); - continue; - // ˽ضϣΪַͼʱ + printf(""); + continue; + // ˽ضϣΪַͼʱ - int d = 0; - for (int i = 0; i < 4; ++i) - { - if (!isOutBounds(x + xx[i], y + yy[i]) && map[y + yy[i]][x + xx[i]] == wall) - d |= 1 << i; + // int d = 0; + // for (int i = 0; i < 4; ++i) + // { + // if (!isOutBounds(x + xx[i], y + yy[i]) && map[y + yy[i]][x + xx[i]] == wall) + // d |= 1 << i; - } + // } - const int up = 1; - const int down = 2; - const int left = 4; - const int right = 8; - char ch = 0; - switch (d) - { - case 0: - ch = '&'; - break; - //case up: - //case down: - case up | down: - ch = '|'; - break; - //case left: - //case right: - case left | right: - ch = '-'; - break; - default: - ch = '+'; - } - putchar(ch); - } - cout << endl; - } - //gotoxy(sWidth/2+1, sHeight/2+1); - //putchar('A'); -} - -/* -// ֶʱ -void sleep(int n) -{ -for (int i = 0; i < n*n*n*n; ++i); -} -*/ - -// ˢµͼ -void refresh() -{ - gotoxy(1, 1); - showMap(); + // const int up = 1; + // const int down = 2; + // const int left = 4; + // const int right = 8; + // char ch = 0; + // switch (d) + // { + // case 0: + // ch = '&'; + // break; + // //case up: + // //case down: + // case up | down: + // ch = '|'; + // break; + // //case left: + // //case right: + // case left | right: + // ch = '-'; + // break; + // default: + // ch = '+'; + // } + // putchar(ch); + } + cout << endl; + } + //gotoxy(sWidth/2+1, sHeight/2+1); + //putchar('A'); } // ɵͼ㷨Ϊʼ void generate(int cx, int cy) { - // ȽͼȫΪǽ - setAllWall(); + // ȽͼȫΪǽ + setAllWall(); - // Ѿ߹· - for (int i = 0; i < width*height; ++i) - *((bool*)fog + i) = *((bool*)walk + i) = true; + // Ѿ߹· + for (int i = 0; i < width*height; ++i) + *((bool*)fog + i) = *((bool*)walk + i) = true; - //ҵ - playerx = cx; - playery = cy; + //ҵ + playerx = cx; + playery = cy; - // 㿪ʼܵǽӵб - pushAroundWall(cx, cy); - // · - map[cy][cx] = path; + // 㿪ʼܵǽӵб + pushAroundWall(cx, cy); + // · + map[cy][cx] = path; - // ֻҪдǽһֱѭ - while (!walls.empty()) - { - // Ҹǽ - int index = rand() % walls.size(); - int wx = walls[index].first.first; - int wy = walls[index].first.second; - int d = walls[index].second; - // ǽ򴩣һܵǽڼб - if (map[wy + yy[d]][wx + xx[d]] == wall) - { - map[wy][wx] = path; - map[wy + yy[d]][wx + xx[d]] = path; - pushAroundWall(wx + xx[d], wy + yy[d]); - //sleep(70); - //refresh(); - } - // 򴩺󣬽ǽƳб - walls.erase(walls.begin() + index); - } - - map[height-2][width-2] = target; + // ֻҪдǽһֱѭ + while (!walls.empty()) + { + // Ҹǽ + int index = rand() % walls.size(); + int wx = walls[index].first.first; + int wy = walls[index].first.second; + int d = walls[index].second; + // ǽ򴩣һܵǽڼб + if (map[wy + yy[d]][wx + xx[d]] == wall) + { + map[wy][wx] = path; + map[wy + yy[d]][wx + xx[d]] = path; + pushAroundWall(wx + xx[d], wy + yy[d]); + } + // 򴩺󣬽ǽƳб + walls.erase(walls.begin() + index); + } + + map[height-2][width-2] = target; - // չ - explore(playerx, playery); + // չ + explore(playerx, playery); } // ƶƶķ -void playerMove(int dir) +bool playerMove(Dir dir) { - if (0 > dir || dir >= 4) - return; - int tx = playerx + xx[dir]; - int ty = playery + yy[dir]; - if (isOutBounds(tx, ty)) - return; - if (map[ty][tx] == wall) - return; - if (map[ty][tx] == target) - { - system("cls"); - cout << "Ϸʤ" << endl; - system("pause"); - exit(0); - } - // ֻ·ʱſƶ - if (map[ty][tx] == path) { - playerx = tx, playery = ty; - walk[ty][tx] = false; - } - // ƶٴ̽ - explore(playerx, playery); + if (0 > dir || dir >= 4) + return false; + int tx = playerx + xx[dir]; + int ty = playery + yy[dir]; + if (isOutBounds(tx, ty)) + return false; + if (map[ty][tx] == wall) + return false; + if (map[ty][tx] == target) + { + system("cls"); + cout << "Ϸʤ" << endl; + system("pause"); + return true; + } + // ֻ·ʱſƶ + if (map[ty][tx] == path) { + playerx = tx, playery = ty; + walk[ty][tx] = false; + } + // ƶٴ̽ + explore(playerx, playery); + return false; } // ÿһִ֡и -void updata() +bool updata() { - char ch = getch(); - switch (ch) - { - case 'w': - playerMove(up); - break; - case 's': - playerMove(down); - break; - case 'a': - playerMove(2); - break; - case 'd': - playerMove(3); - break; - } + char ch = getch(); + bool win = false; + switch (ch) + { + case 'w': + win = playerMove(Dir::Up); + break; + case 's': + win = playerMove(Dir::Down); + break; + case 'a': + win = playerMove(Dir::Left); + break; + case 'd': + win = playerMove(Dir::Right); + break; + case 'q': + exit(0); + break; + case 'r': + return false; + break; + } + return !win; } int main() { - // - srand((unsigned int)time(0)); - do { - // ɵͼ - generate(1, 1); - - // Ϸѭ - while (1) - { - // ͼ - showMap(); - // ȴ룬 - updata(); - } - - } while (getch() != 0); - - return 0; + // + srand((unsigned int)time(0)); + + printf("WSADƶQ˳Rؿ"); + system("pause"); + + while (true) { + // ɵͼ + generate(1, 1); + + // Ϸѭ + do { + // ͼ + showMap(); + } while (updata()); + } + + return 0; } \ No newline at end of file diff --git a/play.gif b/play.gif new file mode 100644 index 0000000..caceffe Binary files /dev/null and b/play.gif differ