title: How to check for the previous path searched on a maze C#
body: <p>I am trying to code an algorithm that solves a maze problem but I am facing some difficulty to apply it correctly. </p>
  
  <p>The algorithm runs over the walls instead of changing the direction after finding the valid point.</p>
  
  <p><a href="https://github.com/ewerton-azevedo/Ausbildung/tree/master/TheMaze/TheMaze" rel="noreferrer">Complete Code on Github</a></p>
  
  <p>I do not understand clearly how to check for the previousPoint and then from that point check for the next valid move.</p>
  
  <p>Could someone help me giving me some tips on which direction I could go?</p>
  
  <pre><code>class MapPathFinder
  {
      public bool[,] correctPath = new bool[12,12];
      public int[,] previousPoint = new int[12, 12];
      public bool startPointFound = false;
      public bool nextValidMove(MapFile map, int y, int x)
      {
          if ((y == map.width) &amp;&amp; (x == map.height)) { 
  
              return false; //Checks if at the edge and terminates the method
          }
  
          if ((map.Matrix[y, x]) == 1 ) {
              return true; // check if at a wall and terminate the method
          }
  
          if (y != 0)
          {
              if (nextValidMove(map, y-1,x))
              {
                  map.Matrix[y, x] = 9; //changes the color of the position
                  correctPath[y, x] = true;
                  return correctPath[y, x];
              }
  
              if (y != map.width - 1) //check if at the limit of the map
              {
                  if (nextValidMove(map,y + 1, x))
                  {
                      map.Matrix[y, x] = 9;
                      correctPath[y, x] = true;
                      return correctPath[y, x];
                  }       
              }
  
              if (x != 0)
              {
                  if (nextValidMove(map, y, x - 1))
                  {
                      map.Matrix[y, x] = 9;
                      correctPath[y, x] = true;
                      return correctPath[y, x];
                  }
              }
  
              if (x != map.height - 1)
              {
                  if (nextValidMove(map, y, x + 1))
                  {
                      map.Matrix[y, x] = 9;
                      correctPath[y, x] = true;
  
                      return correctPath[y, x];
                  }
              }
          }
          return false;
      }
  
      public bool PathFinder(MapFile map)
      {
          for (int y = 1; y &lt; map.width; y++)
          {
              for (int x = 1; x &lt; map.height; x++)
              {
                 var status = MapDisplay.DisplayMap(map);
                   if (status)
                 {
                     nextValidMove(map, x, y);
                 }
              }           
          }
          return true;
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Gvwph.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Gvwph.png" alt="Example"></a></p>
  
  <p><a href="https://i.stack.imgur.com/7UeXM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7UeXM.png" alt="How it should behave"></a></p>
  
  <p>I tried to implement the answer given by Paul but could not really get anywhere from it and I am completely lost.</p>
  
  <p>That is what I got from your answer:</p>
  
  <pre><code>public bool nextValidMove(MapFile map, int y, int x)
  {
      if ((y == map.width) || (x == map.height)) return false; 
  
      if(y&lt;0 || x&lt;0) return false;
  
      if ((map.Matrix[y, x]) == 1) return true; // check if at a wall and terminate the method
  
      if (map.Matrix[y, x] == 5) return map.end;
  
      if (y - 1 &gt;= 0 &amp;&amp; map.Matrix[y-1, x] == 2 &amp;&amp; !nextValidMove(map, y-1, x))
      {
          map.Matrix[y, x] = 9;
          previousPoint[y, x] = map.Matrix[y, x];
          return false;
      }
      //  Test the East wall...
      if (x + 1 &lt;= map.width - 1 &amp;&amp; map.Matrix[y + 1, x] == 2 &amp;&amp; !nextValidMove(map, y, x+1))
      {
          map.Matrix[y, x] = 9;
          previousPoint[y, x] = map.Matrix[y, x];
          return false;
      }
      //  Test the South wall...
      if (y + 1 &lt;= map.height - 1 &amp;&amp; map.Matrix[y, x + 1] == 2 &amp;&amp; !nextValidMove(map, y+1,x))
      {
          map.Matrix[y, x] = 9;
          previousPoint[y, x] = map.Matrix[y, x];
          return false;
      }
      //  Test the West wall...
      if (x - 1 &gt;= 0 &amp;&amp; map.Matrix[y, x - 1] == 2 &amp;&amp; !nextValidMove(map, y, x-1))
      {
          map.Matrix[y, x] = 9;
          previousPoint[y, x] = map.Matrix[y, x];
          return false;
      }
  
      return false;
  }
  </code></pre>
  
  <p>When I run it I get a stack overflow error.</p>
  
  <p>When I am checking the possible points and calling the function recursively with </p>
  
  <pre><code>!nextValidMove(map, y-1, x)
  </code></pre>
  
  <p>I don't really understand why I am checking the nextValidMove(y-1,x) since it was already true at the begining of my if statement:</p>
  
  <pre><code>if(map.Matrix[y-1, x] == 2 &amp;&amp; !nextValidMove(y-1,x))
  </code></pre>
  
  <p>I thought of checking the previousPoint together, like this:</p>
  
  <pre><code>if(nextValidMove(map, y - 1, x)&amp;&amp;!previousPoint[y-1,x])
  </code></pre>
  
  <p>But I am getting a stackoverflow error. I have no clue how to get out of there anymore.</p>