随机障碍物的迷宫,超级好玩
你将扮演一位名为「旅行者」($)的神秘角色,在自由的旅行中邂逅性格各异、能力独特的同伴们,和他们一起击败强敌,找回失散的亲人(☆)

#include<iostream>
#include<windows.h>//CursorInfo
#include<conio.h>
#include<ctime>
using namespace std;
const int N = 15;
int m[N][N], step, vis[N][N], startx, starty, endx, endy, mark;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void HideCursor()
{
CONSOLE_CURSOR_INFO CursorInfo; // 定义光标信息的结构体变量
CursorInfo.dwSize = 1; // 赋值光标尺寸才能隐藏光标
CursorInfo.bVisible = FALSE; // 将光标设置为不可见
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // 获取控制台标准输出设备的句柄
SetConsoleCursorInfo(handle, &CursorInfo); // 设置控制台光标信息
}
void CursorJump(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle, pos);
}
void welcome()
{
for (int i = 0; i < 10; i++)
{
cout << endl;
}
cout << R"(
■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
■ ■
■ ■
■ ■
■ 在规定时间内走出迷宫 ■
■ 否则游戏失败 ■
■ ■
■ ■
■ ■
■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ )";
cout << "\n ";
getch();
system("cls");//清除控制台屏幕
for (int i = 0; i < 15; i++)
{
cout << endl;
}
cout << " 用a, w, s, d(英文)控制方向";
getch();
system("cls");
}
void Init()
{
srand(time(0));
for (int j = 0; j < N; j++)
{
m[0][j] = 1;
m[N - 1][j] = 1;
}
for (int j = 0; j < N; j++)
{
m[j][0] = 1;
m[j][N - 1] = 1;
}
for (int j = 0; j < (N - 5) * (N - 5); j++)
m[rand() % (N - 2) + 1][rand() % (N - 2) + 1] = 1;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (m[i][j] == 1) cout << "■ ";
else cout << " ";
}
cout << endl;
}
}
void dfs(int x, int y, int len)
{
if (vis[x][y] == 1)return;
else vis[x][y] = 1;
if (len > step)
{
endx = x, endy = y;
step = len;
}
for (int i = 0; i < 4; i++)
{
if (m[x + dx[i]][y + dy[i]] == 1)continue;
else dfs(x + dx[i], y + dy[i], len + 1);
}
}
void Pos() {
int flag = 0;
for (int i = 1; i < N - 1; i++)
{
for (int j = 1; j < N - 1; j++)
if (m[i][j] == 0)
{
for (int k = 0; k < 4; i++)
{
if (m[i + dx[k]][j + dy[k]] == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
startx = i;
starty = j;
break;
}
}
if (flag == 1)break;
}
if (flag == 0)
{
Init();
Pos();
return;
}
else
{
dfs(startx, starty, 0);
CursorJump(2 * endy, endx);
cout << "☆ ";//空格保证不会被“\b ”覆盖
}
}
void Play()
{
char c;
CursorJump(starty * 2, startx);
cout << "$";
while (1)
{
int x, y, flag = 0;
c = getch();
switch (c)
{
case 'w':
x = startx - 1, y = starty;
break;
case 's':
x = startx + 1, y = starty;
break;
case 'a':
x = startx, y = starty - 1;
break;
case 'd':
x = startx, y = starty + 1;
break;
default:
flag = 1;
break;
}
if (flag == 1 || m[x][y] == 1)
continue;
else {
cout << "\b ";//+++space
CursorJump(2 * y, x);
startx = x, starty = y;
cout << "$";
if (x == endx && y == endy)
{
CursorJump(2 * N + 1, N + 1);
cout << "You have winned 14.514% mankind.";
}
}
}
}
int main()
{
HideCursor();
welcome();
Init();
Pos();
Play();
}
3981





