当前位置:网站首页>Drawing maze EasyX library with recursive backtracking method
Drawing maze EasyX library with recursive backtracking method
2022-07-03 08:37:00 【Zoe_ Cpp】
#include <stdio.h>
#include <easyx.h>
#include <time.h>
int direction = 0;//0 Indicating right , 1 It means down ,2 Means up ,3 Indicating left
int n = 64 * 64 - 1;
struct Space
{
int x;
int y;
Space* bef = NULL;
bool att;//0 Indicates no access to ,1 Indicates that you have visited
};
Space space[64][64];
int x = 1;
int y = 1;
int z = 1;
bool ifHaveWay()
{
if (space[x - 1][y].att && space[x + 1][y].att && space[x][y - 1].att && space[x][y + 1].att)
return 0;
else
return 1;
}
void RandDirection()
{
while (1)// Get directions
{
direction = rand() % 4;
if (direction == 3 && space[x - 1][y].att)
continue;
if (direction == 0 && space[x + 1][y].att)
continue;
if (direction == 2 && space[x][y - 1].att)
continue;
if (direction == 1 && space[x][y + 1].att)
continue;
break;
}
}
void WallBreak()
{
setlinecolor(BLACK);
if (direction == 1)
line(x * 10 + 1, y * 10, x * 10 + 9, y * 10);
if (direction == 0)
line(x * 10, y * 10 + 1, x * 10, y * 10 + 9);
if (direction == 3)
line(x * 10 + 10, y * 10 + 1, x * 10 + 10, y * 10 + 9);
if (direction == 2)
line(x * 10 + 1, y * 10 + 10, x * 10 + 9, y * 10 + 10);
}
void FoundWay()
{
int tx, ty;
Space* p1 = NULL;
while (x != 1 || y != 1 || z)
{
while (ifHaveWay())// Draw the main line of maze
{
p1 = &space[x][y];
space[x][y].att = 1;
RandDirection();
if (direction == 0)
x++;
if (direction == 1)
y++;
if (direction == 2)
y--;
if (direction == 3)
x--;
WallBreak();
space[x][y].bef = p1;
//Sleep(5);
}
space[x][y].att = 1;
tx = x;
ty = y;
x = space[tx][ty].bef->x;
y = space[tx][ty].bef->y;
z = 0;
}
}
// initialization
void SetGraph()
{
for (int i = 0; i < 64; i++)
{
for (int j = 0; j < 64; j++)
{
space[i][j].x = i;
space[i][j].y = j;
}
}
// Clear the elements in the maze
for (int i = 0; i < 64; i++)
{
for (int j = 0; j < 64; j++)
{
space[i][j].att = 0;
}
}
// Build walls around the maze ( Set to visited )
for (int i = 0; i < 64; i++)
{
space[0][i].att = 1;
space[63][i].att = 1;
space[i][0].att = 1;
space[i][63].att = 1;
}
initgraph(640, 640);
}
void DrawWall()
{
for (int i = 0; i < 640; i += 10)
{
line(i, 0, i, 640);
}
for (int i = 0; i < 640; i += 10)
{
line(0, i, 640, i);
}
solidrectangle(0, 0, 640, 10);
solidrectangle(0, 0, 10, 640);
solidrectangle(630, 0, 640, 640);
solidrectangle(0, 630, 640, 640);
}
int main()
{
srand(time(NULL));
SetGraph();
DrawWall();
FoundWay();
setlinecolor(WHITE);
system("pause");
return 0;
}
Practice effect
边栏推荐
- [audio and video] ijkplayer error code
- Vscode, idea, VIM development tool shortcut keys
- 简易入手《SOM神经网络》的本质与原理
- Development material set
- UE4 plug in development
- Downward compatibility and upward compatibility
- Monotonic stack -503 Next bigger Element II
- Unity Editor Extension - Outline
- 单调栈-503. 下一个更大元素 II
- GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
猜你喜欢
简易入手《SOM神经网络》的本质与原理
Graphics_ Games101/202 learning notes
了解小程序的笔记 2022/7/3
UE4 source code reading_ Bone model and animation system_ Animation process
Vscode, idea, VIM development tool shortcut keys
[redis] redis persistent RDB vs AOF (source code)
Image processing 8-cnn image classification
Dealing with duplicate data in Excel with xlwings
Constraintlayout's constraintset dynamically modifies constraints
Unity editor expansion - controls, layouts
随机推荐
[K & R] Chinese Second Edition personal questions Chapter1
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
UE4 source code reading_ Bone model and animation system_ Animation compression
[rust notes] 09- special types and generics
How does unity fixedupdate call at a fixed frame rate
Base64和Base64URL
Servlet的生命周期
Why can void * be a general pointer
UE4 source code reading_ Bone model and animation system_ Animation process
Graphics_ Learnopongl learning notes
【Rust 笔记】10-操作符重载
Use of ue5 QRcode plug-in
Osgconv tool usage
Pit & ADB wireless debugging of vivo real machine debugging
分配异常的servlet
UE4 plug in development
Some understandings of 3dfiles
Unity multi open script
matlab神经网络所有传递函数(激活函数)公式详解
[cloud native] introduction and use of feign of microservices