user643718
user643718

Reputation: 45

PHP Recursion doesn't seem to be traversing all avenues

This program is supposed to find all the possible ways down through a maze, and save the farthest distance down it got in a global variable. Instead it only travels one path and then ends? I'm guessing I'm not using recursion correctly or something. $draw_co2 is an array of 0 and 1s, where 1 is a path and 0 is a wall. I've included the output from the points collected by $GLOBALS['route'] on top of the maze itself below. Based on the code, I'd expect the whole main red branch to be white?

I was using else if instead of just else. As mentioned below, use else in order to explore all branches.

Upvotes: 0

Views: 45

Answers (1)

Oukaasha Habib
Oukaasha Habib

Reputation: 89

There is no problem with recursion in the code snippet. My best guess is that the only thing that could be causing this, is $draw_co2. I think there is no path in $draw_co2 which recursion can traverse. For testing, I would recommend to create a definite path in the array $draw_co2 and pass the starting coordinates in the recursion function. Hope it helps.

EDIT

If you change all of your else ifs to only if, this could traverse all paths.

Upvotes: 2

Related Questions