mirror of
https://github.com/jie65535/maze.git
synced 2024-07-27 19:04:55 +08:00
Update README and fixes
This commit is contained in:
parent
b23a123012
commit
16c763732a
@ -6,3 +6,8 @@ C++ 控制台 迷宫 使用了自动生成迷宫算法,实现迷宫基本玩
|
|||||||
所以该代码只能在windows下编译
|
所以该代码只能在windows下编译
|
||||||
|
|
||||||
程序参数可以在代码头部设置,例如迷宫大小和输出区域大小等
|
程序参数可以在代码头部设置,例如迷宫大小和输出区域大小等
|
||||||
|
|
||||||
|
需要合适的字体才能正常显示
|
||||||
|
|
||||||
|
|
||||||
|

|
131
code.cpp
131
code.cpp
@ -9,10 +9,10 @@
|
|||||||
using namespace std;
|
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 wall = 1;
|
||||||
const char path = 0;
|
const char path = 0;
|
||||||
const char target = 2;
|
const char target = 2;
|
||||||
@ -34,10 +34,7 @@ vector<pair<pair<int, int>, int> > walls;
|
|||||||
——2、如果对面的格子已经是通路了,那就从列表里移除这面墙
|
——2、如果对面的格子已经是通路了,那就从列表里移除这面墙
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const int up = 0;
|
enum Dir { Up, Down, Left, Right };
|
||||||
const int down = 1;
|
|
||||||
const int left = 2;
|
|
||||||
const int right = 3;
|
|
||||||
|
|
||||||
// 四周一步
|
// 四周一步
|
||||||
int xx[] = { 0, 0, -1, 1 };
|
int xx[] = { 0, 0, -1, 1 };
|
||||||
@ -123,7 +120,7 @@ void showMap()
|
|||||||
int dx = sWidth / 2 - playerx;
|
int dx = sWidth / 2 - playerx;
|
||||||
int dy = sHeight / 2 - playery;
|
int dy = sHeight / 2 - playery;
|
||||||
// 移动光标到最开始的位置
|
// 移动光标到最开始的位置
|
||||||
gotoxy(1, 1);
|
gotoxy(0, 0);
|
||||||
|
|
||||||
// 遍历整个地图
|
// 遍历整个地图
|
||||||
for (int sy = 0; sy < sHeight; ++sy)
|
for (int sy = 0; sy < sHeight; ++sy)
|
||||||
@ -171,38 +168,38 @@ void showMap()
|
|||||||
continue;
|
continue;
|
||||||
// 到此截断,后面为单字符地图时用
|
// 到此截断,后面为单字符地图时用
|
||||||
|
|
||||||
int d = 0;
|
// int d = 0;
|
||||||
for (int i = 0; i < 4; ++i)
|
// for (int i = 0; i < 4; ++i)
|
||||||
{
|
// {
|
||||||
if (!isOutBounds(x + xx[i], y + yy[i]) && map[y + yy[i]][x + xx[i]] == wall)
|
// if (!isOutBounds(x + xx[i], y + yy[i]) && map[y + yy[i]][x + xx[i]] == wall)
|
||||||
d |= 1 << i;
|
// d |= 1 << i;
|
||||||
|
|
||||||
}
|
// }
|
||||||
|
|
||||||
const int up = 1;
|
// const int up = 1;
|
||||||
const int down = 2;
|
// const int down = 2;
|
||||||
const int left = 4;
|
// const int left = 4;
|
||||||
const int right = 8;
|
// const int right = 8;
|
||||||
char ch = 0;
|
// char ch = 0;
|
||||||
switch (d)
|
// switch (d)
|
||||||
{
|
// {
|
||||||
case 0:
|
// case 0:
|
||||||
ch = '&';
|
// ch = '&';
|
||||||
break;
|
// break;
|
||||||
//case up:
|
// //case up:
|
||||||
//case down:
|
// //case down:
|
||||||
case up | down:
|
// case up | down:
|
||||||
ch = '|';
|
// ch = '|';
|
||||||
break;
|
// break;
|
||||||
//case left:
|
// //case left:
|
||||||
//case right:
|
// //case right:
|
||||||
case left | right:
|
// case left | right:
|
||||||
ch = '-';
|
// ch = '-';
|
||||||
break;
|
// break;
|
||||||
default:
|
// default:
|
||||||
ch = '+';
|
// ch = '+';
|
||||||
}
|
// }
|
||||||
putchar(ch);
|
// putchar(ch);
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
@ -210,21 +207,6 @@ void showMap()
|
|||||||
//putchar('A');
|
//putchar('A');
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// 手动延时函数
|
|
||||||
void sleep(int n)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < n*n*n*n; ++i);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 刷新地图
|
|
||||||
void refresh()
|
|
||||||
{
|
|
||||||
gotoxy(1, 1);
|
|
||||||
showMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成地图算法,参数为起始点坐标
|
// 生成地图算法,参数为起始点坐标
|
||||||
void generate(int cx, int cy)
|
void generate(int cx, int cy)
|
||||||
{
|
{
|
||||||
@ -258,8 +240,6 @@ void generate(int cx, int cy)
|
|||||||
map[wy][wx] = path;
|
map[wy][wx] = path;
|
||||||
map[wy + yy[d]][wx + xx[d]] = path;
|
map[wy + yy[d]][wx + xx[d]] = path;
|
||||||
pushAroundWall(wx + xx[d], wy + yy[d]);
|
pushAroundWall(wx + xx[d], wy + yy[d]);
|
||||||
//sleep(70);
|
|
||||||
//refresh();
|
|
||||||
}
|
}
|
||||||
// 打穿后,将该墙移除待凿列表
|
// 打穿后,将该墙移除待凿列表
|
||||||
walls.erase(walls.begin() + index);
|
walls.erase(walls.begin() + index);
|
||||||
@ -273,22 +253,22 @@ void generate(int cx, int cy)
|
|||||||
|
|
||||||
|
|
||||||
// 玩家移动,参数是移动的方向
|
// 玩家移动,参数是移动的方向
|
||||||
void playerMove(int dir)
|
bool playerMove(Dir dir)
|
||||||
{
|
{
|
||||||
if (0 > dir || dir >= 4)
|
if (0 > dir || dir >= 4)
|
||||||
return;
|
return false;
|
||||||
int tx = playerx + xx[dir];
|
int tx = playerx + xx[dir];
|
||||||
int ty = playery + yy[dir];
|
int ty = playery + yy[dir];
|
||||||
if (isOutBounds(tx, ty))
|
if (isOutBounds(tx, ty))
|
||||||
return;
|
return false;
|
||||||
if (map[ty][tx] == wall)
|
if (map[ty][tx] == wall)
|
||||||
return;
|
return false;
|
||||||
if (map[ty][tx] == target)
|
if (map[ty][tx] == target)
|
||||||
{
|
{
|
||||||
system("cls");
|
system("cls");
|
||||||
cout << "游戏胜利!" << endl;
|
cout << "游戏胜利!" << endl;
|
||||||
system("pause");
|
system("pause");
|
||||||
exit(0);
|
return true;
|
||||||
}
|
}
|
||||||
// 只有是路的时候才可以移动
|
// 只有是路的时候才可以移动
|
||||||
if (map[ty][tx] == path) {
|
if (map[ty][tx] == path) {
|
||||||
@ -297,47 +277,56 @@ void playerMove(int dir)
|
|||||||
}
|
}
|
||||||
// 移动完后再次探索迷雾
|
// 移动完后再次探索迷雾
|
||||||
explore(playerx, playery);
|
explore(playerx, playery);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每一帧执行更新
|
// 每一帧执行更新
|
||||||
void updata()
|
bool updata()
|
||||||
{
|
{
|
||||||
char ch = getch();
|
char ch = getch();
|
||||||
|
bool win = false;
|
||||||
switch (ch)
|
switch (ch)
|
||||||
{
|
{
|
||||||
case 'w':
|
case 'w':
|
||||||
playerMove(up);
|
win = playerMove(Dir::Up);
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
playerMove(down);
|
win = playerMove(Dir::Down);
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
playerMove(2);
|
win = playerMove(Dir::Left);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
playerMove(3);
|
win = playerMove(Dir::Right);
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
exit(0);
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
return false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return !win;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// 设置种子
|
// 设置种子
|
||||||
srand((unsigned int)time(0));
|
srand((unsigned int)time(0));
|
||||||
do {
|
|
||||||
|
printf("WSADÒÆ¶¯£¬QÍ˳ö£¬RÖØ¿ª");
|
||||||
|
system("pause");
|
||||||
|
|
||||||
|
while (true) {
|
||||||
// 生成地图
|
// 生成地图
|
||||||
generate(1, 1);
|
generate(1, 1);
|
||||||
|
|
||||||
// 游戏循环
|
// 游戏循环
|
||||||
while (1)
|
do {
|
||||||
{
|
|
||||||
// 输出地图
|
// 输出地图
|
||||||
showMap();
|
showMap();
|
||||||
// 等待输入,更新
|
} while (updata());
|
||||||
updata();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (getch() != 0);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user