My current recursive function works to a degree, but then ruins itself when it goes back down the stack.
void Graph::findPath( Room * curRoom )
{
if( curRoom -> myNumber == 0 )
{
cout << "Outside.\n";
//Escape the recursion!
}
else
{
curRoom -> visited = true;
if( curRoom -> North -> visited == false )
{
escapePath[ _index ] = "North";
cout << "_index: " << _index << "\n";
++_index;
findPath( curRoom -> North );
cout << "_index: " << _index << "\n";
escapePath[ _index ] = "";
--_index;
}
if( curRoom -> East -> visited == false )
{
escapePath[ _index ] = "East";
cout << "_index: " << _index << "\n";
++_index;
findPath( curRoom -> East );
cout << "_index: " << _index << "\n";
escapePath[ _index ] = "";
--_index;
}
if( curRoom -> South -> visited == false )
{
escapePath[ _index ] = "South";
cout << "_index: " << _index << "\n";
++_index;
findPath( curRoom -> South );
cout << "_index: " << _index << "\n";
escapePath[ _index ] = "";
--_index;
}
if( curRoom -> West -> visited == false )
{
escapePath[ _index ] = "West";
cout << "_index: " << _index << "\n";
++_index;
findPath( curRoom -> West );
cout << "_index: " << _index << "\n";
escapePath[ _index ] = "";
--_index;
}
}
}
To save you some reading, the idea is that the base case is finding 0. Otherwise it tries four different cardinal directions, which is a different numbered room. Every time it makes a move, it adds the move it made to an external array, and every time it comes back up it removes that step from the stack.
My issue is that it has the correct path stored when it finds the 0, but removes it on the way back up.
Is there a way to escape it, such as a break.
No gotos or exceptions
A first way to escape recursion is to evaluate everything then return 0 when the input list is empty. A second way to escape recursion is to evaluate everything but the last element, then either return the last thing or do something to the last thing and then return the result of that last function.
You don't "break" out of recursive functions. Trying to do so says you're thinking about them the wrong way. Currently your recursive call is ignoring the output, which means that the recursion is pointless; whatever is_pal(middle(str)) returns has no effect on the return value of your function.
A recursive function must have at least one condition where it will stop calling itself, or the function will call itself indefinitely until JavaScript throws an error. The condition that stops a recursive function from calling itself is known as the base case.
exceptions (or the C longjmp ) are the way to leave a recursion. You might also have a static boolean leaveme; flag and start your function with if (leaveme) return; etc. But longjmp don't work nicely with destructors of values in local frames (while exceptions do).
There is a way to exit recursion using exceptions, but I wouldn't recommend it. Instead, modify your function to return a bool that indicates whether you've found 0 or not and modify your logic to return from the function without changing path if 0 has been found. Here's the illustration of the idea:
bool Graph::findPath( Room * curRoom )
{
if( curRoom -> myNumber == 0 )
{
cout << "Outside.\n";
//Escape the recursion!
return true;
}
// ...
if (findPath( curRoom -> North ))
return true;
// ...
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With