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

时间:2016-10-23 01:40:40

标签: java arrays if-statement multidimensional-array

我是初学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;
    }

以下是修改后的代码,如果使用法规和条件,则使用的代码要少得多。

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;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

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

提示#1:想想获胜意味着什么。玩家必须连续获得3个代币 - 水平,垂直或对角线。想想如何在你的程序中表现出来。

提示#2:想想你如何将问题分解成更小的更易处理的部分。考虑每个获胜场景的共同点,并将该逻辑分成可以多次调用的方法。

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

如果您不确定for循环如何工作或Java语言的其他方面,您可以在Oracle's site上找到教程

答案 1 :(得分:0)

是的,你是对的。 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;
    }
}

答案 2 :(得分:0)

不是这个问题的直接答案。 (因为这不是“一次检查所有”的风格)

简化,
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;
}