我已经编写了一个播放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);
}
}
我的问题是,程序现在可以识别出有人在对角线上获胜,但在行和列上却没有。
我将很乐意为您解决问题。
编辑win if语句并删除静态定义:
//diagonal
if(board[0][0] != null && 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] != null && 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 {
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[i][0] != 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变量中。更改动作侦听器:
public void actionPerformed(ActionEvent e) {
ticTacToe.buttonClicked(button);
ticTacToe.gameRules(button);
}
从控件中删除TicTacToe ticTacToe = new TicTacToe();
。始终检查您的错误,您有更多的nullPtr异常超出您的计算范围。