javaalgorithmloopsif-statementtic-tac-toe

TicTacToe win checking logic


I made a method to check for a win in a simple TicTacToe I made, it is very long and has a bunch of if statements, I want to know if I can make it in loops efficiently. I searched but only found already made code. I want an explanation.

Here is the block of code I made to check for wins in a simple TicTacToe game:

`public static boolean checkStatus() {
        x_win = false;
        o_win = false;
        if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') {
            x_win = true;
        } else if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') {
            x_win = true;
        } else if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') {
            x_win = true;
        } else if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') {
            x_win = true;
        } else if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') {
            x_win = true;
        } else if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') {
            x_win = true;`
        `} else if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') {
            x_win = true;
        } else if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') {
            x_win = true;
        } else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') {
            o_win = true;
        } else if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') {
            o_win = true;
        } else if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') {
            o_win = true;
        } else if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') {
            o_win = true;`
       ` } else if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') {
            o_win = true;
        } else if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') {
            o_win = true;
        } else if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') {
            o_win = true;
        } else if (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') {
            o_win = true;
        }`

        if (x_win) {
            return true;
        }
        else return o_win;
    }

Solution

  • It's because your use of else if stops the code from checking all win possibilities, so some winning lines like diagonals might get skipped. You could try using separate if statements instead, so every line gets checked.

    It should look something like this:

    public static boolean checkStatus() {
        x_win = false;
        o_win = false;
    
        // Check rows and columns
        for (int i = 0; i < 3; i++) {
            if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                if (board[i][0] == 'X') x_win = true;
                else if (board[i][0] == 'O') o_win = true;
            }
    
            if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
                if (board[0][i] == 'X') x_win = true;
                else if (board[0][i] == 'O') o_win = true;
            }
        }
    
        // Check diagonals
        if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
            if (board[0][0] == 'X') x_win = true;
            else if (board[0][0] == 'O') o_win = true;
        }
    
        if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
            if (board[0][2] == 'X') x_win = true;
            else if (board[0][2] == 'O') o_win = true;
        }
    
        return x_win || o_win;
    }