Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Recursive backtracking maze error  (Read 1509 times)

0 Members and 1 Guest are viewing this topic.

epilepticPi

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Recursive backtracking maze error
« on: January 28, 2017, 12:24:53 pm »
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.
« Last Edit: January 28, 2017, 12:41:55 pm by epilepticPi »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10988
    • View Profile
    • development blog
    • Email
Re: Recursive backtracking maze error
« Reply #1 on: January 28, 2017, 01:22:31 pm »
Don't misuse exceptions for program logic. Do your own out of range checks!

I don't really want to invest the time understanding your logic, but you should keep in mind that indices for a vector are std::size_t which is often implemented as unsigned int. So when you pass a negative index, it will be implicitly converted (you might get a compiler warning) and will underflow, thus you end up with a huge number, which in turn might lead to a wrapping.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

epilepticPi

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Recursive backtracking maze error
« Reply #2 on: January 28, 2017, 03:09:10 pm »
Thanks! Got it working using my own range checks!