Search code examples
javaintcountingtic-tac-toe

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


I am currently working on an assigned project to create a tic tac toe game in java. Here is what I have so far, all the code works pretty much and the game and its functions work perfectly fine. I am supposed to keep track of X Wins, O Wins, and Ties; however, which is where the problem seems to be. For some reason, Even though I specify there should be 1000 iterations of the computer simulated tic tac toe game, the x wins, o wins, and ties never add up to 1000. It's always a number really close to 1000, but I am not sure why it constantly varies. Here's my code in the main 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);

And here's the code for the actual tic tac toe game:

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

I can't seem to figure out why the X Wins, O Wins, and Ties do not add to 1000, any help you can give me is greatly appreciated. Thank you


Solution

  • Your randomTicTacToeGame function runs a complete game of TicTacToe every time it is called. Your test code therefore runs 4 different games at each iteration

    If you want to run a single game per iteration, you should "save" the game result in a variable, and test that variable, as follows:

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