title: Chess, checking if path is free is not working correctly
body: <p>I'm making chess in Java using Swing, and I have already figured out how to check legal moves for every piece. There is nothing wrong with that part. But now, I am having problems with checking whether a path is free. For example, all the pieces in chess cannot jump over a piece unless it's a knight. If a pawn is blocking a bishop's path, it cannot go over than pawn. I called the method that checks whether a path is free <code>isVectorFree()</code>. It's working fine for horizontal and vertical paths but it's not working properly for diagonal paths.</p>
  
  <p>Here is the code for <code>isVectorFree()</code>:</p>
  
  <pre><code>public boolean isVectorFree(int x1, int y1, int x2, int y2) {
      if (x1 == x2 &amp;&amp; y1 &gt; y2) { // horizontal north
          for (int i = y1 - 1; i &gt; y2; i--) {
              if (piecePositions[i][x1] != null) {
                  return false;
              }
          }
      } else if (x1 &lt; x2 &amp;&amp; y1 == y2) { // horizontal east
          for (int i = x1 + 1; i &lt; x2; i++) {
              if (piecePositions[y1][i] != null) {
                  return false;
              }
          }
      } else if (x1 == x2 &amp;&amp; y1 &lt; y2) { // horizontal south
          for (int i = y1 + 1; i &lt; y2; i++) {
              if (piecePositions[i][x1] != null) {
                  return false;
              }
          }
      } else if (x1 &gt; x2 &amp;&amp; y1 == y2) { // horizontal west
          for (int i = x1 - 1; i &gt; x2; i--) {
              if (piecePositions[y1][i] != null) {
                  return false;
              }
          }
      }
      else if (x1 &lt; x2 &amp;&amp; y1 &gt; y2) { // diagonal northeast
          // these diagonals aren't working properly
          for (int row = x1 + 1; row &lt; x2; row++) {
              for (int col = y1 - 1; col &gt;= y2; col--) {
                  if (piecePositions[row][col] != null) {
                      return false;
                  }
              }
          }
      } else if (x1 &lt; x2 &amp;&amp; y1 &lt; y2) { // diagonal southeast
          for (int row = x1 + 1; row &lt; x2; row++) {
              for (int col = y1 + 1; col &lt; y2; col++) {
                  if (piecePositions[row][col] != null) {
                      return false;
                  }
              }
          }
      } else if (x1 &gt; x2 &amp;&amp; y1 &lt; y2) { // diagonal southwest
          for (int row = x1 - 1; row &gt;= x2; row--) {
              for (int col = y1 + 1; col &lt; y2; col++) {
                  if (piecePositions[row][col] != null) {
                      return false;
                  }
              }
          }
      } else if (x1 &gt; x2 &amp;&amp; y1 &gt; y2) { // diagonal northwest
          for (int row = x1 - 1; row &gt;= x2; row--) {
              for (int col = y1 - 1; col &gt;= y2; col--) {
                  if (piecePositions[row][col] != null) {
                      return false;
                  }
              }
          }
      }
      return true;
  }
  </code></pre>
  
  <p>This method takes the piece's x coordinate <code>x1</code> and y coordinate <code>y1</code> in the matrix <code>piecePositions</code> and the desired x coordinate <code>x2</code> and desired y coordinate <code>y2</code> in order to see if the path between the current coordinates and desired coordinates is free, but as I said earlier, this isn't working properly for the diagonals. Specifically, <code>isVectorFree()</code> is being affected by surrounding pieces. In the starting position of the chess board, if I move the pawns away to make way for the bishop, even if the path is now free for the bishop, it still cannot move anywhere throughout the path, for some reason.</p>
  
  <p>How can I fix this?</p>