gpt4 book ai didi

java - 为什么我在 TicTacToe 中用于计算 X 和 O 获胜的变量在每次测试中都会有所不同? ( java )

转载 作者:行者123 更新时间:2023-12-01 11:48:56 27 4
gpt4 key购买 nike

我目前正在从事一个指定的项目,用 java 创建一个 tic tac toe 游戏。这是我到目前为止所拥有的,所有代码都运行得很好,游戏及其功能也运行得很好。我应该记录 X 胜、O 胜和平局;然而,这似乎就是问题所在。出于某种原因,即使我指定计算机模拟井字游戏应该有 1000 次迭代,但 x 获胜、o 获胜和平局加起来永远不会达到 1000。它始终是一个非常接近 1000 的数字,但我不是确定为什么它不断变化。这是我在主类中的代码:

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

最佳答案

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

关于java - 为什么我在 TicTacToe 中用于计算 X 和 O 获胜的变量在每次测试中都会有所不同? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28935913/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com