gpt4 book ai didi

java - Java 中带有 GUI 的 TicTacToe

转载 作者:行者123 更新时间:2023-12-01 19:12:54 26 4
gpt4 key购买 nike

我编写了一个玩 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com