java - Windows 检查在 Tic Tac Toe 游戏中不起作用?

标签 java tic-tac-toe

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

用于检查行的 for 循环。 用于检查列的 for 循环。 两个 if 语句来检查对角线。

win 方法根本不返回 true。 && 运算符有问题吗?

最佳答案

btns[i][j] 是一个 JButton 因此它永远不会等于字符串。 您应该将 btns[i][0].equals("X") 形式的每个方法调用替换为 btns[i][0].getText().equals( “X”)

除此之外,您的 win 方法仅检查“X”玩家是否获胜。那么“O”玩家呢?

关于java - Windows 检查在 Tic Tac Toe 游戏中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32591371/

相关文章:

Java TicTacToe 程序在非获胜组合时触发

java - 如何设置代码以便允许按钮中使用值 "X"和 "O"?

java - 如何将我的域名映射到 TOMCAT 服务器中的 ZK 应用程序?

java - 如何获得不同的操作栏名称?

java - @Produces 导致 Bean 名称不明确

javascript - 我希望在由 "event"触发时显示图像,而其他问题没有任何效果

java - 用于生成 C# 代码的 XML 语法

java - 将自定义 Java 对象写入 Parquet

javascript - 尝试在我的井字棋中实现 Minimax AI (javascript)

javascript - 为什么状态没有按预期更新?