我正在为学校项目创建一个Tic-Tac-Toe GUI。我很困惑如何做一些检查胜利的东西会有一些帮助或提示如何创建一个胜利者检查。我也很困惑,为什么每当我点击一个按钮时,我的控制台都被垃圾邮件与事件错误。所有帮助表示赞赏。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Main extends JFrame implements ActionListener {
int count = 0;
int letter = 0;
int round = 2;
JPanel j;
JButton[] button;
String[] letters;
public Main() { // The GUI
setTitle("Tic-Tac-Toe");
setSize(new Dimension(500, 500));
setLocation(400, 200);
setLayout(new BorderLayout());
j = new JPanel(new GridLayout(3, 3));
this.add(j);
button = new JButton[9];
for (int i = 0; i < 9; i++) { //9 Buttons
button[i] = new JButton();
j.add(button[i]);
button[i].setEnabled(true);
button[i].addActionListener(this);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
Main world = new Main();
}
public void actionPerformed(ActionEvent e) {
count++;
for (int i = 0; i <= 9; i++) {
if (button[i] == e.getSource()) { //sets buttons to X or O
if (letter == 0) {
button[i].setText("X");
button[i].setEnabled(false);
letter++;
letters[i] = "X"; //would like to use to check wins
} else {
button[i].setText("O");
button[i].setEnabled(false);
letter = 0;
letters[i] = "O"; //would like to use to check wins
}
}
if (count >= 9) {//Resets board and starts a new round
JOptionPane.showMessageDialog(null, "Its a Draw! Start round "+ round +"!");
round++;
count = 0;
button[i].setText("");
button[i].setEnabled(true);
button[i + 1].setText("");
button[i + 1].setEnabled(true);
button[i + 2].setText("");
button[i + 2].setEnabled(true);
button[i + 3].setText("");
button[i + 3].setEnabled(true);
button[i + 4].setText("");
button[i + 4].setEnabled(true);
button[i + 5].setText("");
button[i + 5].setEnabled(true);
button[i + 6].setText("");
button[i + 6].setEnabled(true);
button[i + 7].setText("");
button[i + 7].setEnabled(true);
button[i + 8].setText("");
button[i + 8].setEnabled(true);
button[i + 9].setText("");
button[i + 9].setEnabled(true);
i = 0;
}
}
}
}
每次单击按钮时,都会出现与此类似的错误:
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:2)
您获得空指针异常,因为您没有为数组分配空格。尝试:
String[] letters = new String[9];
使用此版本运行时,您将看到数组索引超出范围:9,因为您的for循环应该只迭代到8,从0开始。
为了检查某人是否赢了,首先找出哪个方块对应于哪个阵列位置。然后,您可以看到其中一个符号是否已占据阵列中八个可能的获胜位置之一。例如,X取对角线获胜。这可以通过以下方式完成:
if (possibility1 || possibility2 || possibility3... || possibility8)
[code to run on winning result]
每次玩家将X或O输入棋盘时都会进行此检查。在你的情况下,它是你写“//想用来检查胜利”的地方。
另外,我不确定你是否已经知道这一点,但是在该控制台输出中只有一个错误,而不是很多。它显示了一系列调用导致错误(顶部),最底层调用另一个调用另一个调用,向上调用另一个调用。