Dear you,
Ive implemented the recursive backtracker maze generation algorithm in sfml. The problem is that the current cell leaves the grid on the left or the right and pops up on the other side. But I dont want that.
Heres my getNeighbours function:
Cell* Cell::checkNB(std::vector <Cell> &grid, int cols)
{
std::vector <Cell*> nbs;
Cell* top;
Cell* right;
Cell* bottom;
Cell* left;
try {
int i = indice(xPos, yPos - 1, cols);
top = &grid.at(i);
}catch(std::out_of_range)
{
top = nullptr;
}
try {
int i = indice(xPos + 1, yPos, cols);
right = &grid.at(i);
}catch(std::out_of_range)
{
right = nullptr;
}
try {
int i = indice(xPos, yPos + 1, cols);
bottom = &grid.at(i);
}catch(std::out_of_range)
{
bottom = nullptr;
}
try {
int i = indice(xPos - 1, yPos, cols);
left = &grid.at(i);
}catch(std::out_of_range)
{
left = nullptr;
}
if(top != nullptr && !top->visited)
{
top->chosenDir = "top";
nbs.push_back(top);
}
if (right != nullptr && !right->visited)
{
right->chosenDir = "right";
nbs.push_back(right);
}
if (bottom != nullptr && !bottom->visited)
{
bottom->chosenDir = "bottom";
nbs.push_back(bottom);
}
if (left != nullptr && !left->visited)
{
left->chosenDir = "left";
nbs.push_back(left);
}
if(nbs.size() > 0)
{
return nbs[rand() % nbs.size()];
}else
{
return nullptr;
}
}
the indice function:
int indice(int i, int j, int cols)
{
return i + j *cols;
}
Its only leaving on the right and left, top and bottom are fine.
Thanks for any help!
Yours, me.