为什么我在 TicTacToe 中用于计算 X 和 O 胜利的变量会随着每次测试而变化? (Java)

Why do my variables used to count X and O wins in TicTacToe vary with every test? (Java)

我目前正在处理分配的项目,以在 java 中创建一个井字游戏。到目前为止,这是我所拥有的,所有代码都运行良好,游戏及其功能运行得非常好。我应该跟踪 X Wins、O Wins 和 Ties;然而,这似乎是问题所在。出于某种原因,尽管我指定计算机模拟井字游戏应该有 1000 次迭代,但 x 赢、o 赢和平局加起来永远不会达到 1000。它总是一个非常接近 1000 的数字,但我不是确定为什么它不断变化。这是我的主要代码 class:

    int count = 0;
    int xWins = 0;
    int oWins = 0;
    int ties = 0;
    boolean doPrint = true;
    while (count < 1000){
        randomTicTacToeGame(doPrint);


        if (randomTicTacToeGame(doPrint) == 1){
            xWins += 1;
        }
        if (randomTicTacToeGame(doPrint) == -1){
            oWins += 1;
        }
        if (randomTicTacToeGame(doPrint) == 0){
            ties += 1;
        }


        if (count >= 10){
            doPrint = false;
        }
        count++;
    }
    System.out.println();
    System.out.println("X Wins: " + xWins + "\n" + "O Wins: " + oWins + "\n" + "Ties: " + ties);

下面是实际井字游戏的代码:

