I am... so very confused.
void CreateMaze(){
//Creates the maze
sf::Vector2f start = StartingPoint();
//Sets information for each node "block"
for (int x = 0; x < WIDTH; x += BLOCK_SIZE){
for (int y = 0; y < HEIGHT; y += BLOCK_SIZE){
block new_block;
///block.shape info
sf::RectangleShape rec;
rec.setSize(sf::Vector2f(BLOCK_SIZE, BLOCK_SIZE));
rec.setPosition(x, y);
//Creates basic grid
if (PartOfCircle(x, y)){
rec.setFillColor(black);
}else{
rec.setFillColor(white);
}
if (PartOfCenter(x, y)){
rec.setFillColor(red);
}
//Starting point
if (rec.getPosition() == start){
rec.setFillColor(green);
greenPos.x = x/BLOCK_SIZE;
greenPos.y = y/BLOCK_SIZE;
}
new_block.shape = rec;
///block costs and parent position info
new_block.fCost = FLT_MAX;
new_block.gCost = FLT_MAX;
new_block.hCost = FLT_MAX;
new_block.pos = sf::Vector2i(x/BLOCK_SIZE, y/BLOCK_SIZE);
new_block.parentPos = sf::Vector2i(-1, -1);
closedList[x][y] = false;
maze[x/BLOCK_SIZE][y/BLOCK_SIZE] = new_block;
}
}
I am implementing a maze solver with the A* algorithm. This is part of a function that creates a procedurally generated maze(not very sophisticated, but I do my best). The code works exactly as it should.
Some time later while implementing the A* algorithm, I stumbled into a problem.
std::vector<block> ASharp(){
//Uses A* algorithm to find the fastest solution through the maze
std::vector<block> openList;
std::cout << openList.size();
sf::Vector2i curPos = greenPos;
maze[curPos.x][curPos.y].fCost = 0.0;
maze[curPos.x][curPos.y].gCost = 0.0;
maze[curPos.x][curPos.y].hCost = 0.0;
maze[curPos.x][curPos.y].parentPos = curPos;
openList.emplace_back(maze[curPos.x][curPos.y]);
while (!openList.empty() && openList.size() < (WIDTH/BLOCK_SIZE) * (HEIGHT/BLOCK_SIZE)){
block route;
do{
float tmp = FLT_MAX;
std::vector<block>::iterator itBlock; //Used to erase the correct pos
//Find the block in openList that has the least f
for (std::vector<block>::iterator it = openList.begin();
it != openList.end(); it = next(it)) {
std::cout << openList.size();
block n = *it;
if (n.fCost < tmp) {
tmp = n.fCost;
itBlock = it;
}
}
route = *itBlock;
openList.erase(itBlock);
}while(!SquareIsValid(route.shape.getFillColor()));
That is part of my implementation.
Trying to run my program causes a crash at route = *itBlock
. I really have no idea if this is related to the problem I'm asking about here, but it might be relevant so I thought I'd add it.
You see, the problem is that while trying to find what was causing the crash in ASharp(), I was using std::cout to output some info. What's weird is that is will print strings, but not integers AFTER CreateMaze() is called in main{}. Before CreateMaze is called, it will print fine.
I went through CreateMaze and started putting in std::cout << 10;
in various places to try and track down where is begins to fail. It would seem, that is stops printing halfway through the x loop.
closedList[x][y] = false;
maze[x/BLOCK_SIZE][y/BLOCK_SIZE] = new_block;
}
std::cout << 10;
}
that prints only a few 10's before just stopping...
I really don't understand why or even how such a thing would occur. If anyone has any idea, please let me know. Thanks in advance.
In C++, we can explicitly flushed to forced the buffer to be written. Generally std::endl function works the same by inserting new-line character and flushes the stream. stdout/cout is line-buffered that is the output doesn't get sent to the OS until you write a newline or explicitly flush the buffer. For instance,
// Causes only one write to underlying file
// instead of 5, which is much better for
// performance.
std::cout << a << " + " << b << " = " << std::endl;
another way is to use std::flush which like the name suggest can flush the buffer. For example:
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " " << flush;
this_thread::sleep_for(chrono::seconds(1));
}
return 0;
}
will print each number one after each other instead of once. So i would suggest fixing your std::cout and adding std::endl to flush the buffer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.