-3

I am trying to read a maze (from input.txt) and solve it. This code doesn't work as it should. What is wrong?

void Maze::create(vector<vector<int> >&info)
{
for (int i = 1; (unsigned)i < info.size(); i++)
{
    for (int j = 0; (unsigned)j < info[i].size(); j++)
    {
        if (info[i][j] == wall)
        {
            map[i - 1][j] = block;
        }
        else if (info[i][j] == path)
        {
            map[i - 1][j] == '  ';
        }
        else if (info[i][j] == start)
        {
            map[i - 1][j] = 'S';
            startingX = (i - 1);
            startingY = j;
        }
        else if (info[i][j] == end)
        {
            map[i - 1][j] = 'E';
            endingX = (i - 1);
            endingY = j;
        }
        else if (info[i][j]>=bonus)
        {
            map[i - 1][j] = 'B';
        }
    }
}
print();
cout << endl;
if (solve(startingX, startingY))
{
    print();
}
else
{
    cout << "DAMN" << endl;
}

}

The above is the function in which I read the input file called "info" and translate to the Maze called "map".

bool Maze::solve(int a, int b)
{

    map[b][a] = road;
    cout << "yes"<<endl;
    if (a == endingX&&b == endingY)
    {
        cout << "yes1" << endl;
        return true;
    }
    else if (a > 0 && map[b][a - 1] ==free && solve((a - 1), b))
    {
        cout << "yes2" << endl;
        return true;
    }
    else if (a < map[0].size() && map[b][a + 1] == free&& solve((a + 1), b))
    {
        cout << "yes3" << endl;
        return true;
    }
    else if (b > 0 && map[b - 1][a] == free && solve(a, (b - 1)))
    {
        cout << "yes4" << endl;
        return true;
    }
    else if (b < map.size() && map[b + 1][a] == free && solve(a, (b + 1)))
    {
        cout << "yes5" << endl;
        return true;
    }
    map[b][a] == free;
    //print();
    return false;
}

and that is my solving code. I try to use the recursive method but seems like it doesn't work but just showing me the starting point only.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Shi Yi Lee
  • 37
  • 8
  • 6
    Start with the smallest maze possible that causes your program to fail, then use a debugger to step through the code line by line. Or add more debug output, for every little thing that happens. – Some programmer dude Mar 16 '16 at 12:51
  • in `Maze::create` `map[i - 1][j] == ' ';` should be `map[i - 1][j] = ' ';` – Jonas Mar 16 '16 at 13:00
  • 3
    You also seem to be using the compare-for-equality operator when you mean to use assignment. And you do that *everywhere*. That will of course not work very well and the compiler should have given you warnings about it (statements or expressions with no effect or similar). If you don't get any warning you need to enable more warnings by the compiler. Warnings are good, it tells you about things that doesn't really break the rules, but may be wrong in other ways. – Some programmer dude Mar 16 '16 at 13:01
  • _everywhere_? I only see a problem in the line I pointed out in my previous comment and `map[b][a] == free;` in `Maze::solve`, also depending on your maze you might be running in circles for ever – Jonas Mar 16 '16 at 13:11
  • 1
    @Jonas Okay, *almost* everywhere... :) Enough for me to wonder if the OP knows the difference between assignment `=` and compare-for-equality `==`. – Some programmer dude Mar 16 '16 at 13:21
  • 1
    is it on purpose, that you have the indices in reverse order in the map and in the function call ? I mean `map[b][a]` vs `solve(a,b)` – 463035818_is_not_an_ai Mar 16 '16 at 13:44

2 Answers2

4

1) Take care not to confuse assignment and checking for equality:

  • in your create function, second option : map[i - 1][j] == ' ';
  • in your solve function, at the bottom : map[b][a] == free;

As such, no data is saved in map. The equality test just returns false and discards that boolean.

2) Also, I don't see any declaration/initialization of map, startingX, etc. But since you do not mention compilation errors, I guess you just didn't copy them to SO.

In conclusion, I bet your problems result from the first problem.

mr_T
  • 2,571
  • 3
  • 22
  • 39
2

1) you use X value as first coordinate in Maze::create() and in calling Maze::solve() but as second coordinate into Maze::solve()

2) when you write, in Maze::solve(), if (a < map[0].size() && map[b][a + 1] == free&& solve((a + 1), b)), you can write in a+1 where a+1 == map[0].size(); a+1 must be less than size

max66
  • 65,235
  • 10
  • 71
  • 111