title: How can the logic of this snippet be abstracted?
body: <p>The following method is used to determine whether or not a chess piece is blocked from making a certain move. At the point that this method is called, the motion itself (i.e. a Bishop's ability to move diagonally) has already been validated -- this method will then look at the "path" that the piece must take.</p>
  
  <p>As is painfully clear, this method is full of redundancy. In fact, there are 6 nearly-identical for-loops, the differences being 1) which variables are controlling the iteration, 2) whether the variable is incrementing or decrementing, and 3), in the case of the diagonal motion, the inclusion of a statement to increment/decrement both the x and y variables simultaneously.</p>
  
  <p>I have made numerous attempts to abstract these statements into a separate method. Unfortunately, the limiting factor has been need to access the board[y][x] -- When I've tried to abstract the logic, I lose sight of <em>which</em> variable represents the y and which the x.</p>
  
  <p>So, my question is this: what tools can Java provide me to abstract this logic and reduce or eliminate redundancy in this method? I will point out that I am quite new to the language, so please don't take my disregard for common idioms as intentional or simply obtuse; I'm learning!</p>
  
  <p>Thanks.</p>
  
  <pre><code>private static boolean notBlocked(Piece[][] board, int xfrom, int yfrom, int xto, int yto) {
  
      int x = xfrom;
      int xstop = xto;
      int y = yfrom;
      int ystop = yto;
  
      int xinc = (x &lt; xstop) ? 1 : -1;
      int yinc = (y &lt; ystop) ? 1 : -1;
  
      Piece to = board[yto][xto];
      Piece from = board[yfrom][xfrom];
  
      if (xfrom == xto) {
          // x is constant, check in y direction
          if (y &lt;= ystop) {
              for (; y &lt;= ystop; y += yinc) {
                  if (board[y][x] != null &amp;&amp; board[y][x] != to &amp;&amp; board[y][x] != from) {
                      return false;
                  }
              }
          } else {
              for (; y &gt;= ystop; y += yinc) {
                  if (board[y][x] != null &amp;&amp; board[y][x] != to &amp;&amp; board[y][x] != from) {
                      return false;
                  }
              }
          }
      } else if (yfrom == yto) {
          // y is constant, check in x direction
          if (x &lt;= xstop) {
              for (; x &lt;= xstop; x += xinc) {
                  if (board[y][x] != null &amp;&amp; board[y][x] != to &amp;&amp; board[y][x] != from) {
                      return false;
                  }
              }
          } else {
              for (; x &gt;= xstop; x += xinc) {
                  if (board[y][x] != null &amp;&amp; board[y][x] != to &amp;&amp; board[y][x] != from) {
                      return false;
                  }
              }
          }
      } else if (Math.abs(xfrom - xto) == Math.abs(yfrom - yto)){
          // the move is diagonal
          if (y &lt;= ystop) {
              for (; y &lt;= ystop; y += yinc) {
                  if (board[y][x] != null &amp;&amp; board[y][x] != to &amp;&amp; board[y][x] != from) {
                      return false;
                  }
                  x += xinc;
              }
          } else {
              for (; y &gt;= ystop; y += yinc) {
                  if (board[y][x] != null &amp;&amp; board[y][x] != to &amp;&amp; board[y][x] != from) {
                      return false;
                  }
                  x += xinc;
              }
          }
      }
      return true;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Wow... much better now!</p>
  
  <pre><code>private static boolean notBlocked(Piece[][] board, int xfrom, int yfrom, int xto, int yto) {
  
      Piece from = board[yfrom][xfrom];
      Piece to = board[yto][xto];
  
      // Determine the direction (if any) of x and y movement
      int dx = (xfrom &lt; xto) ? 1 : ((xfrom == xto) ? 0 : -1);
      int dy = (yfrom &lt; yto) ? 1 : ((yfrom == yto) ? 0 : -1);
  
      // Determine the number of times we must iterate
      int steps = Math.max(Math.abs(xfrom - xto), Math.abs(yfrom - yto));
  
      if (xfrom == xto || yfrom == yto || Math.abs(xfrom - xto) == Math.abs(yfrom - yto)) {
          for (int i = 1; i &lt; steps; i++) {
              int x = xfrom + i * dx;
              int y = yfrom + i * dy;
              if (isBlocked(board, from, to, x, y)) {
                  return false;
              }
          }
      }
      return true;
  }
  </code></pre>