java - Tic Tac Toe 程序中无法访问的代码

标签 java

我已经研究“死代码”和“无法访问的代码”有一段时间了,但我似乎仍然无法弄清楚我的程序中这个问题是怎么回事。这是我所拥有的一个片段; “gameEnd()”方法检查 Tic Tac Toe 中的获胜者:

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?

最佳答案

如果缩进正确,我认为它会表明,如果未命中第一个“return true”实例,则将始终执行第一次出现的“return false”,因此永远不会到达所有剩余代码。

关于java - Tic Tac Toe 程序中无法访问的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16138805/

相关文章:

java - 使用 JPA 删除实体时出现问题

java - 不从发布版本下载 jar

java - 使用 TrueZip 将 .tar.gz 文件转换为 .zip?

java - 在字符串第 n 次出现后追加字符串

java - SurfaceView运行方法代码

java - 使用 RadioButtons 实现 TextWatcher

java - 当我在 hadoop 中运行 map/reduce 作业时,遇到 XMLJAXBElementProvider 异常

java - 与远程计算机上运行的 SQL Server 2005 实例的 JDBC 连接

java - 没有接口(interface)使用spring DI是否正确

java - 如何获取Java Web应用程序路径?