当前位置: 首页 > 知识库问答 >
问题:

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

西门靖琪
2023-03-14

我目前正在工作的一个分配的项目,以创造一个抽搐tac脚趾游戏在Java。这是我到目前为止,所有的代码工作相当好,游戏和它的功能工作非常好。我应该记录X胜、O胜和平局;然而,这似乎是问题所在。由于某种原因,即使我指定了计算机模拟的tic tac toe游戏应该有1000次迭代,x胜、o胜和平局加起来从来不是1000。它总是一个非常接近1000的数字,但我不知道为什么它总是变化。下面是我在main类中的代码:

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

下面是实际的tic tac toe游戏的代码:

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,你能给我的任何帮助都非常感谢。谢谢

共有1个答案

任昊苍
2023-03-14

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

如果您希望每次迭代运行一个游戏,那么您应该将游戏结果“保存”在一个变量中,并测试该变量,如下所示:

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);
 类似资料:
  • 因此,我从文件夹中链接了两个HTML图像:images/但是在javascript中,我试图让骰子在每次刷新后随机更改这些图像。我不知道为什么它不起作用 下面是HTML代码 下面是js代码 注意:我是一个新的学习者,仍在努力理解javascript。谢谢你谦虚的回答和帮助。如果解释得简单一点,我就可以理解了。 修复上面的代码后 我在刷新页面时遇到问题这就是它显示的内容 在此处输入图像描述

  • 为什么gcc在上的末尾插入了,而我却要自己在上添加? 从:

  • 出于某种原因,我为Checkbutton变量编写的代码在函数内部时不会改变其工作方式,但是在函数外部和在其自身脚本中时,它会工作。下面是函数外部的代码 下面是在函数内部定义的代码 对于代码的顶部,当我选中一个框时,变量变为True。对于最下面的一个,不管我做什么,它们都保持在False。

  • 为什么输出的 vsnprintf 的返回值随着执行次数不同而变化呢? man vsnrpintf 手册上说的返回值是写入最终的字符串长度(如下截图中:the final string),但是实际测试并不是? 按文档说法:返回值应该为3; 实际测试结果:不为3,也不为10,而是14、15、16这样变化的数。 输出: ./a.out ====res1=10 ====res2=16, buf=sa ./

  • 问题内容: 因此,我的游戏从垂直方向开始,然后最终变为水平方向。我知道屏幕的宽度是屏幕的宽度,并且在垂直方向上与高度相同。切换为水平方向时,高度是否变为宽度,宽度是否变为高度?还是保持不变? 问题答案: 是的,屏幕的宽度和高度取决于设备的方向。您可以获取显示的大小,用该显示大小可以代替不推荐使用的方法和。从文档中:根据显示器的当前旋转为您调整此值。

  • 方向从一个片段改变到另一个片段。 方位1(景观至肖像): 片段1的OnSaveInstanceState()。 片段2的OnSaveInstanceState()。 片段2的 onstop(). 片段1的 ondestroy(). 片段1的 ondetach()。 片段2的onattach(). 片段2的oncreateview()。 片段2的 onstart(). null 我是这样添加片段的-