如何使 if 语句更小并避免在 java 中出于相同原因使用太多循环

此代码用于我的一项任务(连接四个)。我需要使这段代码少于 25 行,并使“if”语句更短。此外,该板有 6 行和 7 列。我的代码试图弄清楚一个人是否赢了。


我试图将所有循环合并为一个循环,但这并没有给我一个正确的答案。


public static boolean determineWin(String[][] board) {

    boolean won = false;


    for (int i = 0; i < 6; i++) {

        for (int j = 0; j < 4; j++) {

            if (board[i][j] != ". ") {

                if (board[i][j].equals(board[i][j+1]) && board[i][j+1].equals(board[i][j+2]) && board[i][j+2].equals(board[i][j+3])) {

                    won = true;

                    break;

                }

            }

        }

    }


    for (int i = 5; i > 2; i--) {

        for (int j = 6; j > 2; j--) {

            if (board[i][j] != ". ") {

                if (board[i][j].equals(board[i-1][j-1]) && board[i-1][j-1].equals(board[i-2][j-2]) && board[i-2][j-2].equals(board[i-3][j-3])){

                    won = true;

                    break;

                }

            }

        }


        for (int j = 0; j < 4; j++) {

            if (board[i][j] != ". ") {

                if (board[i][j].equals(board[i-1][j+1]) && board[i-1][j+1].equals(board[i-2][j+2]) && board[i-2][j+2].equals(board[i-3][j+3])){

                    won = true;

                    break;

                }

            }

        }


        for (int j = 0; j < 7; j++) {

            if (board[i][j] != ". ") {

                if (board[i][j].equals(board[i-1][j]) && board[i-1][j].equals(board[i-2][j]) && board[i-2][j].equals(board[i-3][j])){

                    won = true;

                    break;

                }

            }

        }

    }


    return won;

}

结果应该与上面的代码相同,但我只需要代码稍微小一点(25 行)并且 if 语句更短。


泛舟湖上清波郎朗
浏览 83回答 1
1回答

三国纷争

上面的代码效率低下,因为它有 4 个单独for的循环(用于跟踪您可以获胜的 4 个方向:1)从左到右,2)从上到下,3)对角线 4)对角线/其他方向 -AND- 因为if语句必须检查 4 个连续的位置。为了优化解决方案,您可以认识到您可以在棋盘中的每个位置保持已经发生的情况,对于您可以赢得的 4 个可能方向中的每一个(4 个唯一状态)state。how many consecutive same pieces以在水平方向获胜为例。当您沿着同一行从左向右移动时,如果左侧的块相同,则状态计数器将增加 1。如果有“.”,则计数器重置为 0。如果有不同的棋子,则计数器重置为 1。如果这 4 个状态计数器中的任何一个变为 4,则您处于获胜位置。下面的代码对于水平(状态变量0)和垂直(状态变量1)的获胜方向是完整的。将完成表示每个对角线方向的两行(状态变量 2 和 3)留作练习。public static boolean determineWin(String[][] board) {&nbsp; &nbsp; int[][][] counters = new int[board[0].length+1][board.length+1][4];&nbsp; &nbsp; for (int y=0; y<board.length; y++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int x=0; x<board[0].length; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!board[y][x].equals(".")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counters[y][x][0] = (x>0 && board[y][x].equals(board[y][x-1])) ? counters[y][x-1][0] + 1 : 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counters[y][x][1] = (y>0 && board[y][x].equals(board[y-1][x])) ? counters[y-1][x][1] + 1 : 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Diagonal 1 TODO:&nbsp; counters[y][x][2] =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Diagonal 2 TODO:&nbsp; counters[y][x][3] =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (counters[y][x][0] == 4 || counters[y][x][1] == 4 || counters[y][x][2] == 4 || counters[y][x][3] == 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java