TicTacToe with GUI in Java

You only need to import import javax.swing.*; for the TicTacToe.java. Edit the win if statements and delete the static definitions:

if(board[0][0] != null && board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2])) {
     JOptionPane.showMessageDialog(null,string + " won."); 
} else if(board[0][2] != null && board[0][2].equals(board[1][1]) && board[1][1].equals(board[2][0])) {
     JOptionPane.showMessageDialog(null,string + " won."); 
} else if(count == 9) {
     JOptionPane.showMessageDialog(null, "draw."); 
} else {
    for (int i = 0; i < 3; i++) {
        if (board[i][0] != null && board[i][0].equals(board[i][1]) && board[i][0].equals(board[i][2])) {
            JOptionPane.showMessageDialog(null, string + " won."); break;
        } 
        if (board[0][i] != null && board[0][i].equals(board[1][i]) && board[0][i].equals(board[2][i])) {
            JOptionPane.showMessageDialog(null, string + " won."); break;
        }
    }
}

Add TicTacToe ticTacToe = new TicTacToe(); to the Gui variables. Change the action listener:

public void actionPerformed(ActionEvent e) {
    ticTacToe.buttonClicked(button);
    ticTacToe.gameRules(button);
}

Delete TicTacToe ticTacToe = new TicTacToe(); from the Control. Always check your errors, you had more NullPointerException exception than you can count.

Leave a Comment