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:
@@ -6,3 +6,8 @@ C++ 控制台 迷宫 使用了自动生成迷宫算法,实现迷宫基本玩
|
|||||||
所以该代码只能在windows下编译
|
所以该代码只能在windows下编译
|
||||||
|
|
||||||
程序参数可以在代码头部设置,例如迷宫大小和输出区域大小等
|
程序参数可以在代码头部设置,例如迷宫大小和输出区域大小等
|
||||||
|
|
||||||
|
需要合适的字体才能正常显示
|
||||||
|
|
||||||
|
|
||||||
|

|
459
code.cpp
459
code.cpp
@@ -9,10 +9,10 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
|
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
|
||||||
const size_t sWidth = 30, sHeight = 24;
|
const size_t sWidth = 60, sHeight = 24;
|
||||||
|
|
||||||
// <20>Թ<EFBFBD><D4B9><EFBFBD>ʵ<EFBFBD>ʴ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// <20>Թ<EFBFBD><D4B9><EFBFBD>ʵ<EFBFBD>ʴ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
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;
|
|||||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD>ͨ·<EFBFBD>ˣ<EFBFBD><EFBFBD>Ǿʹ<EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD>ͨ·<EFBFBD>ˣ<EFBFBD><EFBFBD>Ǿʹ<EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const int up = 0;
|
enum Dir { Up, Down, Left, Right };
|
||||||
const int down = 1;
|
|
||||||
const int left = 2;
|
|
||||||
const int right = 3;
|
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
||||||
int xx[] = { 0, 0, -1, 1 };
|
int xx[] = { 0, 0, -1, 1 };
|
||||||
@@ -56,288 +53,280 @@ const int margin = 3;
|
|||||||
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>굽xyλ<79><CEBB>
|
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>굽xyλ<79><CEBB>
|
||||||
void gotoxy(int x, int y)
|
void gotoxy(int x, int y)
|
||||||
{
|
{
|
||||||
COORD coord = { (short)x, (short)y };
|
COORD coord = { (short)x, (short)y };
|
||||||
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
|
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
|
||||||
}
|
}
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>ͼΪǽ
|
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>ͼΪǽ
|
||||||
void setAllWall()
|
void setAllWall()
|
||||||
{
|
{
|
||||||
for (int y = 0; y < height; ++y)
|
for (int y = 0; y < height; ++y)
|
||||||
for (int x = 0; x < width; ++x)
|
for (int x = 0; x < width; ++x)
|
||||||
map[y][x] = wall;
|
map[y][x] = wall;
|
||||||
}
|
}
|
||||||
|
|
||||||
// <20>Ƿ<EFBFBD>Խ<EFBFBD><D4BD>
|
// <20>Ƿ<EFBFBD>Խ<EFBFBD><D4BD>
|
||||||
bool isOutBounds(int x, int y)
|
bool isOutBounds(int x, int y)
|
||||||
{
|
{
|
||||||
return (x<0 || x >= width
|
return (x<0 || x >= width
|
||||||
|| y<0 || y >= height);
|
|| y<0 || y >= height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̽<EFBFBD><CCBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̽<EFBFBD><CCBD>
|
||||||
void explore(int x, int y)
|
void explore(int x, int y)
|
||||||
{
|
{
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i)
|
||||||
{
|
{
|
||||||
// ˳<><CBB3>һ<EFBFBD><D2BB>·<EFBFBD><C2B7>
|
// ˳<><CBB3>һ<EFBFBD><D2BB>·<EFBFBD><C2B7>
|
||||||
for (int x1 = x, y1 = y;
|
for (int x1 = x, y1 = y;
|
||||||
!isOutBounds(x1, y1)
|
!isOutBounds(x1, y1)
|
||||||
&& map[y1][x1] == path;
|
&& map[y1][x1] == path;
|
||||||
x1 += xx[i], y1 += yy[i])
|
x1 += xx[i], y1 += yy[i])
|
||||||
{
|
{
|
||||||
// ÿ<><C3BF>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊͨ·
|
// ÿ<><C3BF>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊͨ·
|
||||||
for (int fy = y1 - 1; fy <= y1 + 1; ++fy)
|
for (int fy = y1 - 1; fy <= y1 + 1; ++fy)
|
||||||
for (int fx = x1 - 1; fx <= x1 + 1; ++fx)
|
for (int fx = x1 - 1; fx <= x1 + 1; ++fx)
|
||||||
if (!isOutBounds(fx, fy)
|
if (!isOutBounds(fx, fy)
|
||||||
&& fog[fy][fx])
|
&& fog[fy][fx])
|
||||||
fog[fy][fx] = false;
|
fog[fy][fx] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD>괦<EFBFBD><EAB4A6>ǽ<EFBFBD><C7BD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>ͨǽ<CDA8><C7BD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD>괦<EFBFBD><EAB4A6>ǽ<EFBFBD><C7BD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>ͨǽ<CDA8><C7BD>
|
||||||
void pushAroundWall(int x, int y)
|
void pushAroundWall(int x, int y)
|
||||||
{
|
{
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i)
|
||||||
{
|
{
|
||||||
int tx = x + xx[i];
|
int tx = x + xx[i];
|
||||||
int ty = y + yy[i];
|
int ty = y + yy[i];
|
||||||
if (isOutBounds(tx, ty))
|
if (isOutBounds(tx, ty))
|
||||||
continue;
|
continue;
|
||||||
int wx = x + x2[i];
|
int wx = x + x2[i];
|
||||||
int wy = y + y2[i];
|
int wy = y + y2[i];
|
||||||
if (isOutBounds(wx, wy))
|
if (isOutBounds(wx, wy))
|
||||||
continue;
|
continue;
|
||||||
// ֻҪ<D6BB><D2AA>ǽ<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>б<EFBFBD>
|
// ֻҪ<D6BB><D2AA>ǽ<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>б<EFBFBD>
|
||||||
if (map[ty][tx] == wall)
|
if (map[ty][tx] == wall)
|
||||||
walls.push_back(pair<pair<int, int>, int>(pair<int, int>( tx,ty ),i ));
|
walls.push_back(pair<pair<int, int>, int>(pair<int, int>( tx,ty ),i ));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// չʾ<D5B9><CABE>ͼ
|
// չʾ<D5B9><CABE>ͼ
|
||||||
void showMap()
|
void showMap()
|
||||||
{
|
{
|
||||||
int dx = sWidth / 2 - playerx;
|
int dx = sWidth / 2 - playerx;
|
||||||
int dy = sHeight / 2 - playery;
|
int dy = sHeight / 2 - playery;
|
||||||
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>굽<EFBFBD>ʼ<EEBFAA><CABC>λ<EFBFBD><CEBB>
|
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>굽<EFBFBD>ʼ<EEBFAA><CABC>λ<EFBFBD><CEBB>
|
||||||
gotoxy(1, 1);
|
gotoxy(0, 0);
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
|
||||||
for (int sy = 0; sy < sHeight; ++sy)
|
for (int sy = 0; sy < sHeight; ++sy)
|
||||||
{
|
{
|
||||||
for (int sx = 0; sx < sWidth; ++sx)
|
for (int sx = 0; sx < sWidth; ++sx)
|
||||||
{
|
{
|
||||||
//putchar(map[y][x]?'@':' ');
|
//putchar(map[y][x]?'@':' ');
|
||||||
int x = sx - dx;
|
int x = sx - dx;
|
||||||
int y = sy - dy;
|
int y = sy - dy;
|
||||||
|
|
||||||
const char *Symbol[5] = { "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>" };
|
const char *Symbol[5] = { "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD>" };
|
||||||
|
|
||||||
if (isOutBounds(x, y))
|
if (isOutBounds(x, y))
|
||||||
{
|
{
|
||||||
printf("<EFBFBD><EFBFBD>");
|
printf("<EFBFBD><EFBFBD>");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (x == playerx && y == playery)
|
if (x == playerx && y == playery)
|
||||||
{
|
{
|
||||||
printf("<EFBFBD><EFBFBD>");
|
printf("<EFBFBD><EFBFBD>");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (fog[y][x])
|
if (fog[y][x])
|
||||||
{
|
{
|
||||||
printf("<EFBFBD><EFBFBD>");
|
printf("<EFBFBD><EFBFBD>");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!walk[y][x])
|
if (!walk[y][x])
|
||||||
{
|
{
|
||||||
printf("<EFBFBD><EFBFBD>");
|
printf("<EFBFBD><EFBFBD>");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (map[y][x] == path)
|
if (map[y][x] == path)
|
||||||
{
|
{
|
||||||
printf("<EFBFBD><EFBFBD>");
|
printf("<EFBFBD><EFBFBD>");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (map[y][x] == target)
|
if (map[y][x] == target)
|
||||||
{
|
{
|
||||||
printf("<EFBFBD><EFBFBD>");
|
printf("<EFBFBD><EFBFBD>");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("<EFBFBD><EFBFBD>");
|
printf("<EFBFBD><EFBFBD>");
|
||||||
continue;
|
continue;
|
||||||
// <20><><EFBFBD>˽ضϣ<D8B6><CFA3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ͼʱ<CDBC><CAB1>
|
// <20><><EFBFBD>˽ضϣ<D8B6><CFA3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ͼʱ<CDBC><CAB1>
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
//gotoxy(sWidth/2+1, sHeight/2+1);
|
//gotoxy(sWidth/2+1, sHeight/2+1);
|
||||||
//putchar('A');
|
//putchar('A');
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
// <20>ֶ<EFBFBD><D6B6><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|
||||||
void sleep(int n)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < n*n*n*n; ++i);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// ˢ<>µ<EFBFBD>ͼ
|
|
||||||
void refresh()
|
|
||||||
{
|
|
||||||
gotoxy(1, 1);
|
|
||||||
showMap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// <20><><EFBFBD>ɵ<EFBFBD>ͼ<EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// <20><><EFBFBD>ɵ<EFBFBD>ͼ<EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
void generate(int cx, int cy)
|
void generate(int cx, int cy)
|
||||||
{
|
{
|
||||||
// <20><><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD>ͼȫ<CDBC><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊǽ
|
// <20><><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD>ͼȫ<CDBC><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊǽ
|
||||||
setAllWall();
|
setAllWall();
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE>߹<EFBFBD><DFB9><EFBFBD>·<EFBFBD><C2B7>
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE>߹<EFBFBD><DFB9><EFBFBD>·<EFBFBD><C2B7>
|
||||||
for (int i = 0; i < width*height; ++i)
|
for (int i = 0; i < width*height; ++i)
|
||||||
*((bool*)fog + i) = *((bool*)walk + i) = true;
|
*((bool*)fog + i) = *((bool*)walk + i) = true;
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>
|
||||||
playerx = cx;
|
playerx = cx;
|
||||||
playery = cy;
|
playery = cy;
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD>㿪ʼ<E3BFAA><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD>㿪ʼ<E3BFAA><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>
|
||||||
pushAroundWall(cx, cy);
|
pushAroundWall(cx, cy);
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·
|
||||||
map[cy][cx] = path;
|
map[cy][cx] = path;
|
||||||
|
|
||||||
// ֻҪ<D6BB><D2AA><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD><EFBFBD>һֱѭ<D6B1><D1AD>
|
// ֻҪ<D6BB><D2AA><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD><EFBFBD>һֱѭ<D6B1><D1AD>
|
||||||
while (!walls.empty())
|
while (!walls.empty())
|
||||||
{
|
{
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD>Ҹ<EFBFBD><D2B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ҹ<EFBFBD><D2B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ
|
||||||
int index = rand() % walls.size();
|
int index = rand() % walls.size();
|
||||||
int wx = walls[index].first.first;
|
int wx = walls[index].first.first;
|
||||||
int wy = walls[index].first.second;
|
int wy = walls[index].first.second;
|
||||||
int d = walls[index].second;
|
int d = walls[index].second;
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD><EFBFBD><F2B4A9A3><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD>ǽ<EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD><EFBFBD><F2B4A9A3><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD>ǽ<EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
||||||
if (map[wy + yy[d]][wx + xx[d]] == wall)
|
if (map[wy + yy[d]][wx + xx[d]] == wall)
|
||||||
{
|
{
|
||||||
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();
|
// <20><F2B4A9BA><F3A3ACBD><EFBFBD>ǽ<EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
||||||
}
|
walls.erase(walls.begin() + index);
|
||||||
// <20><F2B4A9BA><F3A3ACBD><EFBFBD>ǽ<EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
}
|
||||||
walls.erase(walls.begin() + index);
|
|
||||||
}
|
map[height-2][width-2] = target;
|
||||||
|
|
||||||
map[height-2][width-2] = target;
|
|
||||||
|
|
||||||
// չ<><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// չ<><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
explore(playerx, playery);
|
explore(playerx, playery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ķ<EFBFBD><C4B7><EFBFBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ķ<EFBFBD><C4B7><EFBFBD>
|
||||||
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 << "<EFBFBD><EFBFBD>Ϸʤ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
cout << "<EFBFBD><EFBFBD>Ϸʤ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
system("pause");
|
system("pause");
|
||||||
exit(0);
|
return true;
|
||||||
}
|
}
|
||||||
// ֻ<><D6BB><EFBFBD><EFBFBD>·<EFBFBD><C2B7>ʱ<EFBFBD><CAB1><EFBFBD>ſ<EFBFBD><C5BF><EFBFBD><EFBFBD>ƶ<EFBFBD>
|
// ֻ<><D6BB><EFBFBD><EFBFBD>·<EFBFBD><C2B7>ʱ<EFBFBD><CAB1><EFBFBD>ſ<EFBFBD><C5BF><EFBFBD><EFBFBD>ƶ<EFBFBD>
|
||||||
if (map[ty][tx] == path) {
|
if (map[ty][tx] == path) {
|
||||||
playerx = tx, playery = ty;
|
playerx = tx, playery = ty;
|
||||||
walk[ty][tx] = false;
|
walk[ty][tx] = false;
|
||||||
}
|
}
|
||||||
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD>̽<EFBFBD><CCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD>̽<EFBFBD><CCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
explore(playerx, playery);
|
explore(playerx, playery);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ÿһִ֡<D6A1>и<EFBFBD><D0B8><EFBFBD>
|
// ÿһִ֡<D6A1>и<EFBFBD><D0B8><EFBFBD>
|
||||||
void updata()
|
bool updata()
|
||||||
{
|
{
|
||||||
char ch = getch();
|
char ch = getch();
|
||||||
switch (ch)
|
bool win = false;
|
||||||
{
|
switch (ch)
|
||||||
case 'w':
|
{
|
||||||
playerMove(up);
|
case 'w':
|
||||||
break;
|
win = playerMove(Dir::Up);
|
||||||
case 's':
|
break;
|
||||||
playerMove(down);
|
case 's':
|
||||||
break;
|
win = playerMove(Dir::Down);
|
||||||
case 'a':
|
break;
|
||||||
playerMove(2);
|
case 'a':
|
||||||
break;
|
win = playerMove(Dir::Left);
|
||||||
case 'd':
|
break;
|
||||||
playerMove(3);
|
case 'd':
|
||||||
break;
|
win = playerMove(Dir::Right);
|
||||||
}
|
break;
|
||||||
|
case 'q':
|
||||||
|
exit(0);
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return !win;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
srand((unsigned int)time(0));
|
srand((unsigned int)time(0));
|
||||||
do {
|
|
||||||
// <20><><EFBFBD>ɵ<EFBFBD>ͼ
|
printf("WSAD<EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD>Q<EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD>R<EFBFBD>ؿ<EFBFBD>");
|
||||||
generate(1, 1);
|
system("pause");
|
||||||
|
|
||||||
// <20><>Ϸѭ<CFB7><D1AD>
|
while (true) {
|
||||||
while (1)
|
// <20><><EFBFBD>ɵ<EFBFBD>ͼ
|
||||||
{
|
generate(1, 1);
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
|
|
||||||
showMap();
|
// <20><>Ϸѭ<CFB7><D1AD>
|
||||||
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD>
|
do {
|
||||||
updata();
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
|
||||||
}
|
showMap();
|
||||||
|
} while (updata());
|
||||||
} while (getch() != 0);
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Reference in New Issue
Block a user