2013-04-22 2 views
0

Я уже некоторое время изучаю «мертвый код» и «недостижимый код», и я все еще не могу понять, что происходит с этой проблемой в моей программе. Это фрагмент того, что у меня есть; метод «gameEnd()», который проверяет победителей в Tic Tac Toe:Недоступный код в Tic Tac Toe Program

private boolean gameEnd() { 
    // Setting local variables 
    int x = xMouseSquare; 
    int y = yMouseSquare; 
    int[][] g = gameBoard; 
    int c = CPU; 
    int h = HUMAN; 

    // Checking for a winner 

    /* Checking columns (xMouseSquare) 
    * Enter the y value, the vertical value, first; then x, the horizontal value, second 
    */ 

    // Set y equal to 0 and then add 1 
    for (y = 0; y < 3; y++) { 
     // Set CPU c equal to 0 
     c = 0; 
     // Set x equal to 0 and then add 1 
     for (x = 0; x < 3; x++) { 
      // Add CPU's value to the game board 
      c += g[x][y]; 

      // If statement returning the absolute value of CPU 
      if (Math.abs(c) == 3) { 
       // If these values are correct, return true; the game ends with CPU win horizontally 
       return true; 
      } 
     } 
    } 
    // If not, return false; game continues until all marks are filled 
    return false; 
    // Set y equal to 0 and then add 1 
    for (y = 0; y < 3; y++) { 
     // This time, however, set HUMAN h equal to 0 
     h = 0; 
     // Set x equal to 0 and then add 1 
     for (x = 0; x < 3; x++) { 
      // Then add HUMAN's value to the game board 
      h += g[x][y]; 
      // If statement returning the absolute value of HUMAN 
      if (Math.abs(h) == -3) { 
       // If these values are correct, return true; the game ends with HUMAN win horizontally 
       return true; 
      } 
     } 
    } 
    // If not, return false; game continues until all marks are filled 

    return false; 
    { 
     /* Checking rows (yMouseSquare) 
     * Enter the x value, the horizontal value, first; then y, the vertical value, second 
     */ 
     // Set x equal to 0 and then add 1 
     for (x = 0; x < 3; x++) { 
      // Set CPU equal to 0 
      c = 0; 
      // Set y equal to 0 and then add 1 
      for (y = 0; y < 3; y++) { 
       // Add CPU's value to the game board, but with y and then x 
       c += g[y][x]; 
       // If statement returning the absolute value of CPU 
       if (Math.abs(c) == 3) { 
        // If these values are correct, return true; the game ends with CPU win vertically 
        return true; 
       } 
      } 
     } 
     // If not, return false; game continues until all marks are filled 
     return false; 
     { 
      // Set x equal to 0 and then add 1 
      for (x = 0; x < 3; x++) { 
       // This time, however, set HUMAN h equal to 0 
       h = 0; 
       // Set y equal to 0 and then add 1 
       for (y = 0; y < 3; y++) { 
        // Then add HUMAN's value to the game board 
        h += g[x][y]; 
        // If statement returning the absolute value of HUMAN 
        if (Math.abs(h) == -3) { 
         // If these values are correct, return true; the game ends with CPU win vertically 
         return true; 
        } 
       } 
      } 
      // If not, return false; game continues until all marks are filled 
      return false; 
     } 
    } 
} 
} // error on this bracket; but when I remove it, some of the code above becomes unreachable. Can anyone point to what I'm doing wrong? 
+0

Ошибка будет гораздо легче найти, если вы формат (отступ) ваш код правильно. Ваша IDE может даже сделать это за вас. – jlordo

+0

Во-первых, исправьте свой отступ в соответствии с вашими скобками - сейчас это очень неправильно. Если вы используете хороший редактор, он должен иметь возможность сделать это для вас автоматически. –

+1

Когда вы возвращаете false, 'таким образом, вы выходите из метода. 2/3rds вашего кода никогда не будут выполнены (они также недоступны). – Supericy

ответ

4

Если он отступом правильно, я думаю, было бы показать, что первое вхождение «вернуться ложным» всегда будет выполняться, если первый экземпляр «return true» не попал, поэтому весь оставшийся код никогда не был достигнут.

1

Вот ваша проблема здесь

// If not, return false; game continues until all marks are filled 
     return false; <-- code exits here, everything below will not run. 
     { 
      // Set x equal to 0 and then add 1 
      for (x = 0; x < 3; x++) { 
...