My current recursive function works to some extent, but then crashes when it returns to 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 finds 0. Otherwise, it tries four different cardinal directions, which are another numbered room. Each time he makes a move, he adds the move made to the external array, and each time he returns, he removes that step from the stack.
My problem is that it has the correct path stored when it finds 0, but deletes it along the way back.
Is there a way to avoid this, for example, a gap.
No access or exceptions
source share