public static int randomTicTacToeGame(boolean doPrint){
    squareResult [][] gameBoard = new squareResult[3][3];
    initializer(gameBoard);
    int turnModifier = 1;

    while (isMoveable(gameBoard)){
        if (turnModifier == 1){
            randomXMove(gameBoard);
        }
        if (turnModifier == -1){
            randomOMove(gameBoard);
        }
        if (doPrint){
            printBoard(gameBoard);
        }
        if (isXWin(gameBoard) || isOWin(gameBoard)){
            break;
        }
        turnModifier *= -1;
    }
    if (isXWin(gameBoard)){
        if (doPrint){
            System.out.println("X Wins!");
        }
        return 1;
    }
    else if (isOWin(gameBoard)){
        if (doPrint) {
            System.out.println("O Wins!");
        }
        return -1;
    }
    else{
        if (doPrint) {
            System.out.println("It's a tie!");
        }
        return 0;
    }

}
public static squareResult[][] initializer (squareResult[][] gameBoard){
    for (int i = 0; i < gameBoard.length; i ++){
        for (int j = 0; j < gameBoard[0].length; j++){
            gameBoard[i][j] = squareResult.EMPTY;
        }
    }
    return gameBoard;
}
public static void printBoard(squareResult[][] gameBoard){
    String answer = "-";
    for (int i = 0; i < gameBoard.length; i++){
        for (int j = 0; j < gameBoard[0].length; j++) {
            if (gameBoard[i][j] == squareResult.EMPTY) {
                answer = "-";
            } else if (gameBoard[i][j] == squareResult.O) {
                answer = "O";
            } else if (gameBoard[i][j] == squareResult.X) {
                answer = "X";
            }
            System.out.print("  " + answer + "  ");
            if (j != 2){
                System.out.print("|");
            }
            else
                System.out.println();
        }
        if (i != 2){
            System.out.println("-----+-----+-----");
        }
    }
    System.out.println();
}
public static boolean isMoveable(squareResult[][] gameBoard){
    for (int i = 0; i < gameBoard.length; i++){
        for (int j = 0; j < gameBoard[0].length; j++){
            if (gameBoard[i][j] == squareResult.EMPTY){
                return true;
            }
        }
    }
    return false;
}
public static boolean isXWin (squareResult[][] gameBoard){
    if (gameBoard[0][0] == squareResult.X && gameBoard[0][1] == squareResult.X && gameBoard[0][2] == squareResult.X){
        return true;
    }
    if (gameBoard[1][0] == squareResult.X && gameBoard[1][1] == squareResult.X && gameBoard[1][2] == squareResult.X){
        return true;
    }
    if (gameBoard[2][0] == squareResult.X && gameBoard[2][1] == squareResult.X && gameBoard[2][2] == squareResult.X){
        return true;
    }
    if (gameBoard[0][0] == squareResult.X && gameBoard[1][0] == squareResult.X && gameBoard[2][0] == squareResult.X){
        return true;
    }
    if (gameBoard[0][1] == squareResult.X && gameBoard[1][1] == squareResult.X && gameBoard[2][1] == squareResult.X){
        return true;
    }
    if (gameBoard[0][2] == squareResult.X && gameBoard[1][2] == squareResult.X && gameBoard[2][2] == squareResult.X){
        return true;
    }
    if (gameBoard[0][0] == squareResult.X && gameBoard[1][1] == squareResult.X && gameBoard[2][2] == squareResult.X){
        return true;
    }
    if (gameBoard[0][2] == squareResult.X && gameBoard[1][1] == squareResult.X && gameBoard[2][0] == squareResult.X){
        return true;
    }
    return false;
}
public static boolean isOWin (squareResult[][] gameBoard){
    if (gameBoard[0][0] == squareResult.O && gameBoard[0][1] == squareResult.O && gameBoard[0][2] == squareResult.O){
        return true;
    }
    if (gameBoard[1][0] == squareResult.O && gameBoard[1][1] == squareResult.O && gameBoard[1][2] == squareResult.O){
        return true;
    }
    if (gameBoard[2][0] == squareResult.O && gameBoard[2][1] == squareResult.O && gameBoard[2][2] == squareResult.O){
        return true;
    }
    if (gameBoard[0][0] == squareResult.O && gameBoard[1][0] == squareResult.O && gameBoard[2][0] == squareResult.O){
        return true;
    }
    if (gameBoard[0][1] == squareResult.O && gameBoard[1][1] == squareResult.O && gameBoard[2][1] == squareResult.O){
        return true;
    }
    if (gameBoard[0][2] == squareResult.O && gameBoard[1][2] == squareResult.O && gameBoard[2][2] == squareResult.O){
        return true;
    }
    if (gameBoard[0][0] == squareResult.O && gameBoard[1][1] == squareResult.O && gameBoard[2][2] == squareResult.O){
        return true;
    }
    if (gameBoard[0][2] == squareResult.O && gameBoard[1][1] == squareResult.O && gameBoard[2][0] == squareResult.O){
        return true;
    }
    return false;
}
public static boolean isTie (squareResult[][] gameBoard){
    if (isOWin(gameBoard) || isXWin(gameBoard)){
        return false;
    }
    else
        return true;
}
public static void randomXMove(squareResult[][] gameBoard){
    Random rand = new Random();

    boolean check = false;

    while (check == false) {
        int iChoice = rand.nextInt(3);
        int jChoice = rand.nextInt(3);

        if (isOpen(gameBoard, iChoice, jChoice)) {
            gameBoard[iChoice][jChoice] = squareResult.X;
            check = true;
        }
    }
}
public static void randomOMove(squareResult[][] gameBoard){
    Random rand = new Random();

    boolean check = false;

    while (check == false) {
        int iChoice = rand.nextInt(3);
        int jChoice = rand.nextInt(3);

        if (isOpen(gameBoard, iChoice, jChoice)) {
            gameBoard[iChoice][jChoice] = squareResult.O;
            check = true;
        }
    }
}
public static boolean isOpen(squareResult[][] gameBoard, int iChoice, int jChoice){
    if (gameBoard[iChoice][jChoice] == squareResult.EMPTY){
        return true;
    }
    else
        return false;
}

我似乎无法弄清楚为什么 X 胜、O 胜和平数加起来不等于 1000,非常感谢您能给我的任何帮助。谢谢

您的 randomTicTacToeGame 函数 运行 每次调用时都是一个完整的 TicTacToe 游戏。因此,您的测试代码 运行 每次迭代有 4 个不同的游戏

如果您想 运行 每次迭代一个游戏,您应该 "save" 游戏结果为一个变量,然后测试该变量,如下所示:

int count = 0;
    int xWins = 0;
    int oWins = 0;
    int ties = 0;
    boolean doPrint = true;
    while (count < 1000){
        int gameResult = randomTicTacToeGame(doPrint);

        if (gameResult == 1){
            xWins += 1;
        }
        if (gameResult == -1){
            oWins += 1;
        }
        if (gameResult == 0){
            ties += 1;
        }

        if (count >= 10){
            doPrint = false;
        }
        count++;
    }
    System.out.println();
    System.out.println("X Wins: " + xWins + "\n" + "O Wins: " + oWins + "\n" + "Ties: " + ties);