我有一个程序允许两个玩家玩提克泰索。在每个玩家移动之后,它应该在那一点显示棋盘,并返回一个名为status的枚举,显示玩家是否应该继续,如果一个玩家赢了,或者如果是平局。但是,算法要么返回stackoverflowerror,要么继续输入。这是我使用的算法。

       //Checks for winner by rows
       for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 1; j++) {
            if (board[i][j] == 'X') {
                if (board[i][j] == board[i][0 + 1] && board[i][j] == board[i][0 + 2]) {
                    printStatus(1);
                    return Status.WIN;
                } else {
                    return Status.CONTINUE;
                }
            } else if (board[i][j] == 'O') {
                if (board[i][j] == board[i][0 + 1] && board[i][j] == board[i][0 + 2]) {
                    printStatus(2);
                    return Status.WIN;
                } else {
                    return Status.CONTINUE;
                }
            }
        }
    }
    //Checks for winner by columns
    for (int i = 0; i < 1; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == 'X') {
                if (board[i][j] == board[0 + 1][j] && board[i][j] == board[0 + 2][j]) {
                    printStatus(1);
                    return Status.WIN;
                } else {
                    return Status.CONTINUE;
                }
            } else if (board[i][j] == 'O') {
                if (board[i][j] == board[0 + 1][j] && board[i][j] == board[0 + 2][j]) {
                    printStatus(1);
                    return Status.WIN;
                } else {
                    return Status.CONTINUE;
                }
            }
        }

    }
    //This group of if statements boards for winner diagnolly
    if (board[0][0] == 'X') {
        if (board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
            printStatus(1);
            return Status.WIN;
        } else {
            return Status.CONTINUE;
        }
    }else if (board[0][0] == '0') {
        if (board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
            printStatus(1);
            return Status.WIN;
        } else {
            return Status.CONTINUE;
        }
    }
    if (board[0][2] == 'O') {
        if (board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
            printStatus(1);
            return Status.WIN;
        } else {
            return Status.CONTINUE;
        }
    }else if (board[0][2] == 'X') {
        if (board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
            printStatus(1);
            return Status.WIN;
        } else {
            return Status.CONTINUE;
        }

    }

这里是printStatus方法。
private void printStatus(int player) {
    Status status = gameStatus();
    if (status == Status.DRAW) {
        System.out.println("The game has ended in a draw.");
        System.exit(0);
    } else if (status == Status.WIN) {
        System.out.println("Player " + player + " has won the game.");
        System.exit(0);
    } else if (status == Status.CONTINUE) {
        System.out.println("The game continues.");
        play();
    }

}

错误如下:
Exception in thread "main" java.lang.StackOverflowError
at tictactoe.TicTacToe.gameStatus(TicTacToe.java:86)
at tictactoe.TicTacToe.printStatus(TicTacToe.java:69)
at tictactoe.TicTacToe.gameStatus(TicTacToe.java:92)
    at tictactoe.TicTacToe.printStatus(TicTacToe.java:69)
at tictactoe.TicTacToe.gameStatus(TicTacToe.java:92)
at tictactoe.TicTacToe.printStatus(TicTacToe.java:69)

等等

最佳答案

您的问题是,您的代码会重复调用自己,从而创建一个永无止境的循环例如,如果方法A()有调用方法B()的代码,但在B()中有调用A()的代码,则代码将无限运行,因为A()调用B(),然后B()再次调用A(),循环重复stackoverflow错误常常表明这一点。
在您的例子中,这是因为您的函数gameStatus()(我假设这是您发布的代码的第一部分),调用printStatus(),然后在Status status = gameStatus();行中再次调用gamestatus()。
尝试在printstatus中将status作为参数传递,比如printStatus(2,Status.WIN);,而不是在printstatus中获取gamestatus的返回。

10-02 00:54