所以我明天必须交一个井字游戏。我设法编写了几乎所有代码,但我无法获得获胜者。
到目前为止,这就是我所拥有的:
package TicTacToe;
public class TicTacToeMain {
public static void main(String[] args) {
// TODO code application logic here
TicTacToe t = new TicTacToe();
t.setVisible(true);
}
}
package TicTacToe;
import javax.swing.JOptionPane;
public class TicTacToe extends javax.swing.JFrame {
public TicTacToe() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jRadioButton2 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton2.setText("jRadioButton2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setEnabled(false);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setEnabled(false);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setEnabled(false);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setEnabled(false);
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setEnabled(false);
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setEnabled(false);
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setEnabled(false);
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setEnabled(false);
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setEnabled(false);
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("New Game");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton11.setText("Reset");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jButton12.setText("Quit");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
jRadioButton1.setText("Player 1 ( X )");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
jRadioButton3.setText("Player 1 ( O )");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton1)
.addGap(62, 62, 62)
.addComponent(jRadioButton3)
.addGap(93, 93, 93))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton10)
.addComponent(jButton11)
.addComponent(jButton12)))
);
pack();
}// </editor-fold>
int count = 2;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton1.setText("X");
jButton1.setEnabled(false);
} else {
count++;
jButton1.setText("O");
jButton1.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton1.setText("O");
jButton1.setEnabled(false);
} else {
count++;
jButton1.setText("X");
jButton1.setEnabled(false);
}
}
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton2.setText("X");
jButton2.setEnabled(false);
} else {
count++;
jButton2.setText("O");
jButton2.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton2.setText("O");
jButton2.setEnabled(false);
} else {
count++;
jButton2.setText("X");
jButton2.setEnabled(false);
}
}
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton3.setText("X");
jButton3.setEnabled(false);
} else {
count++;
jButton3.setText("O");
jButton3.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton3.setText("O");
jButton3.setEnabled(false);
} else {
count++;
jButton3.setText("X");
jButton3.setEnabled(false);
}
}
}
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton5.setText("X");
jButton5.setEnabled(false);
} else {
count++;
jButton5.setText("O");
jButton5.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton5.setText("O");
jButton5.setEnabled(false);
} else {
count++;
jButton5.setText("X");
jButton5.setEnabled(false);
}
}
}
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton6.setText("X");
jButton6.setEnabled(false);
} else {
count++;
jButton6.setText("O");
jButton6.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton6.setText("O");
jButton6.setEnabled(false);
} else {
count++;
jButton6.setText("X");
jButton6.setEnabled(false);
}
}
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton4.setText("X");
jButton4.setEnabled(false);
} else {
count++;
jButton4.setText("O");
jButton4.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton4.setText("O");
jButton4.setEnabled(false);
} else {
count++;
jButton4.setText("X");
jButton4.setEnabled(false);
}
}
}
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton8.setText("X");
jButton8.setEnabled(false);
} else {
count++;
jButton8.setText("O");
jButton8.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton8.setText("O");
jButton8.setEnabled(false);
} else {
count++;
jButton8.setText("X");
jButton8.setEnabled(false);
}
}
}
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton9.setText("X");
jButton9.setEnabled(false);
} else {
count++;
jButton9.setText("O");
jButton9.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton9.setText("O");
jButton9.setEnabled(false);
} else {
count++;
jButton9.setText("X");
jButton9.setEnabled(false);
}
}
}
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
if (count % 2 == 0) {
count++;
jButton7.setText("X");
jButton7.setEnabled(false);
} else {
count++;
jButton7.setText("O");
jButton7.setEnabled(false);
}
} else {
if (jRadioButton3.isSelected()) {
if ((count % 2 == 0)) {
count++;
jButton7.setText("O");
jButton7.setEnabled(false);
} else {
count++;
jButton7.setText("X");
jButton7.setEnabled(false);
}
}
}
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jButton1.setText("");
jButton1.setEnabled(false);
jButton2.setText("");
jButton2.setEnabled(false);
jButton3.setText("");
jButton3.setEnabled(false);
jButton4.setText("");
jButton4.setEnabled(false);
jButton5.setText("");
jButton5.setEnabled(false);
jButton6.setText("");
jButton6.setEnabled(false);
jButton7.setText("");
jButton7.setEnabled(false);
jButton8.setText("");
jButton8.setEnabled(false);
jButton9.setText("");
jButton9.setEnabled(false);
jRadioButton1.setSelected(false);
jRadioButton3.setSelected(false);
JOptionPane.showMessageDialog(null, "Seleccione ( X ) o ( O )");
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
jButton1.setEnabled(true);
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
jButton7.setEnabled(true);
jButton8.setEnabled(true);
jButton9.setEnabled(true);
}
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton3.isSelected()) {
jButton1.setEnabled(true);
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
jButton7.setEnabled(true);
jButton8.setEnabled(true);
jButton9.setEnabled(true);
}
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jButton1.setText("");
jButton1.setEnabled(false);
jButton2.setText("");
jButton2.setEnabled(false);
jButton3.setText("");
jButton3.setEnabled(false);
jButton4.setText("");
jButton4.setEnabled(false);
jButton5.setText("");
jButton5.setEnabled(false);
jButton6.setText("");
jButton6.setEnabled(false);
jButton7.setText("");
jButton7.setEnabled(false);
jButton8.setText("");
jButton8.setEnabled(false);
jButton9.setText("");
jButton9.setEnabled(false);
jRadioButton1.setSelected(false);
jRadioButton3.setSelected(false);
count=0;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TicTacToe().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
// End of variables declaration
//if(jButton1.equals(jButton2) && jButton2.equals(jButton3)&& (jButton2) != "")) {
if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
ganador = true;
}
}
游戏运行良好,但我不知道如何获得获胜者。
我不确定我需要在代码中的确切位置放置它。
首先我试过
if((jButton1.getText())==(jButton2.getText())&&((jButton2.getText())==(jButton3.getText()){
ganador =真;
}
但它说它缺少返回语句。然后我更改了代码以使用 .equals 但它说它再次缺少返回语句。
if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
ganador = true;
}
另外,我将这部分代码放在最后,但不确定这是否是问题所在。