title: Refactoring two similar if loops with slightly different values (Java)
body: <pre><code>public boolean hasCapturableEnemy(Piece[][] board, int startX, int startY, int endX, int endY) {
  
      //If the Pawn belong to the Upper Team (Faces downward)
      if(board[startX][startY] != null &amp;&amp; board[startX][startY].getTeam() == Player.UP) {
          //If a Piece exists on a diagonally adjacent tile, return true
          if(startX - endX == -1 &amp;&amp; Math.abs(startY - endY) == 1) {
              if( (board[startX + 1][startY - 1] != null &amp;&amp; board[startX + 1][startY - 1].getTeam() != Player.UP) || 
                      (board[startX + 1][startY + 1] != null &amp;&amp; board[startX + 1][startY + 1].getTeam() != Player.UP)) {
                  return true;
              }
          }
      } 
  
      if(board[startX][startY] != null &amp;&amp; board[startX][startY].getTeam() == Player.DOWN) { 
          //If the Pawn belongs to the Down Team (Faces upward)
  
          if(startX - endX == 1 &amp;&amp; Math.abs(startY - endY) == 1) {
              //If a Piece exists on a diagonally adjacent tile, return true
              if( (board[startX - 1][startY - 1] != null &amp;&amp; board[startX - 1][startY - 1].getTeam() != Player.DOWN) || 
                      (board[startX - 1][startY + 1] != null &amp;&amp; board[startX - 1][startY + 1].getTeam() != Player.DOWN)) {
                  return true;
              }
          }
      }
  
      return false;
  
  }
  </code></pre>
  
  <p>I have this function that contains two similar if statements that is kind of hard to refactor. </p>
  
  <p>I am saying this because these two share significant portion of duplicated codes but because of the part where differs slightly like board[startX + 1][startY - 1] and board[startX - 1][startY - 1], I find it difficult to refactor it efficiently.</p>
  
  <p>In other words, I can surely refactor this by creating another function that contains the common parts, but I am afraid of creating few more if loops inside that function and making the code dirtier.</p>
  
  <p>Any advice for refactoring this kind of code?</p>