title: Why does this flood-fill algorithm cause a stack overflow?
body: <pre><code>void FloodFill(int layer, int x, int y, int target, int replacement)
  {
      if (x &lt; 0) return;
      if (y &lt; 0) return;
      if (x &gt;= _mapWidth) return;
      if (y &gt;= _mapHeight) return;
  
      if (_mapLayers[layer, x, y] != target) return;
  
      _mapLayers[layer, x, y] = replacement;
  
      FloodFill(layer, x - 1, y, target, replacement);
      FloodFill(layer, x + 1, y, target, replacement);
      FloodFill(layer, x, y - 1, target, replacement);
      FloodFill(layer, x, y + 1, target, replacement);
  
      return;
  }
  </code></pre>
  
  <p>This is my code so far, but when it reaches the end of the map it causes a Stack Overflow, does anybody know how to solve the problem (probably a tricky condition)?</p>