Tictactoe Check for Draw

Wrote a TicTacToe game with a GUI using JButtons. Below is the code used to check if there is a winner. Also included is my code to check for a draw. HOWEVER, on the event that all buttons are clicked AND the last move results in a win for one of the players, it is still considered a draw due to all of the buttons being clicked. How can I fix it so that it will accurately check for a draw?

public void checkWin(char[] values)
{
    //Check if player 1 (xs) win
    if ((values[0] == 'x') && (values[1] == 'x') && (values[2] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[3] == 'x') && (values[4] == 'x') && (values[5] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[6] == 'x') && (values[7] == 'x') && (values[8] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[0] == 'x') && (values[4] == 'x') && (values[8] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[2] == 'x') && (values[4] == 'x') && (values[6] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[0] == 'x') && (values[3] == 'x') && (values[6] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[1] == 'x') && (values[4] == 'x') && (values[7] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    else if ((values[2] == 'x') && (values[5] == 'x') && (values[8] == 'x'))
    {
        playerLabel.setText("Player 1 wins!");
        disableButtons();
    }
    // check for draw
    else if (((values[0] == 'x') || (values[0] == 'o')) && ((values[1] == 'x') || (values[1] == 'o')) &&
    ((values[2] == 'x') || (values[2] == 'o')) && ((values[3] == 'x') || (values[3] == 'o')) && ((values[4] == 'x') || (values[4] == 'o'))
    && ((values[5] == 'x') || (values[5] == 'o')) && ((values[6] == 'x') || (values[6] == 'o')) && ((values[7] == 'x') || (values[7] == 'o'))
    && ((values[8] == 'x') || (values[8] == 'o')))
    {
        playerLabel.setText("DRAW!");
        disableButtons();
    }

    //Check is player 2 (os) wins
    if ((values[0] == 'o') && (values[1] == 'o') && (values[2] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[3] == 'o') && (values[4] == 'o') && (values[5] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[6] == 'o') && (values[7] == 'o') && (values[8] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[0] == 'o') && (values[4] == 'o') && (values[8] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[2] == 'o') && (values[4] == 'o') && (values[6] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[0] == 'o') && (values[3] == 'o') && (values[6] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[1] == 'o') && (values[4] == 'o') && (values[7] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    else if ((values[2] == 'o') && (values[5] == 'o') && (values[8] == 'o'))
    {
        playerLabel.setText("Player 2 wins!");
        disableButtons();
    }
    // check for draw
    else if (((values[0] == 'x') || (values[0] == 'o')) && ((values[1] == 'x') || (values[1] == 'o')) &&
    ((values[2] == 'x') || (values[2] == 'o')) && ((values[3] == 'x') || (values[3] == 'o')) && ((values[4] == 'x') || (values[4] == 'o'))
    && ((values[5] == 'x') || (values[5] == 'o')) && ((values[6] == 'x') || (values[6] == 'o')) && ((values[7] == 'x') || (values[7] == 'o'))
    && ((values[8] == 'x') || (values[8] == 'o')))
    {
        playerLabel.setText("DRAW!");
        disableButtons();
    }
}
3

3 Answers

Just check for all winning scenarios, then in the last else statement it should be a draw...

else{
  playerLabel.setText("DRAW!");
  disableButtons();
}

On another note, you can really shorten your code here by creating a generic method and passing it the appropriate arguments..This code is very cumbersome.

You can do this by:

1 - you use a String text variable that stores the text that needs to be setted on the player label.

2 - on each if, you set text variable according to the text that needs to be setted, for example on the ifs that the player 1 wins, you put text = "PLAYER 1 WINS".

3 - you only set the player label once, on the end of the method, and not in each "if" as you put on the code above.

4- Use two boolean variables, like boolean player1=false, player2=false. And when player1 wins, its set to true, otherwise false. The same applies to player2. In other words, on each if that for example the player 1 wins, you put player1=true.

5- In the end, you just need to check, if both player1 and player2 variable is true, then its draw. (And dont forget to set the variable text = "DRAW").

You could use some functions and loops:

public void checkGameOver(char[] values) {
    if (checkWin(values, 'x')) {
        playerLabel.setText("Player 1 wins!");
    } else if (checkWin(values, 'o')) {
        playerLabel.setText("Player 2 wins!");
    } else if (checkDraw(values)) {
        playerLabel.setText("DRAW");
    } else {
        return;
    }

    disableButtons();
}

public boolean checkWins(char[] values, char player) {
    if (((values[0] == player) && (values[1] == player) && (values[2] == player)) ||
        ((values[3] == player) && (values[4] == player) && (values[5] == player)) ||
        ((values[6] == player) && (values[7] == player) && (values[8] == player)) ||
        ((values[0] == player) && (values[4] == player) && (values[8] == player)) ||
        ((values[2] == player) && (values[4] == player) && (values[6] == player)) ||
        ((values[0] == player) && (values[3] == player) && (values[6] == player)) || 
        ((values[1] == player) && (values[4] == player) && (values[7] == player)) ||
        ((values[2] == player) && (values[5] == player) && (values[8] == player)))
    {
        return true;
    }

    return false;
}

public boolean checkDraw(char[] values) {
    for (char c : values) { // check if each box holds either an 'x' or an 'o'
        if (c != 'o' && c != 'x') {
            return false;
        }
    }

    return true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest