如何通过使用for循环来提高效率



我是一名初级java程序员,我正在使用2D数组制作一个简单的TicTacToe游戏,这些是我的if语句,用于检查玩家1或玩家2是否获胜。我相信这可以通过使用for循环来简化,但是我不明白如何使用该方法。

if ((grid[0][0] == 1 && grid[0][1] == 1 && grid[0][2] == 1)
            || (grid[1][0] == 1 && grid[1][1] == 1 && grid[1][2] == 1)
            || (grid[2][0] == 1 && grid[2][1] == 1 && grid[2][2] == 1)
            || (grid[0][0] == 1 && grid[1][1] == 1 && grid[2][2] == 1)
            || (grid[0][2] == 1 && grid[1][1] == 1 && grid[2][0] == 1)
            || (grid[0][0] == 1 && grid[1][0] == 1 && grid[2][0] == 1)
            || (grid[0][1] == 1 && grid[1][1] == 1 && grid[2][1] == 1)
            || (grid[0][2] == 1 && grid[1][2] == 1 && grid[2][2] == 1)
            && won == false) {
        title.setText("X wins!");
        won = true;
    } else if ((grid[0][0] == 2 && grid[0][1] == 2 && grid[0][2] == 2)
            || (grid[1][0] == 2 && grid[1][1] == 2 && grid[1][2] == 2)
            || (grid[2][0] == 2 && grid[2][1] == 2 && grid[2][2] == 2)
            || (grid[0][0] == 2 && grid[1][1] == 2 && grid[2][2] == 2)
            || (grid[0][2] == 2 && grid[1][1] == 2 && grid[2][0] == 2)
            || (grid[0][0] == 2 && grid[1][0] == 2 && grid[2][0] == 2)
            || (grid[0][1] == 2 && grid[1][1] == 2 && grid[2][1] == 2)
            || (grid[0][2] == 2 && grid[1][2] == 2 && grid[2][2] == 2)
            && won == false) {
        title.setText("O wins!");
        won = true;
    }

下面是修改后的代码,它使用的if语句和条件要少得多。

public static boolean hasWon(int[][] grid) {
    for (int a = 1; a <= 2; a++) {
        for (int b = 0; b < grid.length; b++) {
            // Checking for win in horizontal, then vertical, then diagonal
            if (grid[b][0] == a && grid[b][1] == a && grid[b][2] == a) {
                won = true;
            } else if (grid[0][b] == a && grid[1][b] == a && grid[2][b] == a) {
                won = true;
            } else if ((grid[0][0] == a && grid[1][1] == a && grid[2][2] == a
                    || (grid[0][2] == a && grid[1][1] == a && grid[2][0] == a))) {
                won = true;
            }
        }
    }
}

为了帮助你自己找到解决方案,我现在给你一些提示。

提示#1:思考获胜意味着什么。玩家必须在一行中获得3个他们的标记——水平的、垂直的或对角线的。考虑一下如何在你的程序中表示它。

提示#2:考虑如何将问题分解成更小更易于管理的部分。考虑每个获胜的场景有什么共同点,并将逻辑分离到一个可以多次调用的方法中。

提示#3:考虑每个获胜场景的独特之处,以及如何使用grid生成您想要检查的空间的表示,从而更容易检查获胜。

如果你不确定for循环是如何工作的或Java语言的其他方面,你可以在Oracle的网站

找到教程。

你是对的。For循环才是正确的选择。下面是实现它的一种方法。

    public class tictactoe {
    public static void main(String[] args) {
        int[][] grid = {{1, 2, 1},
                {1, 2, 1},
                {2, 0, 1}};
        boolean won = hasWon(grid);

    }
    public static boolean hasWon(int[][] grid){
        for (int player = 1; player <= 2; player++){
            boolean playerWon = false;
            for(int i = 0; i < 3; i++){
                //Horizonal win
                playerWon = (grid[i][0] == player && grid[i][1] == player && grid[i][2] == player) || playerWon;
                //Vertical Win
                playerWon = (grid[0][i] == player && grid[1][i] == player && grid[i][2] == player) || playerWon;
            }
            //Diagonal Win
            playerWon = (grid[0][0] == player && grid[1][1] == player && grid[2][2] == player) || playerWon;
            playerWon = (grid[0][2] == player && grid[1][1] == player && grid[2][0] == player) || playerWon;
            if(playerWon){
                if(player == 1){
                    System.out.println("X wins!");
                    return true;
                }
                else{
                    System.out.println("O wins!");
                    return true;
                }
            }
        }
        //neither have won
        return false;
    }
}

不是对问题的直接回答。(因为这不是"check all at once"的样式)

简化

,
1. 单击单元格时检查。
2. 条件取决于单击单元格的位置以及单击该单元格的人。
3.如果有人赢了,结束游戏。


代码示例

// Part of codes.(not tested.)
// Each cell has three states (0, 1, or 2)
int player = 1; // (Not written here but) switch this each turn (1 or 2)
// In some place (activity.onCreate() etc)
{
    // For on click event(0, 0)
    cell_0_0.setOnClickListener(
        new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                grid[0][0] = player;
                final boolean isEnd = checkEnd_0_0();
                if (isEnd) {
                    // Call some function to end the game.
                    // Calling title.setText() in game end function maybe good.
                    // (as not needed to write many times.)
                    if (player == 1) {
                        title.setText("X wins!");
                    } else {
                        title.setText("O wins!");
                    }
                } else {
                    switchPlayer(); // Not written in this code.
                }
            }
        };
    );
    ...
}
// Call this on cell(0, 0) click event
// Returns true if someone wins.
boolean checkEnd_0_0() {
    // Omit checking grid[0][0] is 1 or 2 as it is clear.
    // Check horizontal.
    if (grid[0][1] == player) {
        if (grid[0][2] == player) {
            return true; // This is the case shown in question.
        }
    }
    // Check other cases (vertical, diagonal)
    ...
    // No match.
    return false;
}

相关内容

  • 没有找到相关文章

最新更新