java - Java 中带有 GUI 的 TicTacToe

标签 java tic-tac-toe

我编写了一个玩 TicTacToe 的程序。 这是该类,其中包含检查玩家是否获胜的方法:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TicTacToe {
    public static int count = 0;
    public static String[][] board = new String[3][3];

    public static void buttonClicked(JButton button) {
        if(button.getText().equals("")) {
            count++;
            if(count % 2 == 1) {
                button.setText("X");
            }
            if(count % 2 == 0) {
                button.setText("O");
            }
        }   
    }


    public static void gameRules(JButton button) {
        //"X" or "O"?
        String string = button.getText();

        //Gives coordinates of the button
        int x = Character.getNumericValue(button.getName().charAt(0));
        int y = Character.getNumericValue(button.getName().charAt(1));
        board[x][y] = string;

        //diagonal
        if(board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2])) {
            JOptionPane.showMessageDialog(null,string + " won."); 

        }

        //other diagonal
        else if(board[0][2].equals(board[1][1]) && board[1][1].equals(board[2][0])) {
            JOptionPane.showMessageDialog(null,string + " won."); 

        }

        //draw?
        else if(count==9) {
            JOptionPane.showMessageDialog(null,"draw."); 

        }

        else {

            //row
            for(int i = 0; i < 3; i++) {
                if(board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2])) {
                    JOptionPane.showMessageDialog(null,string + " won."); 

                }
            }

            //column
            for(int j = 0; j < 3; j++) {
                if(board[0][j].equals(board[1][j]) && board[1][j].equals(board[2][j])) {
                    JOptionPane.showMessageDialog(null,string + " won."); 

                }
            }
        }   
    }
}

这是另外两个类,现在您拥有完整的代码:

public class Control {
    public static void main(String args[]) {
        Gui gui = new Gui();
        TicTacToe ticTacToe = new TicTacToe();
    }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Gui {
    public Gui() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new java.awt.GridLayout(3, 3));

        for (int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++) {
                final JButton button = new JButton();
                String string = i +  "" + j;
                button.setText("");
                button.setName(string);
                button.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            TicTacToe.buttonClicked(button);
                            TicTacToe.gameRules(button);
                        }
                    });
                button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                panel.add(button);
            }

        }

        frame.add(panel);
        frame.setSize(400,400);
        frame.setVisible(true);




    }
}

我的问题是,程序现在可以识别某人在对角线上获胜,而不是在行和列上获胜。

如果您能帮助解决该问题,我将不胜感激。

最佳答案

您只需为 TicTacToe.java 导入import javax.swing.*;。 编辑 win if 语句并删除静态定义:

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

TicTacToe ticTacToe = new TicTacToe(); 添加到 Gui 变量中。 更改 Action 监听器:

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

从控件中删除TicTacToe ticTacToe = new TicTacToe();。 经常检查你的错误,你遇到的 NullPointerException 异常多得你数不过来。

关于java - Java 中带有 GUI 的 TicTacToe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454490/

相关文章:

java - 将复杂的 while 循环重构为 Java 8 流

"indexed"属性文件的 Java API

java - 阻止方法参数名称在 Android Studio 中突出显示

javascript - jQuery Tic Tac Toe 程序在它应该只选择一个时选择了两种组合

java - 重播井字游戏

java - 此内容无法在框架中显示 - eclipse (displayHelpResource(href))

java - Cassandra 中的 IN 关系对查询不利吗?

java - 无法让这个 Tic Tac Toe 游戏公平竞争(第 2 部分)

java - 井字棋板二维阵列

c - 我的 tictactoe 极小极大算法有什么问题