检查我的TicTacToe游戏的赢家方法。

问题描述 投票:0回答:1

我试图在我的TicTacToe游戏中使用checkgamewinner方法,但我在循环开始或结束时遇到了问题。每当我开始打开我的游戏并点击一个字段时,它就会打印出 "没有赢家"。这是我目前的代码。

public String checkGameWinner(char [][]grid) {
    boolean winner = false;
    String result = "None";
    while(!winner) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (gridAt(0, 0) == 'x' & gridAt(0, 1) == 'x' & gridAt(0, 2) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(0, 0) == 'x' & gridAt(1, 0) == 'x' & gridAt(2, 0) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(0, 0) == 'x' & gridAt(1, 1) == 'x' & gridAt(2, 2) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(1, 0) == 'x' & gridAt(1, 1) == 'x' & gridAt(1, 2) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(2, 0) == 'x' & gridAt(2, 1) == 'x' & gridAt(2, 2) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(0, 1) == 'x' & gridAt(1, 1) == 'x' & gridAt(2, 1) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(0, 2) == 'x' & gridAt(1, 2) == 'x' & gridAt(2, 2) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(0, 2) == 'x' & gridAt(1, 1) == 'x' & gridAt(2, 0) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(0, 0) == 'x' & gridAt(0, 1) == 'x' & gridAt(0, 2) == 'x') {
                    winner = true;
                    System.out.println("Player x has won");
                    break;
                } else if (gridAt(0, 0) == 'o' & gridAt(1, 0) == 'o' & gridAt(2, 0) == 'o') {
                    winner = true;
                    System.out.println("Player o has won");
                } else if (gridAt(0, 0) == 'o' & gridAt(1, 1) == 'o' & gridAt(2, 2) == 'o') {
                    winner = true;
                    System.out.println("Player o has won");
                } else if (gridAt(1, 0) == 'o' & gridAt(1, 1) == 'o' & gridAt(1, 2) == 'o') {
                    winner = true;
                    System.out.println("Player o has won");
                } else if (gridAt(2, 0) == 'o' & gridAt(2, 1) == 'o' & gridAt(2, 2) == 'o') {
                    winner = true;
                    System.out.println("Player o has won");
                } else if (gridAt(0, 1) == 'o' & gridAt(1, 1) == 'o' & gridAt(2, 1) == 'o') {
                    winner = true;
                    System.out.println("Player o has won");
                } else if (gridAt(0, 2) == 'o' & gridAt(1, 2) == 'o' & gridAt(2, 2) == 'o') {
                    winner = true;
                    System.out.println("Player o has won");
                } else if (gridAt(0, 2) == 'o' & gridAt(1, 1) == 'o' & gridAt(2, 0) == 'o') {
                    winner = true;
                    System.out.println("Player o has won");
                } else {
                    result = "no winner";
                    System.out.println(result);
                }
            }

        }
    }
    return result;
}

我不知道我还能做什么,我用booleans玩了一下,但当我的棋盘上有赢家时,它就不会停止......我希望有人能帮我一下,谢谢!

java jpanel tic-tac-toe
1个回答
0
投票

你不需要同时使用循环和if else条件。我已经创建了带有if else条件的示例程序。

public class Main
{
    public static void main(String[] args) {
        char [][]grid = {
                {'x', 'x', 'x'},
                {'x', 'x', 'x'},
                {'x', 'x', 'x'}
        };
        checkGameWinner(grid);
    }

    public static String checkGameWinner(char [][]grid) {
        String result = "None";
        boolean winner = true;
        if (grid[0][0] == 'x' && grid[0][1] == 'x' && grid[0][2] == 'x') {
            result = "Player x has won";
        } else if (grid[0][0] == 'x' && grid[1][0] == 'x' && grid[2][0] == 'x') {
            result = "Player x has won";
        } else if (grid[0][0] == 'x' && grid[1][1] == 'x' && grid[2][2] == 'x') {
            result = "Player x has won";
        } else if (grid[1][0] == 'x' && grid[1][1] == 'x' && grid[1][2] == 'x') {
            result = "Player x has won";
        } else if (grid[2][0] == 'x' && grid[2][1] == 'x' && grid[2][2] == 'x') {
            result = "Player x has won";
        } else if (grid[0][1] == 'x' && grid[1][1] == 'x' && grid[2][1] == 'x') {
            result = "Player x has won";
        } else if (grid[0][2] == 'x' && grid[1][2] == 'x' && grid[2][2] == 'x') {
            result = "Player x has won";
        } else if (grid[0][2] == 'x' && grid[1][1] == 'x' && grid[2][0] == 'x') {
            result = "Player x has won";
        } else if (grid[0][0] == 'x' && grid[0][1] == 'x' && grid[0][2] == 'x') {
            result = "Player x has won";
        } else if (grid[0][0] == 'o' && grid[1][0] == 'o' && grid[2][0] == 'o') {
            result = "Player o has won";
        } else if (grid[0][0] == 'o' && grid[1][1] == 'o' && grid[2][2] == 'o') {
            result = "Player o has won";
        } else if (grid[1][0] == 'o' && grid[1][1] == 'o' && grid[1][2] == 'o') {
            result = "Player o has won";
        } else if (grid[2][0] == 'o' && grid[2][1] == 'o' && grid[2][2] == 'o') {
            result = "Player o has won";
        } else if (grid[0][1] == 'o' && grid[1][1] == 'o' && grid[2][1] == 'o') {
            result = "Player o has won";
        } else if (grid[0][2] == 'o' && grid[1][2] == 'o' && grid[2][2] == 'o') {
            result = "Player o has won";
        } else if (grid[0][2] == 'o' && grid[1][1] == 'o' && grid[2][0] == 'o') {
            result = "Player o has won";
        } else {
            result = "no winner";
            winner = false;
        }
        System.out.println(result);
        return result;
    }
}

注意:以上不是解决这个问题的最好方法。 以上并不是解决这个问题的最佳方法。


0
投票

为什么不使自己更容易,并创建这样的方法。

private boolean checkRows() {
}
private boolean checkColumns() {
}
private boolean checkDiagnonals() {
}

这样的话,每个方法中的测试次数就会减少,而且它们作为单独的方法可以更容易地进行调试。

if (checkRows() || checkColumns() || checkDiagonals()) {
  // winning code here.
}

我假设它们是实例方法,所以它们可以访问到 char[][] grid.

关于你现在的代码。

  • 把while循环去掉,它没有任何作用
  • 去掉for循环。你已经在检查显式索引了。
  • 当你决定谁赢的时候,打印出来,然后简单地 return true.
  • 如果你到了所有测试的最后,没有赢家。return false.

为了调试,建立两个测试用例 xo 所以你知道该去哪里找


-1
投票

我没有检查过你的逻辑,但从我的观察来看,你提到的问题是因为当你打开游戏的时候,你的格子会完全是空的,因此它会转到 else 块来执行。

希望能给你指引正确的方向,如何正确的写逻辑。

另外,尽量把你的方法正确的划分,这样才容易阅读、调试和维护。

祝你学习愉快!

© www.soinside.com 2019 - 2024. All rights reserved.