
First you check if there is a path ahead, if there isn’t you check if there is a path to the left you turn left and move forward, if there isn’t you check if there is a path to the right, if it has one you turn right if it doesn’t have one you turn left.