Search code examples
javatic-tac-toe

Win check not working in TicTacToe game?


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


class Tix extends JFrame {
    private static final int WIDTH = 500;
    private static final int HEIGHT = 500;
    private boolean xTurn = true;
    private Font style;
    private static JButton[][] btns = new JButton[3][3];

public Tix() {
    setTitle("Tix");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(WIDTH, HEIGHT);
    createContents();
    setVisible(true);
}

public void createContents() {
    style = new Font("Comic Sans", 1, 100);
    Listener listener = new Listener();

    setLayout(new GridLayout(3,3));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            btns[i][j] = new JButton();
            btns[i][j].setFont(style);
            btns[i][j].addActionListener(listener);
            add(btns[i][j]);
        }
    }
}
private class Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        JButton btn = (JButton) e.getSource();

        if (xTurn)
            btn.setForeground(Color.RED);
        else
            btn.setForeground(Color.BLUE);


        if (btn.getText().isEmpty()) {
            btn.setText(xTurn ? "X" : "O");

            if (win()) {
                JOptionPane.showMessageDialog(null, "Congratulations! Player " + (xTurn ? "X" : "O") + " wins!");
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        btns[i][j].setText(null);
                    }
                }
                xTurn = true;
            }
            else {
                xTurn = !xTurn;
            }
        }
        else {
            JOptionPane.showMessageDialog(null,"Cell already clicked!");
        }
    }
}
public static boolean win() {

    for (int i = 0; i < 3; i++)
        if (btns[i][0].equals("X") && btns[i][1].equals("X") && btns[i][2].equals("X"))
            return true;

    for (int j = 0; j < 3; j++)
        if (btns[0][j].equals("X") && btns[1][j].equals("X") && btns[2][j].equals("X"))
            return true;

    if (btns[0][0].equals("X") && btns[1][1].equals("X") && btns[2][2].equals("X"))
        return true;

    if (btns[0][2].equals("X") && btns[1][1].equals("X") && btns[2][0].equals("X"))
        return true;

    return false;

}

public static void main(String[] args) {
    new Tix();
}
}

A for loop to check for the rows. A for loop to check for the columns. Two if statements to check for diagonals.

The win method does not return true at all. Is there something wrong with the && operators?


Solution

  • btns[i][j] is a JButton so it will never be equal to a String. You should replace each method call of the form btns[i][0].equals("X") with something like btns[i][0].getText().equals("X").

    Beside that, your win method only checks if the "X" player won. What about the "O" player?