我正在使用Java开发基本的Tic Tac Toe游戏。它应该在一个网格中有9个按钮,当你点击一个按钮时,会出现X.单击按钮后,一种切换变量 public void actionPerformed (ActionEvent evt) {
int i = 1;
boolean b1xon=false;
boolean b2xon=false;
boolean b3xon=false;
boolean b4xon=false;
boolean b5xon=false;
boolean b6xon=false;
boolean b7xon=false;
boolean b8xon=false;
boolean b9xon=false;
boolean b1oon=false;
boolean b2oon=false;
boolean b3oon=false;
boolean b4oon=false;
boolean b5oon=false;
boolean b6oon=false;
boolean b7oon=false;
boolean b8oon=false;
boolean b9oon=false;
boolean win=false;
if ((evt.getSource() == b1) && (i == 1)) {
b1.setText("X");
i++;
b1xon=true;
}
else if (evt.getSource() == b1 && i == 2) {
b1.setText("O");
i = 1;
b1oon=true;
}
else if (evt.getSource() == b2 && i == 1) {
b2.setText("X");
i++;
b2xon=true;
}
else if (evt.getSource() == b2 && i == 2) {
b2.setText("O");
i = 1;
b2oon=true;
}
else if (evt.getSource() == b3 && i == 1) {
b3.setText("X");
i++;
b3xon=true;
}
else if (evt.getSource() == b3 && i == 2) {
b3.setText("O");
i = 1;
b3oon=true;
}
else if (evt.getSource() == b4 && i == 1) {
b4.setText("X");
i++;
b4xon=true;
}
else if (evt.getSource() == b4 && i == 2) {
b4.setText("O");
i = 1;
b4oon=true;
}
else if (evt.getSource() == b5 && i == 1) {
b5.setText("X");
i++;
b5xon=true;
}
else if (evt.getSource() == b5 && i == 2) {
b5.setText("O");
i = 1;
b5oon=true;
}
else if (evt.getSource() == b6 && i == 1) {
b6.setText("X");
i++;
b6xon=true;
}
else if (evt.getSource() == b6 && i == 2) {
b6.setText("O");
i = 1;
b6oon=true;
}
else if (evt.getSource() == b7 && i == 1) {
b7.setText("X");
i++;
b7xon=true;
}
else if (evt.getSource() == b7 && i == 2) {
b7.setText("O");
i = 1;
b7oon=true;
}
else if (evt.getSource() == b8 && i == 1) {
b8.setText("X");
i++;
b8xon=true;
}
else if (evt.getSource() == b8 && i == 2) {
b8.setText("O");
i = 1;
b8oon=true;
}
else if (evt.getSource() == b9 && i == 1) {
b9.setText("X");
i++;
b9xon=true;
}
else if(evt.getSource() == b9 && i==1){
b9.setText("O");
i = 1;
b9oon=true;
}
if((b1xon&&b2xon&&b3xon)||(b4xon&&b5xon&&b6xon)||(b7xon&&b8xon&&b9xon)||(b1xon&&b4xon&&b7xon)||(b2xon&&b5xon&&b8xon)||(b3xon&&b6xon&&b9xon)||(b1xon&&b5xon&&b9xon)||(b3xon&&b5xon&&b7xon)){
win=true;
}
else if((b1oon&&b2oon&&b3oon)||(b4oon&&b5oon&&b6oon)||(b7oon&&b8oon&&b9oon)||(b1oon&&b4oon&&b7oon)||(b2oon&&b5oon&&b8oon)||(b3oon&&b6oon&&b9oon)||(b1oon&&b5oon&&b9oon)||(b3oon&&b5oon&&b7oon)){
win=true;
}
应该更改值,因此下次单击O时会出现。没有语法错误,但是存在问题。当我点击一个按钮时,X会出现,但是当我点击另一个按钮而不是O出现时,X再次出现。
这是我的逻辑代码。
{{1}}